prepareBoxplotData.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 numberUtil = require("../../lib/util/number");
  20. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. /**
  39. * See:
  40. * <https://en.wikipedia.org/wiki/Box_plot#cite_note-frigge_hoaglin_iglewicz-2>
  41. * <http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/boxplot.stats.html>
  42. *
  43. * Helper method for preparing data.
  44. *
  45. * @param {Array.<number>} rawData like
  46. * [
  47. * [12,232,443], (raw data set for the first box)
  48. * [3843,5545,1232], (raw datat set for the second box)
  49. * ...
  50. * ]
  51. * @param {Object} [opt]
  52. *
  53. * @param {(number|string)} [opt.boundIQR=1.5] Data less than min bound is outlier.
  54. * default 1.5, means Q1 - 1.5 * (Q3 - Q1).
  55. * If 'none'/0 passed, min bound will not be used.
  56. * @param {(number|string)} [opt.layout='horizontal']
  57. * Box plot layout, can be 'horizontal' or 'vertical'
  58. * @return {Object} {
  59. * boxData: Array.<Array.<number>>
  60. * outliers: Array.<Array.<number>>
  61. * axisData: Array.<string>
  62. * }
  63. */
  64. function _default(rawData, opt) {
  65. opt = opt || [];
  66. var boxData = [];
  67. var outliers = [];
  68. var axisData = [];
  69. var boundIQR = opt.boundIQR;
  70. var useExtreme = boundIQR === 'none' || boundIQR === 0;
  71. for (var i = 0; i < rawData.length; i++) {
  72. axisData.push(i + '');
  73. var ascList = numberUtil.asc(rawData[i].slice());
  74. var Q1 = numberUtil.quantile(ascList, 0.25);
  75. var Q2 = numberUtil.quantile(ascList, 0.5);
  76. var Q3 = numberUtil.quantile(ascList, 0.75);
  77. var min = ascList[0];
  78. var max = ascList[ascList.length - 1];
  79. var bound = (boundIQR == null ? 1.5 : boundIQR) * (Q3 - Q1);
  80. var low = useExtreme ? min : Math.max(min, Q1 - bound);
  81. var high = useExtreme ? max : Math.min(max, Q3 + bound);
  82. boxData.push([low, Q1, Q2, Q3, high]);
  83. for (var j = 0; j < ascList.length; j++) {
  84. var dataItem = ascList[j];
  85. if (dataItem < low || dataItem > high) {
  86. var outlier = [i, dataItem];
  87. opt.layout === 'vertical' && outlier.reverse();
  88. outliers.push(outlier);
  89. }
  90. }
  91. }
  92. return {
  93. boxData: boxData,
  94. outliers: outliers,
  95. axisData: axisData
  96. };
  97. }
  98. module.exports = _default;