(
rawData: number[][],
opt: PrepareBoxplotDataOpt
)
| 45 | * If 'none'/0 passed, min bound will not be used. |
| 46 | */ |
| 47 | export default function prepareBoxplotData( |
| 48 | rawData: number[][], |
| 49 | opt: PrepareBoxplotDataOpt |
| 50 | ): { |
| 51 | boxData: (number | string)[][]; |
| 52 | outliers: (number | string)[][]; |
| 53 | } { |
| 54 | opt = opt || {}; |
| 55 | const boxData = []; |
| 56 | const outliers = []; |
| 57 | const boundIQR = opt.boundIQR; |
| 58 | const useExtreme = boundIQR === 'none' || boundIQR === 0; |
| 59 | |
| 60 | for (let i = 0; i < rawData.length; i++) { |
| 61 | const ascList = asc(rawData[i].slice()); |
| 62 | |
| 63 | const Q1 = quantile(ascList, 0.25); |
| 64 | const Q2 = quantile(ascList, 0.5); |
| 65 | const Q3 = quantile(ascList, 0.75); |
| 66 | const min = ascList[0]; |
| 67 | const max = ascList[ascList.length - 1]; |
| 68 | |
| 69 | const bound = (boundIQR == null ? 1.5 : boundIQR as number) * (Q3 - Q1); |
| 70 | |
| 71 | const low = useExtreme |
| 72 | ? min |
| 73 | : Math.max(min, Q1 - bound); |
| 74 | const high = useExtreme |
| 75 | ? max |
| 76 | : Math.min(max, Q3 + bound); |
| 77 | |
| 78 | const itemNameFormatter = opt.itemNameFormatter; |
| 79 | const itemName = isFunction(itemNameFormatter) |
| 80 | ? itemNameFormatter({ value: i }) |
| 81 | : isString(itemNameFormatter) |
| 82 | ? itemNameFormatter.replace('{value}', i + '') |
| 83 | : i + ''; |
| 84 | |
| 85 | boxData.push([itemName, low, Q1, Q2, Q3, high]); |
| 86 | |
| 87 | for (let j = 0; j < ascList.length; j++) { |
| 88 | const dataItem = ascList[j]; |
| 89 | if (dataItem < low || dataItem > high) { |
| 90 | const outlier = [itemName, dataItem]; |
| 91 | outliers.push(outlier); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return { |
| 96 | boxData: boxData, |
| 97 | outliers: outliers |
| 98 | }; |
| 99 | } |
no test coverage detected
searching dependent graphs…