-
Notifications
You must be signed in to change notification settings - Fork 224
/
jenks.js
40 lines (35 loc) · 1.43 KB
/
jenks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import jenksBreaks from "./jenks_breaks.js";
import jenksMatrices from "./jenks_matrices.js";
/**
* The **[jenks natural breaks optimization](http://en.wikipedia.org/wiki/Jenks_natural_breaks_optimization)**
* is an algorithm commonly used in cartography and visualization to decide
* upon groupings of data values that minimize variance within themselves
* and maximize variation between themselves.
*
* For instance, cartographers often use jenks in order to choose which
* values are assigned to which colors in a [choropleth](https://en.wikipedia.org/wiki/Choropleth_map)
* map.
*
* @param {Array<number>} data input data, as an array of number values
* @param {number} nClasses number of desired classes
* @returns {Array<number>} array of class break positions
* // split data into 3 break points
* jenks([1, 2, 4, 5, 7, 9, 10, 20], 3) // = [1, 7, 20, 20]
*/
function jenks(data, nClasses) {
if (nClasses > data.length) {
return null;
}
// sort data in numerical order, since this is expected
// by the matrices function
data = data.slice().sort(function (a, b) {
return a - b;
});
// get our basic matrices
const matrices = jenksMatrices(data, nClasses);
// we only need lower class limits here
const lowerClassLimits = matrices.lowerClassLimits;
// extract nClasses out of the computed matrices
return jenksBreaks(data, lowerClassLimits, nClasses);
}
export default jenks;