-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathpie.jsx
157 lines (129 loc) · 3.35 KB
/
pie.jsx
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"use strict";
import {
default as React,
Component,
} from 'react';
import D3Shape from 'd3-shape'
import {pieProps} from '../commonProps';
export default class Pie extends Component {
constructor (props) {
super(props);
}
static defaultProps = {
onMouseOver: (d) => {},
onMouseOut: (d) => {},
...pieProps
}
triggerOver(data , e) {
this.props.onMouseOver(e, data)
}
triggerOut(data, e) {
this.props.onMouseOut(e, data)
}
mkSeries() {
const {
data,
chartSeries,
value,
name,
categoricalColors
} = this.props;
var chartSeriesData = chartSeries.map((f, i) => {
// set a color if not set
if(!f.color)
f.color = categoricalColors(i);
// set name if not set
if(!f.name)
f.name = f.field;
var val;
data.forEach((d) => {
if(name(d) === f.field)
val = d;
})
return Object.assign(f, {value: value(val)});
})
return chartSeriesData;
}
_mkPie () {
var {
width,
height,
innerRadius,
outerRadius,
pieSort,
value,
radius,
pieTextShow
} = this.props;
const that = this
var radius = this.props.radius || Math.min(width - 100, height - 100) / 2;
var outerRadius = outerRadius || (radius - 10)
var labelr = radius + 10;
var chartSeriesData = this.mkSeries();
var arc = D3Shape.arc()
.outerRadius(outerRadius)
.innerRadius(innerRadius);
var arcOver = D3Shape.arc()
.outerRadius(outerRadius + 10)
.innerRadius(innerRadius);
var pie = D3Shape.pie()
.sort((a, b) => { return pieSort(a.value, b.value)})
.value((d) => { return d.value; })
return (
<g className="arc">
{
pie(chartSeriesData).map((slice, i) => {
var textTransform = (d) => {
var c = arc.centroid(d),
x = c[0],
y = c[1],
// pythagorean theorem for hypotenuse
h = Math.sqrt( x * x + y * y);
return "translate(" + (x / h * labelr) + ',' +
(y / h * labelr) + ")";
}
var textAnchor = (d) => {
return (d.endAngle + d.startAngle)/2 > Math.PI ?
"end" : "start";
}
return (
<g key={i}>
<path
d={arc(slice)}
style={{fill: slice.data.color, stroke: '#FFF', ...slice.data.style}}
onMouseOut={that.triggerOut.bind(this, slice)}
onMouseOver={that.triggerOver.bind(this, slice)}
/>
{
pieTextShow? (
<text
transform={textTransform(slice)}
dy=".35em"
textAnchor={textAnchor(slice)}
>
{slice.data.name}
</text>
): null
}
</g>
)
})
}
</g>
)
}
render() {
const{
width,
height,
margins
} = this.props;
var t = `translate(${(width - margins.left - margins.right) / 2}, ${(height - margins.top - margins.bottom) / 2})`;
var pie = this._mkPie();
return (
<g transform={t}>
{pie}
</g>
)
}
}