prepareBoxplotData.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. define(function (require) {
  2. var quantile = require('./quantile');
  3. var numberUtil = require('echarts').number;
  4. /**
  5. * Helper method for preparing data.
  6. * @param {Array.<number>} rawData like
  7. * [
  8. * [12,232,443], (raw data set for the first box)
  9. * [3843,5545,1232], (raw datat set for the second box)
  10. * ...
  11. * ]
  12. * @param {Object} [opt]
  13. *
  14. * @param {(number|string)} [opt.boundIQR=1.5] Data less than min bound is outlier.
  15. * default 1.5, means Q1 - 1.5 * (Q3 - Q1).
  16. * If pass 'none', min bound will not be used.
  17. * @param {(number|string)} [opt.layout='horizontal']
  18. * Box plot layout, can be 'horizontal' or 'vertical'
  19. */
  20. return function (rawData, opt) {
  21. opt = opt || [];
  22. var boxData = [];
  23. var outliers = [];
  24. var axisData = [];
  25. var boundIQR = opt.boundIQR;
  26. for (var i = 0; i < rawData.length; i++) {
  27. axisData.push(i + '');
  28. var ascList = numberUtil.asc(rawData[i].slice());
  29. var Q1 = quantile(ascList, 0.25);
  30. var Q2 = quantile(ascList, 0.5);
  31. var Q3 = quantile(ascList, 0.75);
  32. var IQR = Q3 - Q1;
  33. var low = boundIQR === 'none'
  34. ? ascList[0]
  35. : Q1 - (boundIQR == null ? 1.5 : boundIQR) * IQR;
  36. var high = boundIQR === 'none'
  37. ? ascList[ascList.length - 1]
  38. : Q3 + (boundIQR == null ? 1.5 : boundIQR) * IQR;
  39. boxData.push([low, Q1, Q2, Q3, high]);
  40. for (var j = 0; j < ascList.length; j++) {
  41. var dataItem = ascList[j];
  42. if (dataItem < low || dataItem > high) {
  43. var outlier = [i, dataItem];
  44. opt.layout === 'vertical' && outlier.reverse();
  45. outliers.push(outlier);
  46. }
  47. }
  48. }
  49. return {
  50. boxData: boxData,
  51. outliers: outliers,
  52. axisData: axisData
  53. };
  54. };
  55. });