-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathwith-dynamic-slices.js
62 lines (58 loc) · 1.67 KB
/
with-dynamic-slices.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
import React from 'react';
import {
Text,
View,
Dimensions
} from 'react-native';
import { PieChart } from 'react-native-svg-charts'
class PieChartWithDynamicSlices extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
selectedSlice: {
label: '',
value: 0
},
labelWidth: 0
}
}
render() {
const { labelWidth, selectedSlice } = this.state;
const { label, value } = selectedSlice;
const keys = ['google', 'facebook', 'linkedin', 'youtube', 'Twitter'];
const values = [15, 25, 35, 45, 55];
const colors = ['#600080', '#9900cc', '#c61aff', '#d966ff', '#ecb3ff']
const data = keys.map((key, index) => {
return {
key,
value: values[index],
svg: { fill: colors[index] },
arc: { outerRadius: (70 + values[index]) + '%', padAngle: label === key ? 0.1 : 0 },
onPress: () => this.setState({ selectedSlice: { label: key, value: values[index] } })
}
})
const deviceWidth = Dimensions.get('window').width
return (
<View style={{ justifyContent: 'center', flex: 1 }}>
<PieChart
style={{ height: 200 }}
outerRadius={'80%'}
innerRadius={'45%'}
data={data}
/>
<Text
onLayout={({ nativeEvent: { layout: { width } } }) => {
this.setState({ labelWidth: width });
}}
style={{
position: 'absolute',
left: deviceWidth / 2 - labelWidth / 2,
textAlign: 'center'
}}>
{`${label} \n ${value}`}
</Text>
</View>
)
}
}
export default PieChartWithDynamicSlices;