boxplotLayout.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. var each = zrUtil.each;
  43. export default function boxplotLayout(ecModel) {
  44. var groupResult = groupSeriesByAxis(ecModel);
  45. each(groupResult, function (groupItem) {
  46. var seriesModels = groupItem.seriesModels;
  47. if (!seriesModels.length) {
  48. return;
  49. }
  50. calculateBase(groupItem);
  51. each(seriesModels, function (seriesModel, idx) {
  52. layoutSingleSeries(seriesModel, groupItem.boxOffsetList[idx], groupItem.boxWidthList[idx]);
  53. });
  54. });
  55. }
  56. /**
  57. * Group series by axis.
  58. */
  59. function groupSeriesByAxis(ecModel) {
  60. var result = [];
  61. var axisList = [];
  62. ecModel.eachSeriesByType('boxplot', function (seriesModel) {
  63. var baseAxis = seriesModel.getBaseAxis();
  64. var idx = zrUtil.indexOf(axisList, baseAxis);
  65. if (idx < 0) {
  66. idx = axisList.length;
  67. axisList[idx] = baseAxis;
  68. result[idx] = {
  69. axis: baseAxis,
  70. seriesModels: []
  71. };
  72. }
  73. result[idx].seriesModels.push(seriesModel);
  74. });
  75. return result;
  76. }
  77. /**
  78. * Calculate offset and box width for each series.
  79. */
  80. function calculateBase(groupItem) {
  81. var baseAxis = groupItem.axis;
  82. var seriesModels = groupItem.seriesModels;
  83. var seriesCount = seriesModels.length;
  84. var boxWidthList = groupItem.boxWidthList = [];
  85. var boxOffsetList = groupItem.boxOffsetList = [];
  86. var boundList = [];
  87. var bandWidth;
  88. if (baseAxis.type === 'category') {
  89. bandWidth = baseAxis.getBandWidth();
  90. } else {
  91. var maxDataCount_1 = 0;
  92. each(seriesModels, function (seriesModel) {
  93. maxDataCount_1 = Math.max(maxDataCount_1, seriesModel.getData().count());
  94. });
  95. var extent = baseAxis.getExtent();
  96. bandWidth = Math.abs(extent[1] - extent[0]) / maxDataCount_1;
  97. }
  98. each(seriesModels, function (seriesModel) {
  99. var boxWidthBound = seriesModel.get('boxWidth');
  100. if (!zrUtil.isArray(boxWidthBound)) {
  101. boxWidthBound = [boxWidthBound, boxWidthBound];
  102. }
  103. boundList.push([parsePercent(boxWidthBound[0], bandWidth) || 0, parsePercent(boxWidthBound[1], bandWidth) || 0]);
  104. });
  105. var availableWidth = bandWidth * 0.8 - 2;
  106. var boxGap = availableWidth / seriesCount * 0.3;
  107. var boxWidth = (availableWidth - boxGap * (seriesCount - 1)) / seriesCount;
  108. var base = boxWidth / 2 - availableWidth / 2;
  109. each(seriesModels, function (seriesModel, idx) {
  110. boxOffsetList.push(base);
  111. base += boxGap + boxWidth;
  112. boxWidthList.push(Math.min(Math.max(boxWidth, boundList[idx][0]), boundList[idx][1]));
  113. });
  114. }
  115. /**
  116. * Calculate points location for each series.
  117. */
  118. function layoutSingleSeries(seriesModel, offset, boxWidth) {
  119. var coordSys = seriesModel.coordinateSystem;
  120. var data = seriesModel.getData();
  121. var halfWidth = boxWidth / 2;
  122. var cDimIdx = seriesModel.get('layout') === 'horizontal' ? 0 : 1;
  123. var vDimIdx = 1 - cDimIdx;
  124. var coordDims = ['x', 'y'];
  125. var cDim = data.mapDimension(coordDims[cDimIdx]);
  126. var vDims = data.mapDimensionsAll(coordDims[vDimIdx]);
  127. if (cDim == null || vDims.length < 5) {
  128. return;
  129. }
  130. for (var dataIndex = 0; dataIndex < data.count(); dataIndex++) {
  131. var axisDimVal = data.get(cDim, dataIndex);
  132. var median = getPoint(axisDimVal, vDims[2], dataIndex);
  133. var end1 = getPoint(axisDimVal, vDims[0], dataIndex);
  134. var end2 = getPoint(axisDimVal, vDims[1], dataIndex);
  135. var end4 = getPoint(axisDimVal, vDims[3], dataIndex);
  136. var end5 = getPoint(axisDimVal, vDims[4], dataIndex);
  137. var ends = [];
  138. addBodyEnd(ends, end2, false);
  139. addBodyEnd(ends, end4, true);
  140. ends.push(end1, end2, end5, end4);
  141. layEndLine(ends, end1);
  142. layEndLine(ends, end5);
  143. layEndLine(ends, median);
  144. data.setItemLayout(dataIndex, {
  145. initBaseline: median[vDimIdx],
  146. ends: ends
  147. });
  148. }
  149. function getPoint(axisDimVal, dim, dataIndex) {
  150. var val = data.get(dim, dataIndex);
  151. var p = [];
  152. p[cDimIdx] = axisDimVal;
  153. p[vDimIdx] = val;
  154. var point;
  155. if (isNaN(axisDimVal) || isNaN(val)) {
  156. point = [NaN, NaN];
  157. } else {
  158. point = coordSys.dataToPoint(p);
  159. point[cDimIdx] += offset;
  160. }
  161. return point;
  162. }
  163. function addBodyEnd(ends, point, start) {
  164. var point1 = point.slice();
  165. var point2 = point.slice();
  166. point1[cDimIdx] += halfWidth;
  167. point2[cDimIdx] -= halfWidth;
  168. start ? ends.push(point1, point2) : ends.push(point2, point1);
  169. }
  170. function layEndLine(ends, endCenter) {
  171. var from = endCenter.slice();
  172. var to = endCenter.slice();
  173. from[cDimIdx] -= halfWidth;
  174. to[cDimIdx] += halfWidth;
  175. ends.push(from, to);
  176. }
  177. }