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