barPolar.js 9.9 KB

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