classify-series
Classification algorithms for number-arrays.
Description
This package provides pure javascript functions for classification of Number-arrays.
There are 4 different algorithms to choose from. See differences in docs. All algorithms take a number-array and the number of classes as parameters. The output is a bounds-array of length nbClass + 1. This bounds-array includes the minimum and maximum value of the series as well as the breakpoints for classification.
The package also exposes two helper functions to process bounds-arrays: classIdx(bounds, val)
will take bounds and a concrete value and return the classification index. An index that ranges from 0 to nbClass - 1. You can use this for example to classify by a color-array. getRanges(bound)
returns an array that represent the classes as strings. It can take a custom separator (default = ' - ') and a precision for rounding the values.
Example (two classes):
const serie = 1 2 3 4const nbClass = 2 const bounds = // [1, 2.5, 4]const ranges = // ['1 - 2.5', '2.5 - 4'] // 0 // 0 // 0 // 1 // -1
Example (classification by color).
// classification by colorconst colors = '#fff' '#000'colors // '#fff'colors // undefinedcolors || '#888' // '#888' be careful for for falsible values in colors for let i = 0; i < rangeslength; i++ console // Class 1: 1 - 2.5 | #fff // Class 2: 2.5 - 4 | #000
If you have falsible values in your classes-array ("null", "0", ...) and want to classify out of bound values this package provides a helper function: getClass(bounds, val, classes, oob)
. It safely classifies values out of the series range as the provided oob-value.
// '#fff' // '#888' // it's basically this:typeof colors !== 'undefined' ? colors : '#888' // '#888'
docs
todo...
Inspiration
Thanks to simogeo/geostats for the algorithm implementations. I altered them to use newer javascript syntax and functionality. Also this package provides each algorithm as a pure function. These changes result in a smaller build and make tree-shaking possible.
Known Issues
- Jenks is behaving strange when number of classes is smaller or equal to series.length.