boxplotLayout.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 { isArray } from 'zrender/lib/core/util.js';
  41. import { mathMax, mathMin, parsePercent } from '../../util/number.js';
  42. import { SERIES_TYPE_BOXPLOT } from './BoxplotSeries.js';
  43. import { countSeriesOnAxisOnKey, eachAxisOnKey, eachSeriesOnAxisOnKey, requireAxisStatistics } from '../../coord/axisStatistics.js';
  44. import { createSimpleOverallStageHandler, makeCallOnlyOnce } from '../../util/model.js';
  45. import { registerAxisContainShapeHandler } from '../../coord/scaleRawExtentInfo.js';
  46. import { calcBandWidth } from '../../coord/axisBand.js';
  47. import { createBandWidthBasedAxisContainShapeHandler, createMetricsNonOrdinalLinearPositiveMinGap, makeAxisStatKey } from '../helper/axisSnippets.js';
  48. var callOnlyOnce = makeCallOnlyOnce();
  49. export var boxplotLayoutStageHandler = createSimpleOverallStageHandler(SERIES_TYPE_BOXPLOT, boxplotLayout);
  50. function boxplotLayout(ecModel) {
  51. var axisStatKey = makeAxisStatKey(SERIES_TYPE_BOXPLOT);
  52. eachAxisOnKey(ecModel, axisStatKey, function (axis) {
  53. var seriesCount = countSeriesOnAxisOnKey(axis, axisStatKey);
  54. if (!seriesCount) {
  55. return;
  56. }
  57. var baseResult = calculateBase(axis, seriesCount);
  58. eachSeriesOnAxisOnKey(axis, axisStatKey, function (seriesModel) {
  59. var seriesIndex = seriesModel.seriesIndex;
  60. layoutSingleSeries(seriesModel, baseResult.boxOffsetList[seriesIndex], baseResult.boxWidthList[seriesIndex]);
  61. });
  62. });
  63. }
  64. /**
  65. * Calculate offset and box width for each series.
  66. */
  67. function calculateBase(baseAxis, seriesCount) {
  68. var boxWidthList = [];
  69. var boxOffsetList = [];
  70. var boundList = [];
  71. var bandWidth = calcBandWidth(baseAxis, {
  72. fromStat: {
  73. key: makeAxisStatKey(SERIES_TYPE_BOXPLOT)
  74. },
  75. min: 1
  76. }).w;
  77. eachSeriesOnAxisOnKey(baseAxis, makeAxisStatKey(SERIES_TYPE_BOXPLOT), function (seriesModel) {
  78. var boxWidthBound = seriesModel.get('boxWidth');
  79. if (!isArray(boxWidthBound)) {
  80. boxWidthBound = [boxWidthBound, boxWidthBound];
  81. }
  82. boundList[seriesModel.seriesIndex] = [parsePercent(boxWidthBound[0], bandWidth) || 0, parsePercent(boxWidthBound[1], bandWidth) || 0];
  83. });
  84. var availableWidth = bandWidth * 0.8 - 2;
  85. var boxGap = availableWidth / seriesCount * 0.3;
  86. var boxWidth = (availableWidth - boxGap * (seriesCount - 1)) / seriesCount;
  87. var base = boxWidth / 2 - availableWidth / 2;
  88. eachSeriesOnAxisOnKey(baseAxis, makeAxisStatKey(SERIES_TYPE_BOXPLOT), function (seriesModel) {
  89. var seriesIndex = seriesModel.seriesIndex;
  90. boxOffsetList[seriesIndex] = base;
  91. base += boxGap + boxWidth;
  92. boxWidthList[seriesIndex] = mathMin(mathMax(boxWidth, boundList[seriesIndex][0]), boundList[seriesIndex][1]);
  93. });
  94. return {
  95. boxOffsetList: boxOffsetList,
  96. boxWidthList: boxWidthList
  97. };
  98. }
  99. /**
  100. * Calculate points location for each series.
  101. */
  102. function layoutSingleSeries(seriesModel, offset, boxWidth) {
  103. var coordSys = seriesModel.coordinateSystem;
  104. var data = seriesModel.getData();
  105. var halfWidth = boxWidth / 2;
  106. var cDimIdx = seriesModel.getWhiskerBoxesLayout() === 'horizontal' ? 0 : 1;
  107. var vDimIdx = 1 - cDimIdx;
  108. var coordDims = ['x', 'y'];
  109. var cDim = data.mapDimension(coordDims[cDimIdx]);
  110. var vDims = data.mapDimensionsAll(coordDims[vDimIdx]);
  111. if (cDim == null || vDims.length < 5) {
  112. return;
  113. }
  114. for (var dataIndex = 0; dataIndex < data.count(); dataIndex++) {
  115. var axisDimVal = data.get(cDim, dataIndex);
  116. var median = getPoint(axisDimVal, vDims[2], dataIndex);
  117. var end1 = getPoint(axisDimVal, vDims[0], dataIndex);
  118. var end2 = getPoint(axisDimVal, vDims[1], dataIndex);
  119. var end4 = getPoint(axisDimVal, vDims[3], dataIndex);
  120. var end5 = getPoint(axisDimVal, vDims[4], dataIndex);
  121. var ends = [];
  122. addBodyEnd(ends, end2, false);
  123. addBodyEnd(ends, end4, true);
  124. ends.push(end1, end2, end5, end4);
  125. layEndLine(ends, end1);
  126. layEndLine(ends, end5);
  127. layEndLine(ends, median);
  128. data.setItemLayout(dataIndex, {
  129. initBaseline: median[vDimIdx],
  130. ends: ends
  131. });
  132. }
  133. function getPoint(axisDimVal, dim, dataIndex) {
  134. var val = data.get(dim, dataIndex);
  135. var p = [];
  136. p[cDimIdx] = axisDimVal;
  137. p[vDimIdx] = val;
  138. var point;
  139. if (isNaN(axisDimVal) || isNaN(val)) {
  140. point = [NaN, NaN];
  141. } else {
  142. point = coordSys.dataToPoint(p);
  143. point[cDimIdx] += offset;
  144. }
  145. return point;
  146. }
  147. function addBodyEnd(ends, point, start) {
  148. var point1 = point.slice();
  149. var point2 = point.slice();
  150. point1[cDimIdx] += halfWidth;
  151. point2[cDimIdx] -= halfWidth;
  152. start ? ends.push(point1, point2) : ends.push(point2, point1);
  153. }
  154. function layEndLine(ends, endCenter) {
  155. var from = endCenter.slice();
  156. var to = endCenter.slice();
  157. from[cDimIdx] -= halfWidth;
  158. to[cDimIdx] += halfWidth;
  159. ends.push(from, to);
  160. }
  161. }
  162. export function registerBoxplotAxisHandlers(registers) {
  163. callOnlyOnce(registers, function () {
  164. var axisStatKey = makeAxisStatKey(SERIES_TYPE_BOXPLOT);
  165. requireAxisStatistics(registers, {
  166. key: axisStatKey,
  167. seriesType: SERIES_TYPE_BOXPLOT,
  168. getMetrics: createMetricsNonOrdinalLinearPositiveMinGap
  169. });
  170. registerAxisContainShapeHandler(axisStatKey, createBandWidthBasedAxisContainShapeHandler(axisStatKey));
  171. });
  172. }