prepareBoxplotData.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 { quantile, asc } from '../../util/number.js';
  41. import { isFunction, isString } from 'zrender/lib/core/util.js';
  42. /**
  43. * See:
  44. * <https://en.wikipedia.org/wiki/Box_plot#cite_note-frigge_hoaglin_iglewicz-2>
  45. * <http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/boxplot.stats.html>
  46. *
  47. * Helper method for preparing data.
  48. *
  49. * @param rawData like
  50. * [
  51. * [12,232,443], (raw data set for the first box)
  52. * [3843,5545,1232], (raw data set for the second box)
  53. * ...
  54. * ]
  55. * @param opt.boundIQR=1.5 Data less than min bound is outlier.
  56. * default 1.5, means Q1 - 1.5 * (Q3 - Q1).
  57. * If 'none'/0 passed, min bound will not be used.
  58. */
  59. export default function prepareBoxplotData(rawData, opt) {
  60. opt = opt || {};
  61. var boxData = [];
  62. var outliers = [];
  63. var boundIQR = opt.boundIQR;
  64. var useExtreme = boundIQR === 'none' || boundIQR === 0;
  65. for (var i = 0; i < rawData.length; i++) {
  66. var ascList = asc(rawData[i].slice());
  67. var Q1 = quantile(ascList, 0.25);
  68. var Q2 = quantile(ascList, 0.5);
  69. var Q3 = quantile(ascList, 0.75);
  70. var min = ascList[0];
  71. var max = ascList[ascList.length - 1];
  72. var bound = (boundIQR == null ? 1.5 : boundIQR) * (Q3 - Q1);
  73. var low = useExtreme ? min : Math.max(min, Q1 - bound);
  74. var high = useExtreme ? max : Math.min(max, Q3 + bound);
  75. var itemNameFormatter = opt.itemNameFormatter;
  76. var itemName = isFunction(itemNameFormatter) ? itemNameFormatter({
  77. value: i
  78. }) : isString(itemNameFormatter) ? itemNameFormatter.replace('{value}', i + '') : i + '';
  79. boxData.push([itemName, low, Q1, Q2, Q3, high]);
  80. for (var j = 0; j < ascList.length; j++) {
  81. var dataItem = ascList[j];
  82. if (dataItem < low || dataItem > high) {
  83. var outlier = [itemName, dataItem];
  84. outliers.push(outlier);
  85. }
  86. }
  87. }
  88. return {
  89. boxData: boxData,
  90. outliers: outliers
  91. };
  92. }