boxplotLayout.js 6.1 KB

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