forked from JesperLekland/react-native-svg-charts-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertical-with-labels.js
48 lines (41 loc) · 1.46 KB
/
vertical-with-labels.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
import React from 'react'
import { View } from 'react-native'
import { BarChart, Grid } from 'react-native-svg-charts'
import { Text } from 'react-native-svg'
class BarChartVerticalWithLabels extends React.PureComponent {
render() {
const data = [ 10, 5, 25, 15, 20 ]
const CUT_OFF = 20
const Labels = ({ x, y, bandwidth, data }) => (
data.map((value, index) => (
<Text
key={ index }
x={ x(index) + (bandwidth / 2) }
y={ value < CUT_OFF ? y(value) - 10 : y(value) + 15 }
fontSize={ 14 }
fill={ value >= CUT_OFF ? 'white' : 'black' }
alignmentBaseline={ 'middle' }
textAnchor={ 'middle' }
>
{value}
</Text>
))
)
return (
<View style={{ flexDirection: 'row', height: 200, paddingVertical: 16 }}>
<BarChart
style={{ flex: 1 }}
data={data}
svg={{ fill: 'rgba(134, 65, 244, 0.8)' }}
contentInset={{ top: 10, bottom: 10 }}
spacing={0.2}
gridMin={0}
>
<Grid direction={Grid.Direction.HORIZONTAL}/>
<Labels/>
</BarChart>
</View>
)
}
}
export default BarChartVerticalWithLabels