barPolar.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.dim === 'radius' ? valueAxis.dataToRadius(0) : valueAxis.dataToAngle(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. var sign = value >= 0 ? 'p' : 'n';
  88. var baseCoord = valueAxisStart; // Because of the barMinHeight, we can not use the value in
  89. // stackResultDimension directly.
  90. // Only ordinal axis can be stacked.
  91. if (stacked) {
  92. if (!lastStackCoords[stackId][baseValue]) {
  93. lastStackCoords[stackId][baseValue] = {
  94. p: valueAxisStart,
  95. // Positive stack
  96. n: valueAxisStart // Negative stack
  97. };
  98. } // Should also consider #4243
  99. baseCoord = lastStackCoords[stackId][baseValue][sign];
  100. }
  101. var r0;
  102. var r;
  103. var startAngle;
  104. var endAngle; // radial sector
  105. if (valueAxis.dim === 'radius') {
  106. var radiusSpan = valueAxis.dataToRadius(value) - valueAxisStart;
  107. var angle = baseAxis.dataToAngle(baseValue);
  108. if (Math.abs(radiusSpan) < barMinHeight) {
  109. radiusSpan = (radiusSpan < 0 ? -1 : 1) * barMinHeight;
  110. }
  111. r0 = baseCoord;
  112. r = baseCoord + radiusSpan;
  113. startAngle = angle - columnOffset;
  114. endAngle = startAngle - columnWidth;
  115. stacked && (lastStackCoords[stackId][baseValue][sign] = r);
  116. } // tangential sector
  117. else {
  118. var angleSpan = valueAxis.dataToAngle(value, clampLayout) - valueAxisStart;
  119. var radius = baseAxis.dataToRadius(baseValue);
  120. if (Math.abs(angleSpan) < barMinAngle) {
  121. angleSpan = (angleSpan < 0 ? -1 : 1) * barMinAngle;
  122. }
  123. r0 = radius + columnOffset;
  124. r = r0 + columnWidth;
  125. startAngle = baseCoord;
  126. endAngle = baseCoord + angleSpan; // if the previous stack is at the end of the ring,
  127. // add a round to differentiate it from origin
  128. // var extent = angleAxis.getExtent();
  129. // var 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. }
  149. }, this);
  150. }
  151. /**
  152. * Calculate bar width and offset for radial bar charts
  153. */
  154. function calRadialBar(barSeries, api) {
  155. // Columns info on each category axis. Key is polar name
  156. var columnsMap = {};
  157. zrUtil.each(barSeries, function (seriesModel, idx) {
  158. var data = seriesModel.getData();
  159. var polar = seriesModel.coordinateSystem;
  160. var baseAxis = polar.getBaseAxis();
  161. var axisKey = getAxisKey(polar, baseAxis);
  162. var axisExtent = baseAxis.getExtent();
  163. var bandWidth = baseAxis.type === 'category' ? baseAxis.getBandWidth() : Math.abs(axisExtent[1] - axisExtent[0]) / data.count();
  164. var columnsOnAxis = columnsMap[axisKey] || {
  165. bandWidth: bandWidth,
  166. remainedWidth: bandWidth,
  167. autoWidthCount: 0,
  168. categoryGap: '20%',
  169. gap: '30%',
  170. stacks: {}
  171. };
  172. var stacks = columnsOnAxis.stacks;
  173. columnsMap[axisKey] = columnsOnAxis;
  174. var stackId = getSeriesStackId(seriesModel);
  175. if (!stacks[stackId]) {
  176. columnsOnAxis.autoWidthCount++;
  177. }
  178. stacks[stackId] = stacks[stackId] || {
  179. width: 0,
  180. maxWidth: 0
  181. };
  182. var barWidth = parsePercent(seriesModel.get('barWidth'), bandWidth);
  183. var barMaxWidth = parsePercent(seriesModel.get('barMaxWidth'), bandWidth);
  184. var barGap = seriesModel.get('barGap');
  185. var barCategoryGap = seriesModel.get('barCategoryGap');
  186. if (barWidth && !stacks[stackId].width) {
  187. barWidth = Math.min(columnsOnAxis.remainedWidth, barWidth);
  188. stacks[stackId].width = barWidth;
  189. columnsOnAxis.remainedWidth -= barWidth;
  190. }
  191. barMaxWidth && (stacks[stackId].maxWidth = barMaxWidth);
  192. barGap != null && (columnsOnAxis.gap = barGap);
  193. barCategoryGap != null && (columnsOnAxis.categoryGap = barCategoryGap);
  194. });
  195. var result = {};
  196. zrUtil.each(columnsMap, function (columnsOnAxis, coordSysName) {
  197. result[coordSysName] = {};
  198. var stacks = columnsOnAxis.stacks;
  199. var bandWidth = columnsOnAxis.bandWidth;
  200. var categoryGap = parsePercent(columnsOnAxis.categoryGap, bandWidth);
  201. var barGapPercent = parsePercent(columnsOnAxis.gap, 1);
  202. var remainedWidth = columnsOnAxis.remainedWidth;
  203. var autoWidthCount = columnsOnAxis.autoWidthCount;
  204. var autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  205. autoWidth = Math.max(autoWidth, 0); // Find if any auto calculated bar exceeded maxBarWidth
  206. zrUtil.each(stacks, function (column, stack) {
  207. var maxWidth = column.maxWidth;
  208. if (maxWidth && maxWidth < autoWidth) {
  209. maxWidth = Math.min(maxWidth, remainedWidth);
  210. if (column.width) {
  211. maxWidth = Math.min(maxWidth, column.width);
  212. }
  213. remainedWidth -= maxWidth;
  214. column.width = maxWidth;
  215. autoWidthCount--;
  216. }
  217. }); // Recalculate width again
  218. autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  219. autoWidth = Math.max(autoWidth, 0);
  220. var widthSum = 0;
  221. var lastColumn;
  222. zrUtil.each(stacks, function (column, idx) {
  223. if (!column.width) {
  224. column.width = autoWidth;
  225. }
  226. lastColumn = column;
  227. widthSum += column.width * (1 + barGapPercent);
  228. });
  229. if (lastColumn) {
  230. widthSum -= lastColumn.width * barGapPercent;
  231. }
  232. var offset = -widthSum / 2;
  233. zrUtil.each(stacks, function (column, stackId) {
  234. result[coordSysName][stackId] = result[coordSysName][stackId] || {
  235. offset: offset,
  236. width: column.width
  237. };
  238. offset += column.width * (1 + barGapPercent);
  239. });
  240. });
  241. return result;
  242. }
  243. var _default = barLayoutPolar;
  244. module.exports = _default;