RadarChart.js
4.69 KB
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
// packageStudyCenter/pages/myTask/components/RadarChart/RadarChart.js
import { getSystemInfoSync } from '~/utils/systemInfo'
import uCharts from '~/packageStudyCenter/ucharts/index.js'
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
Component({
properties: {
chartData: {
type: Object,
value: {},
observer: function(newVal) {
this.checkData(newVal);
if (this.data.hasData) {
this.scheduleDraw();
}
}
},
heightClass: {
type: String,
value: ''
},
labelColors: {
type: Array,
value: ['#666666', '#666666', '#666666', '#666666', '#666666', '#666666', '#666666', '#666666', '#666666'],
observer: function(newVal) {
if (this.data.hasData && this.chartDrawn) {
this.scheduleDraw();
}
}
}
},
data: {
IMG_URL,
hasData: false,
chartHeight: 272
},
lifetimes: {
ready() {
this.initHeight();
this.checkData(this.properties.chartData);
},
moved() {
if (this.data.hasData && !this.chartDrawn) {
this.scheduleDraw();
}
},
detached() {
if (this.drawTimer) {
clearTimeout(this.drawTimer);
}
}
},
pageLifetimes: {
show() {
if (this.data.hasData && !this.chartDrawn) {
this.scheduleDraw();
}
}
},
methods: {
initHeight() {
const chartHeight = this.properties.heightClass === 'big-height' ? 300 : 272;
this.setData({ chartHeight });
},
checkData(chartData) {
const hasData = chartData &&
chartData.categories &&
chartData.categories.length > 0 &&
chartData.series &&
chartData.series.length > 0 &&
chartData.series[0].data &&
chartData.series[0].data.length > 0;
this.setData({ hasData });
},
scheduleDraw() {
if (this.drawTimer) {
clearTimeout(this.drawTimer);
}
this.drawTimer = setTimeout(() => {
this.drawCharts();
}, 100);
},
drawCharts() {
return new Promise((resolve, reject) => {
const query = wx.createSelectorQuery().in(this);
query.select('#radarChart').fields({ node: true, size: true }).exec((res) => {
if (!res[0]) {
reject(new Error('未找到canvas元素'));
return;
}
const canvas = res[0].node;
if (!canvas) {
reject(new Error('canvas节点为空'));
return;
}
try {
const ctx = canvas.getContext('2d');
const systemInfo = getSystemInfoSync();
const pixelRatio = systemInfo.pixelRatio;
const actualWidth = res[0].width * pixelRatio;
const actualHeight = res[0].height * pixelRatio;
canvas.width = actualWidth;
canvas.height = actualHeight;
const config = {
type: "radar",
animation: false,
background: "#FFFFFF",
fontSize: 12,
canvas2d: true,
categories: this.properties.chartData.categories,
color: ["#3B73FF"],
context: ctx,
dataLabel: false,
extra: {
radar: {
gridType: "radar",
gridColor: "#dae8fd",
gridCount: 3,
opacity: 0.2,
max: 3,
labelShow: true,
labelColor: this.properties.labelColors,
labelFontSize: 12,
dataPointShape: false,
axisLabelTofix: 1,
labelFormatter: (val) => typeof val === 'number' ? val.toFixed(1) : val,
border: false,
borderColor: "#3B73FF",
borderWidth: 1,
fillOpacity: 0.2
}
},
height: actualHeight,
legend: { show: false },
padding: [5, 5, 5, 5],
pixelRatio: pixelRatio,
series: this.properties.chartData.series,
width: actualWidth,
xAxis: { disableGrid: false },
yAxis: { data: [{ min: 0 }] }
};
new uCharts(config);
this.chartDrawn = true;
resolve();
} catch (error) {
console.error('绘制雷达图失败:', error);
reject(error);
}
});
});
},
// 外部调用方法:强制重绘
redraw() {
this.chartDrawn = false;
this.scheduleDraw();
}
}
});