barPolar.js 10 KB

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