DrawLine.js
4.38 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
const _ctx_ = Symbol('_ctx_')
const _pixelRatio_ = Symbol('_pixelRatio_')
const _canvas_ = Symbol('_canvas_')
const _params_ = Symbol('_params_')
class DrawLineChart {
[_ctx_] = null;
[_pixelRatio_] = null;
[_canvas_] = null;
[_params_] = null;
maxYNum = null;
currentSegment = 1;
constructor(el, params, componentsThis) {
const query = wx.createSelectorQuery().in(componentsThis)
query.select(el).fields({
node: true,
size: true
}).exec((res) => {
console.log(res)
const canvas = res[0].node,
ctx = canvas.getContext('2d'),
dpr = wx.getSystemInfoSync().pixelRatio,
width = res[0].width * dpr,
height = res[0].height * dpr
canvas.width = width
canvas.height = height
this[_canvas_] = canvas
this[_ctx_] = ctx
this[_pixelRatio_] = dpr
this[_params_] = params
let max = Math.max(...params.data)
if (max < params?.areaData?.max) max = params.areaData.max
this.maxYNum = max + max / 10 || 10
this.drawLine(params.data)
})
}
drawXLine() {
const {
height,
width
} = this[_canvas_],
lineNum = 5
this[_ctx_].lineWidth = this.toRpx(1)
for (let i = 0; i < lineNum; i++) {
const y = height / lineNum * i
this[_ctx_].beginPath()
this[_ctx_].strokeStyle = '#E5E5EF'
this[_ctx_].setLineDash([this.toRpx(6), this.toRpx(4)])
this[_ctx_].moveTo(0, y)
this[_ctx_].lineTo(width, y)
this[_ctx_].stroke()
this[_ctx_].save()
}
this[_ctx_].beginPath()
this[_ctx_].strokeStyle = '#E5E5EF'
this[_ctx_].setLineDash([], 0)
this[_ctx_].moveTo(0, height)
this[_ctx_].lineTo(width, height)
this[_ctx_].stroke()
this[_ctx_].save()
}
drawAreaView() {
const {
height,
width
} = this[_canvas_], {
areaData
} = this[_params_],
min = height - (height / this.maxYNum * (areaData.min || 0)),
max = height - (height / this.maxYNum * (areaData.max || 10))
this[_ctx_].beginPath()
this[_ctx_].fillStyle = '#3275fa1a'
this[_ctx_].fillRect(this.toRpx(4), max, width - this.toRpx(8), min - max)
this[_ctx_].save()
}
drawLine(data) {
const {
height,
width
} = this[_canvas_], {
maxYNum
} = this,
maxXNum = data.length,
curveData = []
for (let i = 0; i < maxXNum; i++) {
curveData.push({
x: width / maxXNum * i + (width / maxXNum / 2),
y: height - (height / maxYNum * data[i])
})
}
this.drawCurve(curveData)
}
drawCurve(curveData, currentSegment = 1) {
const {
width,
height
} = this[_canvas_]
if (currentSegment < curveData.length - 1) {
this[_ctx_].clearRect(0, 0, width, height)
this.drawXLine()
this.drawAreaView()
this[_ctx_].lineWidth = this.toRpx(2)
const gradient = this[_ctx_].createLinearGradient(0, 0, width, 0);
gradient.addColorStop(0, '#51D3F4'); // 起点颜色
gradient.addColorStop(1, '#002EF1'); // 终点颜色
this[_ctx_].strokeStyle = gradient;
this[_ctx_].beginPath();
this[_ctx_].moveTo(curveData[0].x, curveData[0].y);
for (let i = 1; i <= currentSegment; i++) {
const p1 = curveData[i];
const p2 = curveData[i + 1];
const cp1 = {
x: p1.x + (p2.x - p1.x) * 0.5,
y: p1.y
};
const cp2 = {
x: p2.x - (p2.x - p1.x) * 0.5,
y: p2.y
};
this[_ctx_].bezierCurveTo(cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y);
this[_ctx_].shadowOffsetX = 0
this[_ctx_].shadowOffsetY = this.toRpx(8)
this[_ctx_].shadowBlur = this.toRpx(4)
this[_ctx_].shadowColor = '#c5cee080'
}
// 处理最后一个点
if (currentSegment == curveData.length - 2) {
const lastPoint = curveData[curveData.length - 1];
this[_ctx_].lineTo(lastPoint.x, lastPoint.y)
}
this[_ctx_].stroke();
this[_ctx_].restore()
}
currentSegment += 1
// 如果还有未绘制的曲线段,则请求下一帧动画
if (currentSegment <= curveData.length) {
// setTimeout(() => {
this[_canvas_].requestAnimationFrame(() => {
this.drawCurve(curveData, currentSegment)
})
// this.drawCurve(curveData, currentSegment)
// }, 1e3 / 30)
}
}
toRpx(val) {
return this[_pixelRatio_] * val
}
}
export default DrawLineChart