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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
import React from 'react';
import PropTypes from 'prop-types';
import { createMuiTheme } from '@material-ui/core/styles';
import ThemePallete from 'ba-api/themePalette';
import {
PieChart,
Pie,
Sector
} from 'recharts';
import { data6 } from './sampleData';
const theme = createMuiTheme(ThemePallete.greenTheme);
const color = ({
primary: theme.palette.primary.main,
secondary: theme.palette.secondary.main,
});
const renderActiveShape = props => {
const RADIAN = Math.PI / 180;
const {
cx,
cy,
midAngle,
innerRadius,
outerRadius,
startAngle,
endAngle,
fill,
payload,
percent,
value
} = props;
const sin = Math.sin(-RADIAN * midAngle);
const cos = Math.cos(-RADIAN * midAngle);
const sx = cx + ((outerRadius + 10) * cos);
const sy = cy + ((outerRadius + 10) * sin);
const mx = cx + ((outerRadius + 30) * cos);
const my = cy + ((outerRadius + 30) * sin);
const ex = mx + ((cos >= 0 ? 1 : -1) * 22);
const ey = my;
const textAnchor = cos >= 0 ? 'start' : 'end';
return (
<g>
<text x={cx} y={cy} dy={8} textAnchor="middle" fill={fill}>{payload.name}</text>
<Sector
cx={cx}
cy={cy}
innerRadius={innerRadius}
outerRadius={outerRadius}
startAngle={startAngle}
endAngle={endAngle}
fill={fill}
/>
<Sector
cx={cx}
cy={cy}
startAngle={startAngle}
endAngle={endAngle}
innerRadius={outerRadius + 6}
outerRadius={outerRadius + 10}
fill={fill}
/>
<path d={`M${sx},${sy}L${mx},${my}L${ex},${ey}`} stroke={fill} fill="none" />
<circle cx={ex} cy={ey} r={2} fill={fill} stroke="none" />
<text x={ex + ((cos >= 0 ? 1 : -1) * 12)} y={ey} textAnchor={textAnchor} fill="#333">{`PV ${value}`}</text>
<text x={ex + ((cos >= 0 ? 1 : -1) * 12)} y={ey} dy={18} textAnchor={textAnchor} fill="#999">
{`(Rate ${(percent * 100).toFixed(2)}%)`}
</text>
</g>
);
};
renderActiveShape.propTypes = {
cx: PropTypes.number,
cy: PropTypes.number,
midAngle: PropTypes.number,
innerRadius: PropTypes.number,
outerRadius: PropTypes.number,
startAngle: PropTypes.number,
endAngle: PropTypes.number,
fill: PropTypes.string,
payload: PropTypes.string,
percent: PropTypes.number,
value: PropTypes.number,
};
renderActiveShape.defaultProps = {
cx: 0,
cy: 0,
midAngle: 0,
innerRadius: 0,
outerRadius: 0,
startAngle: 0,
endAngle: 0,
fill: '#eee',
payload: '',
percent: 0,
value: 0,
};
class PieCustomShape extends React.Component {
state = {
activeIndex: 0
};
onPieEnter(evt) {
const index = data6.findIndex(p => p.name === evt.name);
this.setState({
activeIndex: index,
});
}
render() {
return (
<PieChart
width={800}
height={400}
margin={{
top: 5,
right: 30,
left: 20,
bottom: 5
}}
>
<Pie
dataKey="value"
activeIndex={this.state.activeIndex}
activeShape={renderActiveShape}
data={data6}
cx={300}
cy={200}
innerRadius={60}
outerRadius={100}
fill={color.secondary}
fillOpacity="0.8"
onMouseEnter={(event) => this.onPieEnter(event)}
/>
</PieChart>
);
}
}
export default PieCustomShape;
|