barPolar.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 valueAxisStart = valueAxis.dataToCoord(0);
  78. for (var idx = 0, len = data.count(); idx < len; idx++) {
  79. var value = data.get(valueDim, idx);
  80. var baseValue = data.get(baseDim, idx);
  81. var sign = value >= 0 ? 'p' : 'n';
  82. var baseCoord = valueAxisStart;
  83. // Because of the barMinHeight, we can not use the value in
  84. // stackResultDimension directly.
  85. // Only ordinal axis can be stacked.
  86. if (stacked) {
  87. if (!lastStackCoords[stackId][baseValue]) {
  88. lastStackCoords[stackId][baseValue] = {
  89. p: valueAxisStart,
  90. n: valueAxisStart // Negative stack
  91. };
  92. }
  93. // Should also consider #4243
  94. baseCoord = lastStackCoords[stackId][baseValue][sign];
  95. }
  96. var r0 = void 0;
  97. var r = void 0;
  98. var startAngle = void 0;
  99. var endAngle = void 0;
  100. // radial sector
  101. if (valueAxis.dim === 'radius') {
  102. var radiusSpan = valueAxis.dataToCoord(value) - valueAxisStart;
  103. var angle = baseAxis.dataToCoord(baseValue);
  104. if (Math.abs(radiusSpan) < barMinHeight) {
  105. radiusSpan = (radiusSpan < 0 ? -1 : 1) * barMinHeight;
  106. }
  107. r0 = baseCoord;
  108. r = baseCoord + radiusSpan;
  109. startAngle = angle - columnOffset;
  110. endAngle = startAngle - columnWidth;
  111. stacked && (lastStackCoords[stackId][baseValue][sign] = r);
  112. }
  113. // tangential sector
  114. else {
  115. var angleSpan = valueAxis.dataToCoord(value, clampLayout) - valueAxisStart;
  116. var radius = baseAxis.dataToCoord(baseValue);
  117. if (Math.abs(angleSpan) < barMinAngle) {
  118. angleSpan = (angleSpan < 0 ? -1 : 1) * barMinAngle;
  119. }
  120. r0 = radius + columnOffset;
  121. r = r0 + columnWidth;
  122. startAngle = baseCoord;
  123. endAngle = baseCoord + angleSpan;
  124. // if the previous stack is at the end of the ring,
  125. // add a round to differentiate it from origin
  126. // let extent = angleAxis.getExtent();
  127. // let stackCoord = angle;
  128. // if (stackCoord === extent[0] && value > 0) {
  129. // stackCoord = extent[1];
  130. // }
  131. // else if (stackCoord === extent[1] && value < 0) {
  132. // stackCoord = extent[0];
  133. // }
  134. stacked && (lastStackCoords[stackId][baseValue][sign] = endAngle);
  135. }
  136. data.setItemLayout(idx, {
  137. cx: cx,
  138. cy: cy,
  139. r0: r0,
  140. r: r,
  141. // Consider that positive angle is anti-clockwise,
  142. // while positive radian of sector is clockwise
  143. startAngle: -startAngle * Math.PI / 180,
  144. endAngle: -endAngle * Math.PI / 180,
  145. /**
  146. * Keep the same logic with bar in catesion: use end value to
  147. * control direction. Notice that if clockwise is true (by
  148. * default), the sector will always draw clockwisely, no matter
  149. * whether endAngle is greater or less than startAngle.
  150. */
  151. clockwise: startAngle >= endAngle
  152. });
  153. }
  154. });
  155. }
  156. /**
  157. * Calculate bar width and offset for radial bar charts
  158. */
  159. function calRadialBar(barSeries) {
  160. // Columns info on each category axis. Key is polar name
  161. var columnsMap = {};
  162. zrUtil.each(barSeries, function (seriesModel, idx) {
  163. var data = seriesModel.getData();
  164. var polar = seriesModel.coordinateSystem;
  165. var baseAxis = polar.getBaseAxis();
  166. var axisKey = getAxisKey(polar, baseAxis);
  167. var axisExtent = baseAxis.getExtent();
  168. var bandWidth = baseAxis.type === 'category' ? baseAxis.getBandWidth() : Math.abs(axisExtent[1] - axisExtent[0]) / data.count();
  169. var columnsOnAxis = columnsMap[axisKey] || {
  170. bandWidth: bandWidth,
  171. remainedWidth: bandWidth,
  172. autoWidthCount: 0,
  173. categoryGap: '20%',
  174. gap: '30%',
  175. stacks: {}
  176. };
  177. var stacks = columnsOnAxis.stacks;
  178. columnsMap[axisKey] = columnsOnAxis;
  179. var stackId = getSeriesStackId(seriesModel);
  180. if (!stacks[stackId]) {
  181. columnsOnAxis.autoWidthCount++;
  182. }
  183. stacks[stackId] = stacks[stackId] || {
  184. width: 0,
  185. maxWidth: 0
  186. };
  187. var barWidth = parsePercent(seriesModel.get('barWidth'), bandWidth);
  188. var barMaxWidth = parsePercent(seriesModel.get('barMaxWidth'), bandWidth);
  189. var barGap = seriesModel.get('barGap');
  190. var barCategoryGap = seriesModel.get('barCategoryGap');
  191. if (barWidth && !stacks[stackId].width) {
  192. barWidth = Math.min(columnsOnAxis.remainedWidth, barWidth);
  193. stacks[stackId].width = barWidth;
  194. columnsOnAxis.remainedWidth -= barWidth;
  195. }
  196. barMaxWidth && (stacks[stackId].maxWidth = barMaxWidth);
  197. barGap != null && (columnsOnAxis.gap = barGap);
  198. barCategoryGap != null && (columnsOnAxis.categoryGap = barCategoryGap);
  199. });
  200. var result = {};
  201. zrUtil.each(columnsMap, function (columnsOnAxis, coordSysName) {
  202. result[coordSysName] = {};
  203. var stacks = columnsOnAxis.stacks;
  204. var bandWidth = columnsOnAxis.bandWidth;
  205. var categoryGap = parsePercent(columnsOnAxis.categoryGap, bandWidth);
  206. var barGapPercent = parsePercent(columnsOnAxis.gap, 1);
  207. var remainedWidth = columnsOnAxis.remainedWidth;
  208. var autoWidthCount = columnsOnAxis.autoWidthCount;
  209. var autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  210. autoWidth = Math.max(autoWidth, 0);
  211. // Find if any auto calculated bar exceeded maxBarWidth
  212. zrUtil.each(stacks, function (column, stack) {
  213. var maxWidth = column.maxWidth;
  214. if (maxWidth && maxWidth < autoWidth) {
  215. maxWidth = Math.min(maxWidth, remainedWidth);
  216. if (column.width) {
  217. maxWidth = Math.min(maxWidth, column.width);
  218. }
  219. remainedWidth -= maxWidth;
  220. column.width = maxWidth;
  221. autoWidthCount--;
  222. }
  223. });
  224. // Recalculate width again
  225. autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  226. autoWidth = Math.max(autoWidth, 0);
  227. var widthSum = 0;
  228. var lastColumn;
  229. zrUtil.each(stacks, function (column, idx) {
  230. if (!column.width) {
  231. column.width = autoWidth;
  232. }
  233. lastColumn = column;
  234. widthSum += column.width * (1 + barGapPercent);
  235. });
  236. if (lastColumn) {
  237. widthSum -= lastColumn.width * barGapPercent;
  238. }
  239. var offset = -widthSum / 2;
  240. zrUtil.each(stacks, function (column, stackId) {
  241. result[coordSysName][stackId] = result[coordSysName][stackId] || {
  242. offset: offset,
  243. width: column.width
  244. };
  245. offset += column.width * (1 + barGapPercent);
  246. });
  247. });
  248. return result;
  249. }
  250. export default barLayoutPolar;