forked from JesperLekland/react-native-svg-charts-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
with-centered-labels.js
73 lines (66 loc) · 1.89 KB
/
with-centered-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
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
import React from 'react'
import { PieChart } from 'react-native-svg-charts'
import { Text } from 'react-native-svg'
class PieChartWithCenteredLabels extends React.PureComponent {
render() {
const data = [
{
key: 1,
amount: 50,
svg: { fill: '#600080' },
},
{
key: 2,
amount: 50,
svg: { fill: '#9900cc' }
},
{
key: 3,
amount: 40,
svg: { fill: '#c61aff' }
},
{
key: 4,
amount: 95,
svg: { fill: '#d966ff' }
},
{
key: 5,
amount: 35,
svg: { fill: '#ecb3ff' }
}
]
const Labels = ({ slices, height, width }) => {
return slices.map((slice, index) => {
const { labelCentroid, pieCentroid, data } = slice;
return (
<Text
key={index}
x={pieCentroid[ 0 ]}
y={pieCentroid[ 1 ]}
fill={'white'}
textAnchor={'middle'}
alignmentBaseline={'middle'}
fontSize={24}
stroke={'black'}
strokeWidth={0.2}
>
{data.amount}
</Text>
)
})
}
return (
<PieChart
style={{ height: 200 }}
valueAccessor={({ item }) => item.amount}
data={data}
spacing={0}
outerRadius={'95%'}
>
<Labels/>
</PieChart>
)
}
}
export default PieChartWithCenteredLabels