-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
compute.js
68 lines (64 loc) · 1.81 KB
/
compute.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
62
63
64
65
66
67
68
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { linearScale, pointScale, timeScale } from '@nivo/scales'
export const computeScale = (scaleSpec, dimensions, data) => {
if (scaleSpec.type === 'linear') {
return linearScale(scaleSpec, dimensions, data)
} else if (scaleSpec.type === 'point') {
return pointScale(scaleSpec, dimensions, data)
} else if (scaleSpec.type === 'time') {
return timeScale(scaleSpec, dimensions, data)
}
}
export const computeScales = ({ data, width, height, xScale, yScale }) => ({
xScale: computeScale(
{
...xScale,
axis: 'x',
},
{
width,
height,
},
data
),
yScale: computeScale(
{
...yScale,
axis: 'y',
},
{
width,
height,
},
data
),
})
export const generateLines = (enhancedData, xScale, yScale, getColor) => {
if (yScale.stacked === true) {
return enhancedData.y.stack.series.map(serie => ({
...serie,
color: getColor(serie),
data: serie.data.map(datum => ({
x: datum.x !== null ? xScale(datum.x) : null,
y: datum.y !== null ? yScale(datum.y) : null,
data: datum,
})),
}))
}
return enhancedData.series.map(serie => ({
...serie,
color: getColor(serie),
data: serie.data.map(datum => ({
x: datum.x !== null ? xScale(datum.x) : null,
y: datum.y !== null ? yScale(datum.y) : null,
data: datum,
})),
}))
}