barPolar.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 * as zrUtil from 'zrender/lib/core/util.js';
  41. import { parsePercent } from '../util/number.js';
  42. import { isDimensionStacked } from '../data/helper/dataStackHelper.js';
  43. function getSeriesStackId(seriesModel) {
  44. return seriesModel.get('stack') || '__ec_stack_' + seriesModel.seriesIndex;
  45. }
  46. function getAxisKey(polar, axis) {
  47. return axis.dim + polar.model.componentIndex;
  48. }
  49. function barLayoutPolar(seriesType, ecModel, api) {
  50. var lastStackCoords = {};
  51. var barWidthAndOffset = calRadialBar(zrUtil.filter(ecModel.getSeriesByType(seriesType), function (seriesModel) {
  52. return !ecModel.isSeriesFiltered(seriesModel) && seriesModel.coordinateSystem && seriesModel.coordinateSystem.type === 'polar';
  53. }));
  54. ecModel.eachSeriesByType(seriesType, function (seriesModel) {
  55. // Check series coordinate, do layout for polar only
  56. if (seriesModel.coordinateSystem.type !== 'polar') {
  57. return;
  58. }
  59. var data = seriesModel.getData();
  60. var polar = seriesModel.coordinateSystem;
  61. var baseAxis = polar.getBaseAxis();
  62. var axisKey = getAxisKey(polar, baseAxis);
  63. var stackId = getSeriesStackId(seriesModel);
  64. var columnLayoutInfo = barWidthAndOffset[axisKey][stackId];
  65. var columnOffset = columnLayoutInfo.offset;
  66. var columnWidth = columnLayoutInfo.width;
  67. var valueAxis = polar.getOtherAxis(baseAxis);
  68. var cx = seriesModel.coordinateSystem.cx;
  69. var cy = seriesModel.coordinateSystem.cy;
  70. var barMinHeight = seriesModel.get('barMinHeight') || 0;
  71. var barMinAngle = seriesModel.get('barMinAngle') || 0;
  72. lastStackCoords[stackId] = lastStackCoords[stackId] || [];
  73. var valueDim = data.mapDimension(valueAxis.dim);
  74. var baseDim = data.mapDimension(baseAxis.dim);
  75. var stacked = isDimensionStacked(data, valueDim /* , baseDim */);
  76. var clampLayout = baseAxis.dim !== 'radius' || !seriesModel.get('roundCap', true);
  77. var valueAxisModel = valueAxis.model;
  78. var startValue = valueAxisModel.get('startValue');
  79. var valueAxisStart = valueAxis.dataToCoord(startValue || 0);
  80. for (var idx = 0, len = data.count(); idx < len; idx++) {
  81. var value = data.get(valueDim, idx);
  82. var baseValue = data.get(baseDim, idx);
  83. var sign = value >= 0 ? 'p' : 'n';
  84. var baseCoord = valueAxisStart;
  85. // Because of the barMinHeight, we can not use the value in
  86. // stackResultDimension directly.
  87. // Only ordinal axis can be stacked.
  88. if (stacked) {
  89. if (!lastStackCoords[stackId][baseValue]) {
  90. lastStackCoords[stackId][baseValue] = {
  91. p: valueAxisStart,
  92. n: valueAxisStart // Negative stack
  93. };
  94. }
  95. // Should also consider #4243
  96. baseCoord = lastStackCoords[stackId][baseValue][sign];
  97. }
  98. var r0 = void 0;
  99. var r = void 0;
  100. var startAngle = void 0;
  101. var endAngle = void 0;
  102. // radial sector
  103. if (valueAxis.dim === 'radius') {
  104. var radiusSpan = valueAxis.dataToCoord(value) - valueAxisStart;
  105. var angle = baseAxis.dataToCoord(baseValue);
  106. if (Math.abs(radiusSpan) < barMinHeight) {
  107. radiusSpan = (radiusSpan < 0 ? -1 : 1) * barMinHeight;
  108. }
  109. r0 = baseCoord;
  110. r = baseCoord + radiusSpan;
  111. startAngle = angle - columnOffset;
  112. endAngle = startAngle - columnWidth;
  113. stacked && (lastStackCoords[stackId][baseValue][sign] = r);
  114. }
  115. // tangential sector
  116. else {
  117. var angleSpan = valueAxis.dataToCoord(value, clampLayout) - valueAxisStart;
  118. var radius = baseAxis.dataToCoord(baseValue);
  119. if (Math.abs(angleSpan) < barMinAngle) {
  120. angleSpan = (angleSpan < 0 ? -1 : 1) * barMinAngle;
  121. }
  122. r0 = radius + columnOffset;
  123. r = r0 + columnWidth;
  124. startAngle = baseCoord;
  125. endAngle = baseCoord + angleSpan;
  126. // if the previous stack is at the end of the ring,
  127. // add a round to differentiate it from origin
  128. // let extent = angleAxis.getExtent();
  129. // let stackCoord = angle;
  130. // if (stackCoord === extent[0] && value > 0) {
  131. // stackCoord = extent[1];
  132. // }
  133. // else if (stackCoord === extent[1] && value < 0) {
  134. // stackCoord = extent[0];
  135. // }
  136. stacked && (lastStackCoords[stackId][baseValue][sign] = endAngle);
  137. }
  138. data.setItemLayout(idx, {
  139. cx: cx,
  140. cy: cy,
  141. r0: r0,
  142. r: r,
  143. // Consider that positive angle is anti-clockwise,
  144. // while positive radian of sector is clockwise
  145. startAngle: -startAngle * Math.PI / 180,
  146. endAngle: -endAngle * Math.PI / 180,
  147. /**
  148. * Keep the same logic with bar in catesion: use end value to
  149. * control direction. Notice that if clockwise is true (by
  150. * default), the sector will always draw clockwisely, no matter
  151. * whether endAngle is greater or less than startAngle.
  152. */
  153. clockwise: startAngle >= endAngle
  154. });
  155. }
  156. });
  157. }
  158. /**
  159. * Calculate bar width and offset for radial bar charts
  160. */
  161. function calRadialBar(barSeries) {
  162. // Columns info on each category axis. Key is polar name
  163. var columnsMap = {};
  164. zrUtil.each(barSeries, function (seriesModel, idx) {
  165. var data = seriesModel.getData();
  166. var polar = seriesModel.coordinateSystem;
  167. var baseAxis = polar.getBaseAxis();
  168. var axisKey = getAxisKey(polar, baseAxis);
  169. var axisExtent = baseAxis.getExtent();
  170. var bandWidth = baseAxis.type === 'category' ? baseAxis.getBandWidth() : Math.abs(axisExtent[1] - axisExtent[0]) / data.count();
  171. var columnsOnAxis = columnsMap[axisKey] || {
  172. bandWidth: bandWidth,
  173. remainedWidth: bandWidth,
  174. autoWidthCount: 0,
  175. categoryGap: '20%',
  176. gap: '30%',
  177. stacks: {}
  178. };
  179. var stacks = columnsOnAxis.stacks;
  180. columnsMap[axisKey] = columnsOnAxis;
  181. var stackId = getSeriesStackId(seriesModel);
  182. if (!stacks[stackId]) {
  183. columnsOnAxis.autoWidthCount++;
  184. }
  185. stacks[stackId] = stacks[stackId] || {
  186. width: 0,
  187. maxWidth: 0
  188. };
  189. var barWidth = parsePercent(seriesModel.get('barWidth'), bandWidth);
  190. var barMaxWidth = parsePercent(seriesModel.get('barMaxWidth'), bandWidth);
  191. var barGap = seriesModel.get('barGap');
  192. var barCategoryGap = seriesModel.get('barCategoryGap');
  193. if (barWidth && !stacks[stackId].width) {
  194. barWidth = Math.min(columnsOnAxis.remainedWidth, barWidth);
  195. stacks[stackId].width = barWidth;
  196. columnsOnAxis.remainedWidth -= barWidth;
  197. }
  198. barMaxWidth && (stacks[stackId].maxWidth = barMaxWidth);
  199. barGap != null && (columnsOnAxis.gap = barGap);
  200. barCategoryGap != null && (columnsOnAxis.categoryGap = barCategoryGap);
  201. });
  202. var result = {};
  203. zrUtil.each(columnsMap, function (columnsOnAxis, coordSysName) {
  204. result[coordSysName] = {};
  205. var stacks = columnsOnAxis.stacks;
  206. var bandWidth = columnsOnAxis.bandWidth;
  207. var categoryGap = parsePercent(columnsOnAxis.categoryGap, bandWidth);
  208. var barGapPercent = parsePercent(columnsOnAxis.gap, 1);
  209. var remainedWidth = columnsOnAxis.remainedWidth;
  210. var autoWidthCount = columnsOnAxis.autoWidthCount;
  211. var autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  212. autoWidth = Math.max(autoWidth, 0);
  213. // Find if any auto calculated bar exceeded maxBarWidth
  214. zrUtil.each(stacks, function (column, stack) {
  215. var maxWidth = column.maxWidth;
  216. if (maxWidth && maxWidth < autoWidth) {
  217. maxWidth = Math.min(maxWidth, remainedWidth);
  218. if (column.width) {
  219. maxWidth = Math.min(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 = Math.max(autoWidth, 0);
  229. var widthSum = 0;
  230. var lastColumn;
  231. zrUtil.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. zrUtil.each(stacks, function (column, stackId) {
  243. result[coordSysName][stackId] = result[coordSysName][stackId] || {
  244. offset: offset,
  245. width: column.width
  246. };
  247. offset += column.width * (1 + barGapPercent);
  248. });
  249. });
  250. return result;
  251. }
  252. export default barLayoutPolar;