barPolar.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { mathAbs, mathMax, mathMin, mathPI, parsePercent } from '../util/number.js';
  41. import { isDimensionStacked } from '../data/helper/dataStackHelper.js';
  42. import AngleAxis from '../coord/polar/AngleAxis.js';
  43. import RadiusAxis from '../coord/polar/RadiusAxis.js';
  44. import { calcBandWidth } from '../coord/axisBand.js';
  45. import { createBandWidthBasedAxisContainShapeHandler, makeAxisStatKey2 } from '../chart/helper/axisSnippets.js';
  46. import { createSimpleOverallStageHandler, makeCallOnlyOnce } from '../util/model.js';
  47. import { registerAxisContainShapeHandler } from '../coord/scaleRawExtentInfo.js';
  48. import { getStartValue, requireAxisStatisticsForBaseBar, SERIES_TYPE_BAR } from './barCommon.js';
  49. import { eachAxisOnKey, eachSeriesOnAxisOnKey } from '../coord/axisStatistics.js';
  50. import { COORD_SYS_TYPE_POLAR } from '../coord/polar/PolarModel.js';
  51. import { assert, each } from 'zrender/lib/core/util.js';
  52. var callOnlyOnce = makeCallOnlyOnce();
  53. function getSeriesStackId(seriesModel) {
  54. return seriesModel.get('stack') || '__ec_stack_' + seriesModel.seriesIndex;
  55. }
  56. export var barLayoutPolarStageHandler = createSimpleOverallStageHandler(SERIES_TYPE_BAR, barLayoutPolar);
  57. function barLayoutPolar(ecModel) {
  58. var axisStatKey = makeAxisStatKey2(SERIES_TYPE_BAR, COORD_SYS_TYPE_POLAR);
  59. eachAxisOnKey(ecModel, axisStatKey, function (axis) {
  60. if (process.env.NODE_ENV !== 'production') {
  61. assert(axis instanceof AngleAxis || axis instanceof RadiusAxis);
  62. }
  63. var barWidthAndOffset = calcRadialBar(axis, SERIES_TYPE_BAR);
  64. var lastStackCoords = {};
  65. eachSeriesOnAxisOnKey(axis, axisStatKey, function (seriesModel) {
  66. layoutPerAxisPerSeries(axis, seriesModel, barWidthAndOffset, lastStackCoords);
  67. });
  68. });
  69. }
  70. function layoutPerAxisPerSeries(baseAxis, seriesModel, barWidthAndOffset, lastStackCoords) {
  71. var data = seriesModel.getData();
  72. var stackId = getSeriesStackId(seriesModel);
  73. var columnLayoutInfo = barWidthAndOffset[stackId];
  74. var columnOffset = columnLayoutInfo.offset;
  75. var columnWidth = columnLayoutInfo.width;
  76. var polar = seriesModel.coordinateSystem;
  77. if (process.env.NODE_ENV !== 'production') {
  78. assert(polar.type === COORD_SYS_TYPE_POLAR);
  79. }
  80. var valueAxis = polar.getOtherAxis(baseAxis);
  81. var cx = polar.cx;
  82. var cy = polar.cy;
  83. var barMinHeight = seriesModel.get('barMinHeight') || 0;
  84. var barMinAngle = seriesModel.get('barMinAngle') || 0;
  85. lastStackCoords[stackId] = lastStackCoords[stackId] || [];
  86. var valueDim = data.mapDimension(valueAxis.dim);
  87. var baseDim = data.mapDimension(baseAxis.dim);
  88. var stacked = isDimensionStacked(data, valueDim /* , baseDim */);
  89. var clampLayout = baseAxis.dim !== 'radius' || !seriesModel.get('roundCap', true);
  90. var valueAxisStart = valueAxis.dataToCoord(getStartValue(valueAxis));
  91. for (var idx = 0, len = data.count(); idx < len; idx++) {
  92. var value = data.get(valueDim, idx);
  93. var baseValue = data.get(baseDim, idx);
  94. var sign = value >= 0 ? 'p' : 'n';
  95. var baseCoord = valueAxisStart;
  96. // Because of the barMinHeight, we can not use the value in
  97. // stackResultDimension directly.
  98. if (stacked) {
  99. // FIXME: follow the same logic in `barGrid.ts`:
  100. // Use stackResultDimension, and lastStackCoords is not needed.
  101. if (!lastStackCoords[stackId][baseValue]) {
  102. lastStackCoords[stackId][baseValue] = {
  103. p: valueAxisStart,
  104. n: valueAxisStart // Negative stack
  105. };
  106. }
  107. // Should also consider #4243
  108. baseCoord = lastStackCoords[stackId][baseValue][sign];
  109. }
  110. var r0 = void 0;
  111. var r = void 0;
  112. var startAngle = void 0;
  113. var endAngle = void 0;
  114. // radial sector
  115. if (valueAxis.dim === 'radius') {
  116. var radiusSpan = valueAxis.dataToCoord(value) - valueAxisStart;
  117. var angle = baseAxis.dataToCoord(baseValue);
  118. if (mathAbs(radiusSpan) < barMinHeight) {
  119. radiusSpan = (radiusSpan < 0 ? -1 : 1) * barMinHeight;
  120. }
  121. r0 = baseCoord;
  122. r = baseCoord + radiusSpan;
  123. startAngle = angle - columnOffset;
  124. endAngle = startAngle - columnWidth;
  125. stacked && (lastStackCoords[stackId][baseValue][sign] = r);
  126. }
  127. // tangential sector
  128. else {
  129. var angleSpan = valueAxis.dataToCoord(value, clampLayout) - valueAxisStart;
  130. var radius = baseAxis.dataToCoord(baseValue);
  131. if (mathAbs(angleSpan) < barMinAngle) {
  132. angleSpan = (angleSpan < 0 ? -1 : 1) * barMinAngle;
  133. }
  134. r0 = radius + columnOffset;
  135. r = r0 + columnWidth;
  136. startAngle = baseCoord;
  137. endAngle = baseCoord + angleSpan;
  138. // if the previous stack is at the end of the ring,
  139. // add a round to differentiate it from origin
  140. // let extent = angleAxis.getExtent();
  141. // let stackCoord = angle;
  142. // if (stackCoord === extent[0] && value > 0) {
  143. // stackCoord = extent[1];
  144. // }
  145. // else if (stackCoord === extent[1] && value < 0) {
  146. // stackCoord = extent[0];
  147. // }
  148. stacked && (lastStackCoords[stackId][baseValue][sign] = endAngle);
  149. }
  150. data.setItemLayout(idx, {
  151. cx: cx,
  152. cy: cy,
  153. r0: r0,
  154. r: r,
  155. // Consider that positive angle is anti-clockwise,
  156. // while positive radian of sector is clockwise
  157. startAngle: -startAngle * mathPI / 180,
  158. endAngle: -endAngle * mathPI / 180,
  159. /**
  160. * Keep the same logic with bar in catesion: use end value to
  161. * control direction. Notice that if clockwise is true (by
  162. * default), the sector will always draw clockwisely, no matter
  163. * whether endAngle is greater or less than startAngle.
  164. */
  165. clockwise: startAngle >= endAngle
  166. });
  167. }
  168. }
  169. /**
  170. * Calculate bar width and offset for radial bar charts
  171. */
  172. function calcRadialBar(axis, seriesType) {
  173. var axisStatKey = makeAxisStatKey2(seriesType, COORD_SYS_TYPE_POLAR);
  174. var bandWidth = calcBandWidth(axis, {
  175. fromStat: {
  176. key: axisStatKey
  177. },
  178. min: 1
  179. }).w;
  180. var remainedWidth = bandWidth;
  181. var autoWidthCount = 0;
  182. var categoryGapOption = '20%';
  183. var gapOption = '30%';
  184. var stacks = {};
  185. eachSeriesOnAxisOnKey(axis, axisStatKey, function (seriesModel) {
  186. var stackId = getSeriesStackId(seriesModel);
  187. if (!stacks[stackId]) {
  188. autoWidthCount++;
  189. }
  190. stacks[stackId] = stacks[stackId] || {
  191. width: 0,
  192. maxWidth: 0
  193. };
  194. var barWidth = parsePercent(seriesModel.get('barWidth'), bandWidth);
  195. var barMaxWidth = parsePercent(seriesModel.get('barMaxWidth'), bandWidth);
  196. var barGapOption = seriesModel.get('barGap');
  197. var barCategoryGapOption = seriesModel.get('barCategoryGap');
  198. if (barWidth && !stacks[stackId].width) {
  199. barWidth = mathMin(remainedWidth, barWidth);
  200. stacks[stackId].width = barWidth;
  201. remainedWidth -= barWidth;
  202. }
  203. barMaxWidth && (stacks[stackId].maxWidth = barMaxWidth);
  204. // For historical design, use the last series declared that.
  205. barGapOption != null && (gapOption = barGapOption);
  206. barCategoryGapOption != null && (categoryGapOption = barCategoryGapOption);
  207. });
  208. var result = {};
  209. var categoryGap = parsePercent(categoryGapOption, bandWidth);
  210. var barGapPercent = parsePercent(gapOption, 1);
  211. var autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  212. autoWidth = mathMax(autoWidth, 0);
  213. // Find if any auto calculated bar exceeded maxBarWidth
  214. each(stacks, function (column, stack) {
  215. var maxWidth = column.maxWidth;
  216. if (maxWidth && maxWidth < autoWidth) {
  217. maxWidth = mathMin(maxWidth, remainedWidth);
  218. if (column.width) {
  219. maxWidth = mathMin(maxWidth, column.width);
  220. }
  221. remainedWidth -= maxWidth;
  222. column.width = maxWidth;
  223. autoWidthCount--;
  224. }
  225. });
  226. // Recalculate width again
  227. autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  228. autoWidth = mathMax(autoWidth, 0);
  229. var widthSum = 0;
  230. var lastColumn;
  231. each(stacks, function (column, idx) {
  232. if (!column.width) {
  233. column.width = autoWidth;
  234. }
  235. lastColumn = column;
  236. widthSum += column.width * (1 + barGapPercent);
  237. });
  238. if (lastColumn) {
  239. widthSum -= lastColumn.width * barGapPercent;
  240. }
  241. var offset = -widthSum / 2;
  242. each(stacks, function (column, stackId) {
  243. result[stackId] = result[stackId] || {
  244. offset: offset,
  245. width: column.width
  246. };
  247. offset += column.width * (1 + barGapPercent);
  248. });
  249. return result;
  250. }
  251. export function registerBarPolarAxisHandlers(registers, seriesType // Currently only 'bar' is supported.
  252. ) {
  253. callOnlyOnce(registers, function () {
  254. var axisStatKey = makeAxisStatKey2(seriesType, COORD_SYS_TYPE_POLAR);
  255. requireAxisStatisticsForBaseBar(registers, axisStatKey, seriesType, COORD_SYS_TYPE_POLAR);
  256. registerAxisContainShapeHandler(axisStatKey, createBandWidthBasedAxisContainShapeHandler(axisStatKey));
  257. });
  258. }