# 9.5:自定义饼状图(PieChart)
本节笔者带领读者实现一个饼状图 PieChart
组件,该组件是依托笔者之前封装的 MiniCanvas (opens new window) 实现的, PieChart
的最终演示效果如下图所示:
# 9.5.1:饼状图实现的拆分
根据上图的样式效果,实现一个饼状图,实质就是绘制一个个的实心圆弧加上圆弧对应颜色就搞定了,圆弧的大小是根据饼状的数据分布计算出来的,对应的颜色自己指定就可以了,其次手指点击到饼状图,需要找到对应的饼状块并突出显示,找到饼状块先计算手指点击坐标和圆弧中心的夹角,根据夹角和每个圆弧的大小找到对应的圆弧,找到圆弧后计算圆弧的突出偏移量并重置所有饼状块的圆弧起始值就可以了。
计算夹角
计算夹角就是计算手指点击饼状图上的坐标 (x, y) 和饼状图的圆心坐标 (centerX, centerY) 之间的顺时针角度,计算方法如下所示:
private getTouchedAngle(centerX: number, centerY, x: number, y: number) { var deltaX = x - centerX; var deltaY = centerY - y; var t = deltaY / Math.sqrt(deltaX * deltaX + deltaY * deltaY); var angle = 0; if (deltaX > 0) { if (deltaY > 0) { angle = Math.asin(t); } else { angle = Math.PI * 2 + Math.asin(t); } } else if (deltaY > 0) { angle = Math.PI - Math.asin(t); } else { angle = Math.PI - Math.asin(t); } return 360 - (angle * 180 / Math.PI) % 360; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18找圆弧块
计算出手指点击位置和圆心的夹角后,遍历每一个饼状块做比较就可以了,代码如下所示:
private getTouchedPieItem(angle: number): PieItem { for(var i = 0; i < this.pieItems.length; i++) { var item = this.pieItems[i]; if(item.getStopAngle() < 360) { if(angle >= item.getStartAngle() && angle < item.getStopAngle()) { return item; } } else { if(angle >= item.getStartAngle() && angle < 360 || (angle >= 0 && angle < item.getStopAngle() - 360)) { return item; } } } return null; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15计算偏移量
找到圆弧块后,根据圆弧块的圆弧大小,计算出该圆弧突出后的偏移量,代码如下所示:
private calculateRoteAngle(item: PieItem): number { var result = item.getStartAngle() + item.getAngle() / 2 + this.getDirectionAngle(); if (result >= 360) { result -= 360; } if (result <= 180) { result = -result; } else { result = 360 - result; } return result; }
1
2
3
4
5
6
7
8
9
10
11
12重置偏移量
有了目标圆弧块的偏移角度后,重置每一个圆弧块的起始偏移量就可以了,代码如下所示:
private resetStartAngle(angle: number) { this.pieItems.forEach((item) => { item.setSelected(false); item.setStartAngle(item.getStartAngle() + angle); }); }
1
2
3
4
5
6重新绘制圆弧
绘制圆弧使用 MiniCanvas 提供的
drawArc()
方法即可,代码如下所示:drawPieItem() { this.pieItems.forEach((item) => { this.paint.setColor(item.color); var x = this.calculateCenterX(item.isSelected()); var y = this.calculateCenterY(item.isSelected()); this.canvas.drawArc(x, y, this.radius, item.getStartAngle(), item.getStopAngle(), this.paint); }) }
1
2
3
4
5
6
7
8
# 9.5.2:饼状图的实现
拆分完饼状图的步骤后,实现起来就方便多了, PieChart
的完整代码如下所示:
import { MiniCanvas, Paint, ICanvas } from './icanvas'
@Entry @Component struct PieChart {
private delegate: PieChartDelegate;
build() {
Column() {
MiniCanvas({
attribute: {
width: this.delegate.calculateWidth(),
height: this.delegate.calculateHeight(),
clickListener: (event) => {
// 根据点击绘制突出的饼状块
this.delegate.onClicked(event.x, event.y);
}
},
onDraw: (canvas) => {
// 开始绘制
this.delegate.setCanvas(canvas);
this.delegate.drawPieItem();
}
})
}
.padding(10)
.size({width: "100%", height: "100%"})
}
aboutToAppear() {
// mock测试数据
var pieItems = PieItem.mock();
// 初始化delegate
this.delegate = new PieChartDelegate(pieItems, RotateDirection.BOTTOM);
}
}
// 定义饼状块的属性,包括角度,起始角度,占比,颜色,是否选中突出
export class PieItem {
private startAngle: number = 0;
private rate: number = 0;
private angle: number = 0;
private selected: boolean = false;
constructor(public count: number, public color: string) {
}
setSelected(selected: boolean) {
this.selected = selected;
return this;
}
isSelected() {
return this.selected;
}
setStartAngle(startAngle: number) {
this.startAngle = startAngle > 360 ? startAngle - 360 : startAngle < 0 ? 360 + startAngle : startAngle;
return this;
}
getStartAngle() {
return this.startAngle;
}
getStopAngle() {
return this.startAngle + this.angle;
}
setRate(rate: number) {
this.rate = rate;
return this;
}
getRate() {
return this.rate;
}
setAngle(angle: number) {
this.angle = angle;
return this;
}
getAngle() {
return this.angle;
}
// mock一份测试数据
static mock(): Array<PieItem> {
var pieItems = new Array<PieItem>();
pieItems.push(new PieItem(21, "#6A5ACD"))
pieItems.push(new PieItem(18, "#20B2AA"))
pieItems.push(new PieItem(29, "#FFFF00"))
pieItems.push(new PieItem(12, "#00BBFF"))
pieItems.push(new PieItem(20, "#DD5C5C"))
pieItems.push(new PieItem(13, "#8B668B"))
return pieItems;
}
}
// 饼状块的突出方向
export enum RotateDirection {
LEFT,
TOP,
RIGHT,
BOTTOM
}
// 饼状图绘制的具体实现类
class PieChartDelegate {
private paint: Paint;
private canvas: ICanvas;
constructor(private pieItems: Array<PieItem>, private direction: RotateDirection = RotateDirection.BOTTOM, private offset: number = 10, private radius: number = 80) {
this.calculateItemAngle();
}
setPitItems(pieItems: Array<PieItem>) {
this.pieItems = pieItems;
}
setCanvas(canvas: ICanvas) {
this.canvas = canvas;
this.paint = new Paint();
}
onClicked(x: number, y: number) {
if(this.canvas) {
var touchedAngle = this.getTouchedAngle(this.radius, this.radius, x, y);
var touchedItem = this.getTouchedPieItem(touchedAngle);
if(touchedItem) {
var rotateAngle = this.calculateRoteAngle(touchedItem);
this.resetStartAngle(rotateAngle);
touchedItem.setSelected(true)
this.clearCanvas();
this.drawPieItem();
}
} else {
console.warn("canvas invalid!!!")
}
}
clearCanvas() {
this.canvas.clear();
}
drawPieItem() {
this.pieItems.forEach((item) => {
this.paint.setColor(item.color);
var x = this.calculateCenterX(item.isSelected());
var y = this.calculateCenterY(item.isSelected());
this.canvas.drawArc(x, y, this.radius, item.getStartAngle(), item.getStopAngle(), this.paint);
})
}
calculateWidth(): number {
if (this.direction == RotateDirection.LEFT || this.direction == RotateDirection.RIGHT) {
return this.radius * 2 + this.offset;
} else {
return this.radius * 2;
}
}
calculateHeight(): number {
if (this.direction == RotateDirection.TOP || this.direction == RotateDirection.BOTTOM) {
return this.radius * 2 + this.offset;
} else {
return this.radius * 2;
}
}
private calculateCenterX(hint: boolean): number {
if(this.direction == RotateDirection.LEFT) {
return hint ? this.radius : this.radius + this.offset;
} else if(this.direction == RotateDirection.TOP) {
return this.radius;
} else if(this.direction == RotateDirection.RIGHT) {
return hint ? this.radius + this.offset : this.radius;
} else {
return this.radius;
}
}
private calculateCenterY(hint: boolean): number {
if(this.direction == RotateDirection.LEFT) {
return this.radius;
} else if(this.direction == RotateDirection.TOP) {
return hint ? this.radius : this.radius + this.offset;
} else if(this.direction == RotateDirection.RIGHT) {
return this.radius;
} else {
return hint ? this.radius + this.offset : this.radius;
}
}
private resetStartAngle(angle: number) {
this.pieItems.forEach((item) => {
item.setSelected(false);
item.setStartAngle(item.getStartAngle() + angle);
});
}
private calculateRoteAngle(item: PieItem): number {
var result = item.getStartAngle() + item.getAngle() / 2 + this.getDirectionAngle();
if (result >= 360) {
result -= 360;
}
if (result <= 180) {
result = -result;
} else {
result = 360 - result;
}
return result;
}
private calculateItemAngle() {
var total = 0;
this.pieItems.forEach((item) => {
total += item.count;
})
for(var i = 0; i < this.pieItems.length; i++) {
var data = this.pieItems[i];
data.setRate(data.count / total);
data.setAngle(data.getRate() * 360);
if (i == 0) {
data.setStartAngle(0);
} else {
var preData = this.pieItems[i - 1];
data.setStartAngle(preData.getStopAngle());
}
}
}
private getDirectionAngle(): number {
var result = 270;
if (this.direction == RotateDirection.RIGHT) {
result = 0;
}
if (this.direction == RotateDirection.BOTTOM) {
result = 270;
}
if (this.direction == RotateDirection.LEFT) {
result = 180;
}
if (this.direction == RotateDirection.TOP) {
result = 90;
}
return result;
}
private getTouchedAngle(centerX: number, centerY, x: number, y: number) {
var deltaX = x - centerX;
var deltaY = centerY - y;
var t = deltaY / Math.sqrt(deltaX * deltaX + deltaY * deltaY);
var angle = 0;
if (deltaX > 0) {
if (deltaY > 0) {
angle = Math.asin(t);
} else {
angle = Math.PI * 2 + Math.asin(t);
}
} else if (deltaY > 0) {
angle = Math.PI - Math.asin(t);
} else {
angle = Math.PI - Math.asin(t);
}
return 360 - (angle * 180 / Math.PI) % 360;
}
private getTouchedPieItem(angle: number): PieItem {
for(var i = 0; i < this.pieItems.length; i++) {
var item = this.pieItems[i];
if(item.getStopAngle() < 360) {
if(angle >= item.getStartAngle() && angle < item.getStopAngle()) {
return item;
}
} else {
if(angle >= item.getStartAngle() && angle < 360 || (angle >= 0 && angle < item.getStopAngle() - 360)) {
return item;
}
}
}
return null;
}
}
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# 9.5.3:小结
本节简单介绍了实现一个饼状图的思路和实现,具体实现读者可以阅读源码,目前 PieChart
在选中饼状块并突出时没有动画特效而是直接旋转过来了,笔者尝试使用 setInterval()
实现,目前没有效果,已经在 OpenHarmony 仓库反馈该问题 (opens new window)了,有了结果后笔者会把旋转的动效加上。
请作者喝杯咖啡
©arkui.club版权所有,禁止私自转发、克隆网站。