prepareBoxplotData.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. function asc(arr) {
  41. arr.sort(function (a, b) {
  42. return a - b;
  43. });
  44. return arr;
  45. }
  46. function quantile(ascArr, p) {
  47. var H = (ascArr.length - 1) * p + 1;
  48. var h = Math.floor(H);
  49. var v = +ascArr[h - 1];
  50. var e = H - h;
  51. return e ? v + e * (ascArr[h] - v) : v;
  52. }
  53. /**
  54. * See:
  55. * <https://en.wikipedia.org/wiki/Box_plot#cite_note-frigge_hoaglin_iglewicz-2>
  56. * <http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/boxplot.stats.html>
  57. *
  58. * Helper method for preparing data.
  59. *
  60. * @param {Array.<number>} rawData like
  61. * [
  62. * [12,232,443], (raw data set for the first box)
  63. * [3843,5545,1232], (raw data set for the second box)
  64. * ...
  65. * ]
  66. * @param {Object} [opt]
  67. *
  68. * @param {(number|string)} [opt.boundIQR=1.5] Data less than min bound is outlier.
  69. * default 1.5, means Q1 - 1.5 * (Q3 - Q1).
  70. * If 'none'/0 passed, min bound will not be used.
  71. * @param {(number|string)} [opt.layout='horizontal']
  72. * Box plot layout, can be 'horizontal' or 'vertical'
  73. * @return {Object} {
  74. * boxData: Array.<Array.<number>>
  75. * outliers: Array.<Array.<number>>
  76. * axisData: Array.<string>
  77. * }
  78. */
  79. export default function (rawData, opt) {
  80. opt = opt || {};
  81. var boxData = [];
  82. var outliers = [];
  83. var axisData = [];
  84. var boundIQR = opt.boundIQR;
  85. var useExtreme = boundIQR === 'none' || boundIQR === 0;
  86. for (var i = 0; i < rawData.length; i++) {
  87. axisData.push(i + '');
  88. var ascList = asc(rawData[i].slice());
  89. var Q1 = quantile(ascList, 0.25);
  90. var Q2 = quantile(ascList, 0.5);
  91. var Q3 = quantile(ascList, 0.75);
  92. var min = ascList[0];
  93. var max = ascList[ascList.length - 1];
  94. var bound = (boundIQR == null ? 1.5 : boundIQR) * (Q3 - Q1);
  95. var low = useExtreme ? min : Math.max(min, Q1 - bound);
  96. var high = useExtreme ? max : Math.min(max, Q3 + bound);
  97. boxData.push([low, Q1, Q2, Q3, high]);
  98. for (var j = 0; j < ascList.length; j++) {
  99. var dataItem = ascList[j];
  100. if (dataItem < low || dataItem > high) {
  101. var outlier = [i, dataItem];
  102. opt.layout === 'vertical' && outlier.reverse();
  103. outliers.push(outlier);
  104. }
  105. }
  106. }
  107. return {
  108. boxData: boxData,
  109. outliers: outliers,
  110. axisData: axisData
  111. };
  112. }