forked from JesperLekland/react-native-svg-charts-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
with-labels.js
59 lines (50 loc) · 1.77 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
import React from 'react'
import { PieChart } from 'react-native-svg-charts'
import { Circle, G, Line } from 'react-native-svg'
class PieChartWithLabelExample extends React.PureComponent {
render() {
const data = [ 50, 10, 40, 95, -4, -24, 85, 91 ]
const randomColor = () => ('#' + (Math.random() * 0xFFFFFF << 0).toString(16) + '000000').slice(0, 7)
const pieData = data
.filter(value => value > 0)
.map((value, index) => ({
value,
svg: { fill: randomColor() },
key: `pie-${index}`,
}))
const Labels = ({ slices }) => {
return slices.map((slice, index) => {
const { labelCentroid, pieCentroid, data } = slice;
return (
<G key={ index }>
<Line
x1={ labelCentroid[ 0 ] }
y1={ labelCentroid[ 1 ] }
x2={ pieCentroid[ 0 ] }
y2={ pieCentroid[ 1 ] }
stroke={ data.svg.fill }
/>
<Circle
cx={ labelCentroid[ 0 ] }
cy={ labelCentroid[ 1 ] }
r={ 15 }
fill={ data.svg.fill }
/>
</G>
)
})
}
return (
<PieChart
style={ { height: 200 } }
data={ pieData }
innerRadius={ 20 }
outerRadius={ 55 }
labelRadius={ 80 }
>
<Labels/>
</PieChart>
)
}
}
export default PieChartWithLabelExample