/
stacked-area-chart.js
193 lines (173 loc) 路 6.09 KB
/
stacked-area-chart.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import PropTypes from 'prop-types'
import * as array from 'd3-array'
import * as scale from 'd3-scale'
import * as shape from 'd3-shape'
import React, { PureComponent } from 'react'
import { View } from 'react-native'
import { Defs, G, Svg } from 'react-native-svg'
import Grid from './grid'
import Path from './animated-path'
class AreaStack extends PureComponent {
static extractDataPoints(data, keys, order = shape.stackOrderNone, offset = shape.stackOffsetNone) {
const series = shape.stack()
.keys(keys)
.order(order)
.offset(offset)
(data)
//double merge arrays to extract just the values
return array.merge(array.merge(series))
}
state = {
height: 0,
width: 0,
}
_onLayout(event) {
const { nativeEvent: { layout: { height, width } } } = event
this.setState({ height, width })
}
render() {
const {
data,
keys,
colors,
animate,
animationDuration,
style,
renderGradient,
curve,
showGrid,
numberOfTicks,
contentInset: {
top = 0,
bottom = 0,
left = 0,
right = 0,
},
gridMin,
gridMax,
gridProps,
renderDecorator,
extras,
renderExtra,
offset,
order,
} = this.props
const { height, width } = this.state
if (data.length === 0) {
return <View style={ style }/>
}
const series = shape.stack()
.keys(keys)
.order(order)
.offset(offset)
(data)
//double merge arrays to extract just the values
const values = array.merge(array.merge(series))
const extent = array.extent([ ...values, gridMin, gridMax ])
const ticks = array.ticks(extent[ 0 ], extent[ 1 ], numberOfTicks)
//invert range to support svg coordinate system
const y = scale.scaleLinear()
.domain([ extent[ 0 ], extent[ 1 ] ])
.range([ height - bottom, top ])
const x = scale.scaleLinear()
.domain([ 0, keys.length - 1 ])
.range([ left, width - right ])
const areas = series.map((serie, index) => {
const path = shape.area()
.x((d, index) => x(index))
.y0(d => y(d[ 0 ]))
.y1(d => y(d[ 1 ]))
.curve(curve)
(keys.map((_, index) => serie[ index ]))
return {
path,
key: keys[ index ],
color: colors[ index ],
}
})
return (
<View style={ style }>
<View
style={ { flex: 1 } }
onLayout={ event => this._onLayout(event) }
>
<Svg style={ { flex: 1 } }>
{ showGrid &&
<Grid
ticks={ ticks }
y={ y }
gridProps={ gridProps }
/>
}
{ areas.map((area, index) => (
<G key={ area.key }>
<Defs>
{ renderGradient && renderGradient({
id: `gradient-${area.key}`,
width,
height,
x,
y,
index,
key: area.key,
color: area.color,
}) }
</Defs>
<Path
animate={ animate }
animationDuration={ animationDuration }
d={ area.path }
fill={ renderGradient ? `url(#gradient-${area.key})` : area.color }
/>
</G>
)
) }
{ series.map((serie) => {
return keys.map((key, index) => {
return renderDecorator({ x, y, index, value: serie[ index ][ 1 ] })
})
}) }
{ extras.map((item, index) => renderExtra({ item, x, y, index, width, height })) }
</Svg>
</View>
</View>
)
}
}
AreaStack.propTypes = {
data: PropTypes.arrayOf(PropTypes.object).isRequired,
keys: PropTypes.arrayOf(PropTypes.string).isRequired,
colors: PropTypes.arrayOf(PropTypes.string).isRequired,
offset: PropTypes.func,
order: PropTypes.func,
renderGradient: PropTypes.func,
style: PropTypes.any,
animate: PropTypes.bool,
animationDuration: PropTypes.number,
contentInset: PropTypes.shape({
top: PropTypes.number,
left: PropTypes.number,
right: PropTypes.number,
bottom: PropTypes.number,
}),
numberOfTicks: PropTypes.number,
showGrid: PropTypes.bool,
extras: PropTypes.array,
renderDecorator: PropTypes.func,
renderExtra: PropTypes.func,
}
AreaStack.defaultProps = {
curve: shape.curveCardinal,
offset: shape.stackOffsetNone,
order: shape.stackOrderNone,
strokeWidth: 2,
contentInset: {},
numberOfTicks: 10,
showGrid: true,
extras: [],
renderDecorator: () => {
},
renderExtra: () => {
},
}
export default AreaStack