barPolar.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. var zrUtil = require("zrender/lib/core/util");
  20. var _number = require("../util/number");
  21. var parsePercent = _number.parsePercent;
  22. var _dataStackHelper = require("../data/helper/dataStackHelper");
  23. var isDimensionStacked = _dataStackHelper.isDimensionStacked;
  24. /*
  25. * Licensed to the Apache Software Foundation (ASF) under one
  26. * or more contributor license agreements. See the NOTICE file
  27. * distributed with this work for additional information
  28. * regarding copyright ownership. The ASF licenses this file
  29. * to you under the Apache License, Version 2.0 (the
  30. * "License"); you may not use this file except in compliance
  31. * with the License. You may obtain a copy of the License at
  32. *
  33. * http://www.apache.org/licenses/LICENSE-2.0
  34. *
  35. * Unless required by applicable law or agreed to in writing,
  36. * software distributed under the License is distributed on an
  37. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  38. * KIND, either express or implied. See the License for the
  39. * specific language governing permissions and limitations
  40. * under the License.
  41. */
  42. function getSeriesStackId(seriesModel) {
  43. return seriesModel.get('stack') || '__ec_stack_' + seriesModel.seriesIndex;
  44. }
  45. function getAxisKey(polar, axis) {
  46. return axis.dim + polar.model.componentIndex;
  47. }
  48. /**
  49. * @param {string} seriesType
  50. * @param {module:echarts/model/Global} ecModel
  51. * @param {module:echarts/ExtensionAPI} api
  52. */
  53. function barLayoutPolar(seriesType, ecModel, api) {
  54. var lastStackCoords = {};
  55. var barWidthAndOffset = calRadialBar(zrUtil.filter(ecModel.getSeriesByType(seriesType), function (seriesModel) {
  56. return !ecModel.isSeriesFiltered(seriesModel) && seriesModel.coordinateSystem && seriesModel.coordinateSystem.type === 'polar';
  57. }));
  58. ecModel.eachSeriesByType(seriesType, function (seriesModel) {
  59. // Check series coordinate, do layout for polar only
  60. if (seriesModel.coordinateSystem.type !== 'polar') {
  61. return;
  62. }
  63. var data = seriesModel.getData();
  64. var polar = seriesModel.coordinateSystem;
  65. var baseAxis = polar.getBaseAxis();
  66. var axisKey = getAxisKey(polar, baseAxis);
  67. var stackId = getSeriesStackId(seriesModel);
  68. var columnLayoutInfo = barWidthAndOffset[axisKey][stackId];
  69. var columnOffset = columnLayoutInfo.offset;
  70. var columnWidth = columnLayoutInfo.width;
  71. var valueAxis = polar.getOtherAxis(baseAxis);
  72. var cx = seriesModel.coordinateSystem.cx;
  73. var cy = seriesModel.coordinateSystem.cy;
  74. var barMinHeight = seriesModel.get('barMinHeight') || 0;
  75. var barMinAngle = seriesModel.get('barMinAngle') || 0;
  76. lastStackCoords[stackId] = lastStackCoords[stackId] || [];
  77. var valueDim = data.mapDimension(valueAxis.dim);
  78. var baseDim = data.mapDimension(baseAxis.dim);
  79. var stacked = isDimensionStacked(data, valueDim
  80. /*, baseDim*/
  81. );
  82. var clampLayout = baseAxis.dim !== 'radius' || !seriesModel.get('roundCap', true);
  83. var valueAxisStart = valueAxis.getExtent()[0];
  84. for (var idx = 0, len = data.count(); idx < len; idx++) {
  85. var value = data.get(valueDim, idx);
  86. var baseValue = data.get(baseDim, idx);
  87. if (isNaN(value)) {
  88. continue;
  89. }
  90. var sign = value >= 0 ? 'p' : 'n';
  91. var baseCoord = valueAxisStart; // Because of the barMinHeight, we can not use the value in
  92. // stackResultDimension directly.
  93. // Only ordinal axis can be stacked.
  94. if (stacked) {
  95. if (!lastStackCoords[stackId][baseValue]) {
  96. lastStackCoords[stackId][baseValue] = {
  97. p: valueAxisStart,
  98. // Positive stack
  99. n: valueAxisStart // Negative stack
  100. };
  101. } // Should also consider #4243
  102. baseCoord = lastStackCoords[stackId][baseValue][sign];
  103. }
  104. var r0;
  105. var r;
  106. var startAngle;
  107. var endAngle; // radial sector
  108. if (valueAxis.dim === 'radius') {
  109. var radiusSpan = valueAxis.dataToRadius(value) - valueAxisStart;
  110. var angle = baseAxis.dataToAngle(baseValue);
  111. if (Math.abs(radiusSpan) < barMinHeight) {
  112. radiusSpan = (radiusSpan < 0 ? -1 : 1) * barMinHeight;
  113. }
  114. r0 = baseCoord;
  115. r = baseCoord + radiusSpan;
  116. startAngle = angle - columnOffset;
  117. endAngle = startAngle - columnWidth;
  118. stacked && (lastStackCoords[stackId][baseValue][sign] = r);
  119. } // tangential sector
  120. else {
  121. var angleSpan = valueAxis.dataToAngle(value, clampLayout) - valueAxisStart;
  122. var radius = baseAxis.dataToRadius(baseValue);
  123. if (Math.abs(angleSpan) < barMinAngle) {
  124. angleSpan = (angleSpan < 0 ? -1 : 1) * barMinAngle;
  125. }
  126. r0 = radius + columnOffset;
  127. r = r0 + columnWidth;
  128. startAngle = baseCoord;
  129. endAngle = baseCoord + angleSpan; // if the previous stack is at the end of the ring,
  130. // add a round to differentiate it from origin
  131. // var extent = angleAxis.getExtent();
  132. // var stackCoord = angle;
  133. // if (stackCoord === extent[0] && value > 0) {
  134. // stackCoord = extent[1];
  135. // }
  136. // else if (stackCoord === extent[1] && value < 0) {
  137. // stackCoord = extent[0];
  138. // }
  139. stacked && (lastStackCoords[stackId][baseValue][sign] = endAngle);
  140. }
  141. data.setItemLayout(idx, {
  142. cx: cx,
  143. cy: cy,
  144. r0: r0,
  145. r: r,
  146. // Consider that positive angle is anti-clockwise,
  147. // while positive radian of sector is clockwise
  148. startAngle: -startAngle * Math.PI / 180,
  149. endAngle: -endAngle * Math.PI / 180
  150. });
  151. }
  152. }, this);
  153. }
  154. /**
  155. * Calculate bar width and offset for radial bar charts
  156. */
  157. function calRadialBar(barSeries, api) {
  158. // Columns info on each category axis. Key is polar name
  159. var columnsMap = {};
  160. zrUtil.each(barSeries, function (seriesModel, idx) {
  161. var data = seriesModel.getData();
  162. var polar = seriesModel.coordinateSystem;
  163. var baseAxis = polar.getBaseAxis();
  164. var axisKey = getAxisKey(polar, baseAxis);
  165. var axisExtent = baseAxis.getExtent();
  166. var bandWidth = baseAxis.type === 'category' ? baseAxis.getBandWidth() : Math.abs(axisExtent[1] - axisExtent[0]) / data.count();
  167. var columnsOnAxis = columnsMap[axisKey] || {
  168. bandWidth: bandWidth,
  169. remainedWidth: bandWidth,
  170. autoWidthCount: 0,
  171. categoryGap: '20%',
  172. gap: '30%',
  173. stacks: {}
  174. };
  175. var stacks = columnsOnAxis.stacks;
  176. columnsMap[axisKey] = columnsOnAxis;
  177. var stackId = getSeriesStackId(seriesModel);
  178. if (!stacks[stackId]) {
  179. columnsOnAxis.autoWidthCount++;
  180. }
  181. stacks[stackId] = stacks[stackId] || {
  182. width: 0,
  183. maxWidth: 0
  184. };
  185. var barWidth = parsePercent(seriesModel.get('barWidth'), bandWidth);
  186. var barMaxWidth = parsePercent(seriesModel.get('barMaxWidth'), bandWidth);
  187. var barGap = seriesModel.get('barGap');
  188. var barCategoryGap = seriesModel.get('barCategoryGap');
  189. if (barWidth && !stacks[stackId].width) {
  190. barWidth = Math.min(columnsOnAxis.remainedWidth, barWidth);
  191. stacks[stackId].width = barWidth;
  192. columnsOnAxis.remainedWidth -= barWidth;
  193. }
  194. barMaxWidth && (stacks[stackId].maxWidth = barMaxWidth);
  195. barGap != null && (columnsOnAxis.gap = barGap);
  196. barCategoryGap != null && (columnsOnAxis.categoryGap = barCategoryGap);
  197. });
  198. var result = {};
  199. zrUtil.each(columnsMap, function (columnsOnAxis, coordSysName) {
  200. result[coordSysName] = {};
  201. var stacks = columnsOnAxis.stacks;
  202. var bandWidth = columnsOnAxis.bandWidth;
  203. var categoryGap = parsePercent(columnsOnAxis.categoryGap, bandWidth);
  204. var barGapPercent = parsePercent(columnsOnAxis.gap, 1);
  205. var remainedWidth = columnsOnAxis.remainedWidth;
  206. var autoWidthCount = columnsOnAxis.autoWidthCount;
  207. var autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  208. autoWidth = Math.max(autoWidth, 0); // Find if any auto calculated bar exceeded maxBarWidth
  209. zrUtil.each(stacks, function (column, stack) {
  210. var maxWidth = column.maxWidth;
  211. if (maxWidth && maxWidth < autoWidth) {
  212. maxWidth = Math.min(maxWidth, remainedWidth);
  213. if (column.width) {
  214. maxWidth = Math.min(maxWidth, column.width);
  215. }
  216. remainedWidth -= maxWidth;
  217. column.width = maxWidth;
  218. autoWidthCount--;
  219. }
  220. }); // Recalculate width again
  221. autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  222. autoWidth = Math.max(autoWidth, 0);
  223. var widthSum = 0;
  224. var lastColumn;
  225. zrUtil.each(stacks, function (column, idx) {
  226. if (!column.width) {
  227. column.width = autoWidth;
  228. }
  229. lastColumn = column;
  230. widthSum += column.width * (1 + barGapPercent);
  231. });
  232. if (lastColumn) {
  233. widthSum -= lastColumn.width * barGapPercent;
  234. }
  235. var offset = -widthSum / 2;
  236. zrUtil.each(stacks, function (column, stackId) {
  237. result[coordSysName][stackId] = result[coordSysName][stackId] || {
  238. offset: offset,
  239. width: column.width
  240. };
  241. offset += column.width * (1 + barGapPercent);
  242. });
  243. });
  244. return result;
  245. }
  246. var _default = barLayoutPolar;
  247. module.exports = _default;