forked from highcharts/highcharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-datagrouping.js
61 lines (54 loc) · 1.94 KB
/
node-datagrouping.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* This is an example of running data grouping in a server environment to avoid
* the overhead of first sending high-resolution data over the network and then
* spending browser CPU to perform the grouping.
*
* The example takes hourly input data and demonstrates how to group it into
* daily average values.
*
* Usage (from highcharts root)
* `node test/node-datagrouping`
*/
/* eslint-env node, es6 */
/* eslint-disable no-console */
const Highcharts = require('../code/highcharts.src.js')();
const dataGrouping = require('../code/modules/datagrouping.src.js')(Highcharts);
// Generate some random, high-resolution data. This function emulates raw data
// with an hourly resolution that we want to group into days.
const [xData, yData] = (function () {
const xs = [];
const ys = [];
let x,
y = 0;
for (
x = Date.UTC(2019, 0, 1);
x < Date.UTC(2019, 11, 31, 23, 59, 59);
x += 36e5 // Increase by one hour
) {
xs.push(x);
ys.push(y++);
}
return [xs, ys];
}());
// First, get the tick distribution where we want the data to be grouped. In
// this example we are dealing with time data, so we use Highcharts.Time to find
// natural time divisions. For non-timebased data, these group positions can be
// on round numbers, like [0, 10, 20, 30] etc.
const time = new Highcharts.Time();
const groupPositions = time.getTimeTicks(
{
// One tick per day
unitRange: 24 * 36e5
},
xData[0], // From
xData[xData.length - 1] // To
);
// Next, group the data by the groupPositions.
const approximation = 'average'; // See https://api.highcharts.com/highstock/series.line.dataGrouping.approximation
const {
groupedXData,
groupedYData
} = dataGrouping.groupData(xData, yData, groupPositions, approximation);
// Optionally, transform it to a Highcharts-compatible two-dimensional array.
const data = groupedXData.map((x, i) => [x, groupedYData[i]]);
console.log(data);