|
| 1 | +/** |
| 2 | +* Copyright 2012-2016, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +var isNumeric = require('fast-isnumeric'); |
| 12 | + |
| 13 | +var Plotly = require('../../plotly'); |
| 14 | +var Lib = require('../../lib'); |
| 15 | + |
| 16 | +module.exports = function calc(gd, trace) { |
| 17 | + // outlier definition based on http://www.physics.csbsju.edu/stats/box2.html |
| 18 | + var xa = Plotly.Axes.getFromId(gd, trace.xaxis||'x'), |
| 19 | + ya = Plotly.Axes.getFromId(gd, trace.yaxis||'y'), |
| 20 | + orientation = trace.orientation, |
| 21 | + cd = [], |
| 22 | + valAxis, valLetter, val, valBinned, |
| 23 | + posAxis, posLetter, pos, posDistinct, dPos; |
| 24 | + |
| 25 | + // Set value (val) and position (pos) keys via orientation |
| 26 | + if (orientation==='h') { |
| 27 | + valAxis = xa; |
| 28 | + valLetter = 'x'; |
| 29 | + posAxis = ya; |
| 30 | + posLetter = 'y'; |
| 31 | + } else { |
| 32 | + valAxis = ya; |
| 33 | + valLetter = 'y'; |
| 34 | + posAxis = xa; |
| 35 | + posLetter = 'x'; |
| 36 | + } |
| 37 | + |
| 38 | + val = valAxis.makeCalcdata(trace, valLetter); // get val |
| 39 | + |
| 40 | + // size autorange based on all source points |
| 41 | + // position happens afterward when we know all the pos |
| 42 | + Plotly.Axes.expand(valAxis, val, {padded: true}); |
| 43 | + |
| 44 | + // In vertical (horizontal) box plots: |
| 45 | + // if no x (y) data, use x0 (y0), or name |
| 46 | + // so if you want one box |
| 47 | + // per trace, set x0 (y0) to the x (y) value or category for this trace |
| 48 | + // (or set x (y) to a constant array matching y (x)) |
| 49 | + function getPos (gd, trace, posLetter, posAxis, val) { |
| 50 | + var pos0; |
| 51 | + if (posLetter in trace) pos = posAxis.makeCalcdata(trace, posLetter); |
| 52 | + else { |
| 53 | + if (posLetter+'0' in trace) pos0 = trace[posLetter+'0']; |
| 54 | + else if ('name' in trace && ( |
| 55 | + posAxis.type==='category' || |
| 56 | + (isNumeric(trace.name) && |
| 57 | + ['linear','log'].indexOf(posAxis.type)!==-1) || |
| 58 | + (Lib.isDateTime(trace.name) && |
| 59 | + posAxis.type==='date') |
| 60 | + )) { |
| 61 | + pos0 = trace.name; |
| 62 | + } |
| 63 | + else pos0 = gd.numboxes; |
| 64 | + pos0 = posAxis.d2c(pos0); |
| 65 | + pos = val.map(function(){ return pos0; }); |
| 66 | + } |
| 67 | + return pos; |
| 68 | + } |
| 69 | + |
| 70 | + pos = getPos(gd, trace, posLetter, posAxis, val); |
| 71 | + |
| 72 | + // get distinct positions and min difference |
| 73 | + var dv = Lib.distinctVals(pos); |
| 74 | + posDistinct = dv.vals; |
| 75 | + dPos = dv.minDiff/2; |
| 76 | + |
| 77 | + function binVal (cd, val, pos, posDistinct, dPos) { |
| 78 | + var posDistinctLength = posDistinct.length, |
| 79 | + valLength = val.length, |
| 80 | + valBinned = [], |
| 81 | + bins = [], |
| 82 | + i, p, n, v; |
| 83 | + |
| 84 | + // store distinct pos in cd, find bins, init. valBinned |
| 85 | + for (i = 0; i < posDistinctLength; ++i) { |
| 86 | + p = posDistinct[i]; |
| 87 | + cd[i] = {pos: p}; |
| 88 | + bins[i] = p - dPos; |
| 89 | + valBinned[i] = []; |
| 90 | + } |
| 91 | + bins.push(posDistinct[posDistinctLength-1] + dPos); |
| 92 | + |
| 93 | + // bin the values |
| 94 | + for (i = 0; i < valLength; ++i) { |
| 95 | + v = val[i]; |
| 96 | + if(!isNumeric(v)) continue; |
| 97 | + n = Lib.findBin(pos[i], bins); |
| 98 | + if(n>=0 && n<valLength) valBinned[n].push(v); |
| 99 | + } |
| 100 | + |
| 101 | + return valBinned; |
| 102 | + } |
| 103 | + |
| 104 | + valBinned = binVal(cd, val, pos, posDistinct, dPos); |
| 105 | + |
| 106 | + // sort the bins and calculate the stats |
| 107 | + function calculateStats (cd, valBinned) { |
| 108 | + var v, l, cdi, i; |
| 109 | + |
| 110 | + for (i = 0; i < valBinned.length; ++i) { |
| 111 | + v = valBinned[i].sort(Lib.sorterAsc); |
| 112 | + l = v.length; |
| 113 | + cdi = cd[i]; |
| 114 | + |
| 115 | + cdi.val = v; // put all values into calcdata |
| 116 | + cdi.min = v[0]; |
| 117 | + cdi.max = v[l-1]; |
| 118 | + cdi.mean = Lib.mean(v,l); |
| 119 | + cdi.sd = Lib.stdev(v,l,cdi.mean); |
| 120 | + cdi.q1 = Lib.interp(v, 0.25); // first quartile |
| 121 | + cdi.med = Lib.interp(v, 0.5); // median |
| 122 | + cdi.q3 = Lib.interp(v, 0.75); // third quartile |
| 123 | + // lower and upper fences - last point inside |
| 124 | + // 1.5 interquartile ranges from quartiles |
| 125 | + cdi.lf = Math.min(cdi.q1, v[ |
| 126 | + Math.min(Lib.findBin(2.5*cdi.q1-1.5*cdi.q3,v,true)+1, l-1)]); |
| 127 | + cdi.uf = Math.max(cdi.q3,v[ |
| 128 | + Math.max(Lib.findBin(2.5*cdi.q3-1.5*cdi.q1,v), 0)]); |
| 129 | + // lower and upper outliers - 3 IQR out (don't clip to max/min, |
| 130 | + // this is only for discriminating suspected & far outliers) |
| 131 | + cdi.lo = 4*cdi.q1-3*cdi.q3; |
| 132 | + cdi.uo = 4*cdi.q3-3*cdi.q1; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + calculateStats(cd, valBinned); |
| 137 | + |
| 138 | + // remove empty bins |
| 139 | + cd = cd.filter(function(cdi){ return cdi.val && cdi.val.length; }); |
| 140 | + if(!cd.length) return [{t: {emptybox: true}}]; |
| 141 | + |
| 142 | + // add numboxes and dPos to cd |
| 143 | + cd[0].t = {boxnum: gd.numboxes, dPos: dPos}; |
| 144 | + gd.numboxes++; |
| 145 | + return cd; |
| 146 | +}; |
0 commit comments