-
Notifications
You must be signed in to change notification settings - Fork 1
/
PieChart.js
263 lines (234 loc) · 8.97 KB
/
PieChart.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { effect } from '@vue/reactivity';
import {
vec2
} from 'gl-matrix';
import colorString from 'color-string';
import { clone } from 'lodash';
import { AnimeArray, makeRenderCallback } from '../../animation/anime';
class PieChart {
constructor(options = {}) {
this.name = "PieChart";
this.options = options;
this.animationContext = {};
}
apply(globalCtx) {
globalCtx.Chart.hooks.initChart.tap(this.name, context2d => {
this.init(context2d, globalCtx);
});
globalCtx.Chart.hooks.renderChart.tap(this.name, context2d => {
this.render(context2d, globalCtx);
});
globalCtx.Chart.hooks.afterRenderChart.tap(this.name, context2d => {
this.afterRender(context2d, globalCtx);
});
}
init(context2d, globalCtx) {
const render = context2d.render.bind(context2d);
const renderCallback = makeRenderCallback(render);
globalCtx.effect(() => {
const {
data,
legend,
} = globalCtx.globalData.source;
const {
spanHorizontal,
spanVertical,
} = globalCtx.Layout;
const {
getColor,
} = globalCtx.theme;
const padding = 50;
const radius = (Math.min(spanHorizontal, spanVertical) - padding) /2;
this.radius = radius;
let sumup = 0;
legend.forEach((l, sid) => {
if(!l.disabled) {
sumup += data[sid];
}
});
const seriesInCoord = [];
// const seriesMeta = [];
let accu = 0;
legend.forEach((l, sid) => {
const theme = getColor(sid);
let spanRadius = 0;
const p = {
startAngle: accu,
middleAngle: accu + spanRadius / 2,
endAngle: accu + spanRadius,
radius,
spanRadius,
};
if(!l.disabled) {
const d = data[sid];
const ratio = d / sumup;
spanRadius = ratio * Math.PI * 2;
Object.assign(p, {
middleAngle: accu + spanRadius / 2,
endAngle: accu + spanRadius,
spanRadius,
});
accu += spanRadius;
}
seriesInCoord.push({
theme,
radius,
legend: l,
p,
dy: data[sid],
});
});
if(!this.animationContext.arcs) {
const animeArr = legend.map(() => ({
startAngle: 0,
middleAngle: 0,
endAngle: 0,
spanRadius: 0,
radius,
}));
this.animationContext.arcs =
new AnimeArray({
startArray: animeArr,
duration: 550,
renderCallback,
callBackOnMakeUp(makeup) {
return makeup.map(() => ({
startAngle: 0,
middleAngle: 0,
endAngle: 0,
radius,
spanRadius: 0,
}));
},
callbackOnElement(newState, oldState, idx, ratio, currentArray) {
if(ratio === 'end') {
currentArray[idx] = {...newState};
} else {
currentArray[idx] = {
startAngle: oldState.startAngle + (newState.startAngle-oldState.startAngle) * ratio,
middleAngle: oldState.middleAngle + (newState.middleAngle-oldState.middleAngle) * ratio,
endAngle: oldState.endAngle + (newState.endAngle-oldState.endAngle) * ratio,
spanRadius: oldState.spanRadius + (newState.spanRadius-oldState.spanRadius) * ratio,
radius: oldState.radius + (newState.radius-oldState.radius) * ratio,
};
}
}
});
}
Object.assign(globalCtx.Chart.chartMeta, {
seriesInCoord,
});
});
effect(() => {
const {
seriesInCoord,
} = globalCtx.Chart.chartMeta;
const {
x, y
} = globalCtx._mouse;
const {
top,
bottom,
left,
right
} = globalCtx.Layout;
const {
transformMtxRawInvert,
} = globalCtx.Coordinate.transformMeta;
if(y > bottom || y < top || x < left || x > right) {
globalCtx.Chart.chartMeta.focused = null;
return;
}
const vec = vec2.fromValues(x, y);
vec2.transformMat2d(vec, vec, transformMtxRawInvert);
const x1 = vec[0];
const y1 = vec[1];
const dist = Math.hypot(x1, y1);
if(dist > this.radius) {
globalCtx.Chart.chartMeta.focused = null;
return;
}
const x2 = 0;
const y2 = 1;
const dot = x1*x2 + y1*y2;
const det = x1*y2 - y1*x2;
let angle = Math.atan2(dot, det);
if(angle < 0) {
angle = Math.PI*2 + angle;
}
globalCtx.Chart.chartMeta.focused = seriesInCoord.find((s) => {
const { startAngle, endAngle } = s.p;
return angle > startAngle && angle < endAngle;
});
});
}
render(ctx, globalCtx) {
const {
seriesInCoord,
} = globalCtx.Chart.chartMeta;
const {
fontSize,
} = globalCtx.theme;
seriesInCoord.forEach(({ theme, legend, radius }, sid) => {
const textRadius = radius + 20;
// const lgname = legend.name;
ctx.addConditionBlockBegin(() => this.animationContext.arcs.value[sid].spanRadius === 0);
ctx.functionsCall(
(ctx) => {
ctx.save();
ctx.beginPath();
const curArc = this.animationContext.arcs.value[sid];
ctx.fillStyle = ctx.strokeStyle = colorString.to.rgb(theme.color);
ctx.moveTo(0, 0);
ctx.arc(0,0,curArc.radius, curArc.startAngle, curArc.endAngle);
ctx.lineTo(0, 0);
ctx.fill();
ctx.beginPath();
ctx.moveTo(0, 0);
const middleAngle = curArc.middleAngle;
const sinr = Math.sin(middleAngle);
const cosr = Math.cos(middleAngle);
const textx = textRadius * cosr;
const texty = textRadius * sinr;
const dir = cosr / Math.abs(cosr);
const textsx = textx + dir * 20;
ctx.lineTo(textx, texty);
ctx.lineTo(textsx, texty);
ctx.stroke();
ctx.restore();
ctx.textAlign = dir > 0 ? 'left' : 'right';
ctx.fillText(`${legend.name}`,
textsx + dir * 10,
texty + fontSize/2);
ctx.restore();
}
);
ctx.addConditionBlockEnd();
});
}
afterRender(context2d, globalCtx) {
globalCtx.effect(() => {
const {
seriesInCoord,
} = globalCtx.Chart.chartMeta;
this.animationContext.arcs.animeTo(seriesInCoord.map(s => clone(s.p)));
});
globalCtx.effect(() => {
const focused = globalCtx.Chart.chartMeta.focused;
const legend = globalCtx.globalData.source.legend;
const {
seriesInCoord,
} = globalCtx.Chart.chartMeta;
const ps = seriesInCoord.map(s => clone(s.p));
legend.forEach((l, lid) => {
if(focused && focused.legend.name === l.name) {
ps[lid].radius = this.radius * 1.15;
} else {
ps[lid].radius = this.radius;
}
});
this.animationContext.arcs.animeTo(ps);
});
}
}
export default PieChart;