-
Notifications
You must be signed in to change notification settings - Fork 0
/
自制Raddar.vue
402 lines (399 loc) · 11.6 KB
/
自制Raddar.vue
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<template></template>
<div class="raddar-box"
:class="{hover:isHover}"
@mousemove="(e)=>mouseMove(e)"
@click="(e)=>mouseMove(e)">
<canvas id="canvas">
<p>你的浏览器不支持Canvas</p>
</canvas>
<div ref="listRef"
class="list">
<p>{{option.raddarData[currentIndex].name}}</p>
<ul>
<li v-for="(item, index) in option.indicator"
:key="index">{{item.name}} : {{option.raddarData[currentIndex].val[index]}}</li>
</ul>
</div>
</div>
</template>
<script>
import { debounce } from 'debounce'
export default {
props: {
data: {
type: Array,
default: () => [],
},
chartOption: {
type: Object,
default: () => {},
},
},
data() {
return {
ctx: '',
center: {},
r: 0,
length: 0,
isHover: false,
listDis: 20,
option: {
width: 800,
height: 800,
split: 3,
paddingGap: 20,
textGap: 15,
legend: [
{ color: '#6837C2', name: '当前技能等级' },
{ color: '#14D3FF', name: '平均技能等级' },
],
areaStyle: ['rgba(97,172,255,0.1)', 'rgba(97,172,255,0.2)', 'rgba(97,172,255,0.8)'],
lineStyle: ['rgba(231,242,255, 1)', 'rgba(231,242,255, 0.3)', 'rgba(255,255,255, 1)'],
raddarData: [
{
name: '平均技能等级',
opt: {
strokeColor: '#14D3FF',
imgUrl: process.env.imgUrl + 'rsxq/radar-symbol.png',
fillColor: ['rgba(0,255,215,0.6)', 'rgba(0,48,255,0.6)'],
},
val: [2, 0, 1, 0, 3, 2],
},
{
name: '当前技能等级',
opt: {
strokeColor: '#6837C2',
imgUrl: process.env.imgUrl + 'rsxq/radar-symbol2.png',
fillColor: ['rgba(149,0,255,0.6)', 'rgba(0,43,255,0.6)'],
},
val: [1, 2, 1, 0, 2, 1],
},
],
},
pointArr: [],
canvasScale: {},
listData: [],
currentIndex: 1,
}
},
mounted() {
this.option.raddarData.forEach((item, index) => {
item.val = this.data[index]
})
this.option = { ...this.option, ...this.chartOption }
this.init('#canvas')
},
methods: {
mouseMove: debounce(function (e) {
let isHover = null
this.pointArr.map((item, index) => {
let flag = this.calcArea({ x: e.offsetX * this.canvasScale.w, y: e.offsetY * this.canvasScale.h }, item)
if (flag) {
isHover = index
}
})
if (isHover != null) {
let dom = document.querySelector('#canvas')
this.isHover = true
this.crashCheck(dom, this.$refs.listRef, e.offsetX, e.offsetY)
this.$refs.listRef.style.display = 'block'
if (this.currentIndex == isHover) {
return
}
this.currentIndex = isHover
if (this.currentIndex == 1) {
this.reRender(this.ctx)
} else {
this.initRender(this.ctx)
}
} else {
this.$refs.listRef.style.display = 'none'
this.isHover = false
if (this.currentIndex == 1) {
return
}
this.currentIndex = 1
this.initRender(this.ctx)
}
}, 20),
init(dom) {
let canvas = document.querySelector(dom)
canvas.width = this.option.width
canvas.height = this.option.height
this.ctx = canvas.getContext('2d')
this.ctx.globalCompositeOperation = 'lighter'
this.canvasScale = this.getScale(canvas)
this.center = {
x: this.option.width / 2 + this.option.paddingGap,
y: this.option.height / 2 + this.option.paddingGap,
}
this.r = this.option.width / 2 - 2 * this.option.paddingGap
this.areaDis = Math.floor(this.r / (this.option.split + 1))
this.length = this.option.indicator.length
this.option.raddarData.forEach((item) => {
let arr = this.data2Point(item.val)
this.pointArr.push(arr)
})
this.initRender(this.ctx)
},
initRender(ctx) {
this.ctx.clearRect(0, 0, this.option.width, this.option.height)
this.drawBg(ctx)
this.drawLine(ctx)
this.drawLegend(ctx)
this.renderData(ctx)
},
renderData(ctx) {
let last = {}
this.option.raddarData.forEach((item, index) => {
if (this.currentIndex !== index) {
this.drawData(ctx, this.pointArr[index], item.opt)
} else {
last.arr = this.pointArr[index]
last.opt = item.opt
}
})
setTimeout(() => this.drawData(ctx, last.arr, last.opt), 0)
},
reRender(ctx) {
this.ctx.clearRect(0, 0, this.option.width, this.option.height)
this.drawBg(ctx)
this.drawLine(ctx)
this.drawLegend(ctx)
this.renderData(ctx)
},
// ================ 绘画 ==================
// 绘制背景
drawBg(ctx) {
let r = this.r
ctx.lineWidth = 2
//每次以不同半径绘制多个由大到小的图形
for (let j = 0; j < this.option.split; j++) {
//移动至第一个绘图点
r = this.areaDis * (this.option.split - j)
ctx.beginPath()
ctx.strokeStyle = this.option.lineStyle[j]
//转动坐标系绘制所有点
this.cacl(r).forEach((item, index) => {
if (index === 0) {
ctx.moveTo(item.x, item.y)
}
ctx.lineTo(item.x, item.y)
if (j === 0) {
this.drawText(ctx, item, index)
}
})
ctx.fillStyle = this.option.areaStyle[j]
ctx.fill()
ctx.closePath()
ctx.stroke()
//明暗色替换填充
}
},
// 绘制图例
drawLegend(ctx) {
let bottom = this.option.height - 10
let initDis = 180
this.option.legend.forEach((item, index) => {
ctx.beginPath()
ctx.fillStyle = item.color
ctx.fillRect(initDis + 300 * index, bottom - 10, 50, 4)
ctx.beginPath()
ctx.fillStyle = '#555550'
ctx.fillText(item.name, initDis + 60 + 300 * index, bottom)
})
},
//绘制文字
drawText(ctx, item, index) {
ctx.fillStyle = '#555550'
ctx.font = `${this.option.width / 30}px bold 黑体`
if (item.x - this.center.x > 0.5) {
ctx.textAlign = 'left'
item.x = item.x + this.option.textGap
} else if (item.x - this.center.x < -0.5) {
ctx.textAlign = 'right'
item.x = item.x - this.option.textGap
} else {
ctx.textAlign = 'center'
item.y = item.y - this.center.y > 0 ? item.y + 20 + this.option.textGap : item.y - this.option.textGap
}
let name = this.option.indicator[index].name
if (name.length > 4) {
ctx.fillText(name.substr(0, 4), item.x, item.y)
ctx.fillText(name.substr(4, 6), item.x, item.y + 30)
} else {
ctx.fillText(name, item.x, item.y)
}
},
// 背景顶点连线
drawLine(ctx) {
ctx.beginPath()
ctx.strokeStyle = '#E7F2FF'
ctx.lineWidth = 1
this.cacl(this.r - this.areaDis).forEach((item, index) => {
ctx.moveTo(item.x, item.y)
ctx.lineTo(this.center.x, this.center.y)
})
ctx.closePath()
ctx.stroke()
},
// 把数据转换成点
data2Point(data) {
let arr = []
data.forEach((item, index) => {
if (item == 0) return
let r = item * this.areaDis
arr.push(this.caclSingle(r, index))
})
return arr
},
// 绘制数据
drawData(dataCtx, data, opt) {
let isFirstPoint = true
let lg
dataCtx.lineWidth = 4
dataCtx.strokeStyle = opt.strokeColor
dataCtx.beginPath()
dataCtx.globalCompositeOperation = 'source-over'
// 描点
data.forEach((item) => {
let img = new Image()
img.src = opt.imgUrl
if (isFirstPoint) {
dataCtx.moveTo(item.x, item.y)
isFirstPoint = false
} else {
dataCtx.lineTo(item.x, item.y)
}
img.onload = () => {
dataCtx.drawImage(img, item.x - 8, item.y - 8, 15, 15)
}
})
if (data.length <= 2) {
dataCtx.lineTo(this.center.x, this.center.y)
}
lg = dataCtx.createLinearGradient(this.center.x, 0, this.center.x, this.option.height)
opt.fillColor.forEach((item, index) => {
lg.addColorStop(index, item)
})
dataCtx.fillStyle = lg
dataCtx.fill()
dataCtx.closePath()
dataCtx.stroke()
// let img = new Image()
// img.src = dataCavas.toDataURL('image/jpg')
// return img
},
// =============== 工具函数 ===================
// 碰撞检测
crashCheck(bDom, elDom, posX, posY) {
let t1 = bDom.offsetTop,
l1 = bDom.offsetLeft,
r1 = bDom.offsetLeft + bDom.offsetWidth,
b1 = bDom.offsetTop + bDom.offsetHeight
let t2 = elDom.offsetTop,
l2 = elDom.offsetLeft,
r2 = elDom.offsetLeft + elDom.offsetWidth,
b2 = elDom.offsetTop + elDom.offsetHeight
// l1 < l2 || t1 < t2 不用管
if (b1 < b2) {
elDom.style.top = posY - (this.listDis + elDom.offsetHeight) + 'px'
elDom.style.left = posX + this.listDis + 'px'
} else if (r1 < r2) {
elDom.style.left = posX - (this.listDis + elDom.offsetWidth) + 'px'
elDom.style.top = posY + this.listDis + 'px'
} else {
elDom.style.left = posX + this.listDis + 'px'
elDom.style.top = posY + this.listDis + 'px'
}
},
// 创建canvas
createCtx() {
let canvas = document.createElement('canvas')
canvas.width = this.option.width
canvas.height = this.option.height
let ctx = canvas.getContext('2d')
return { canvas, ctx }
},
// 获取canvas缩放比例
getScale(dom) {
return { w: this.option.width / dom.offsetWidth, h: this.option.height / dom.offsetHeight }
},
// 计算点的位置
cacl(r) {
let returnPoint = []
for (var i = 0; i < this.length; i++) {
returnPoint.push(this.caclSingle(r, i))
}
return returnPoint
},
caclSingle(r, num) {
let round = -Math.PI / 2
const x = Math.cos(round - ((2 * Math.PI) / this.length) * num) * r + this.center.x
const y = Math.sin(round - ((2 * Math.PI) / this.length) * num) * r + this.center.y
return { x, y }
},
// 计算面积
calcArea(point, array) {
let angle = 0
for (let i = 0; i < array.length; i++) {
let nextIndex = i + 1 >= array.length ? 0 : i + 1
angle = angle + this.getAngle(array[i], array[nextIndex], point)
}
const endA = Math.floor((180 * angle) / Math.PI)
if (endA <= 360 && endA >= 357) {
return true
}
return false
},
//point3为夹角的点
getAngle(point1, point2, point3) {
const a = this.disPoint(point3, point2)
const b = this.disPoint(point3, point1)
const c = this.disPoint(point2, point1)
const cos = (a + b - c) / (2 * Math.pow(a, 0.5) * Math.pow(b, 0.5))
return Math.acos(cos)
},
disPoint(p1, p2) {
return Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2)
},
},
}
</script>
<style lang="scss" scoped>
.raddar-box {
position: relative;
&.hover {
cursor: pointer;
}
}
canvas {
width: 400px;
height: 400px;
}
.list {
position: absolute;
left: 0px;
top: 0px;
background: rgba(0, 0, 0, 0.5);
border-radius: 5px;
z-index: 999;
display: none;
padding: 5px 15px;
color: #ffffff;
min-width: 110px;
transition: all 0.4s ease-in-out;
p {
margin-bottom: 2px;
}
li {
line-height: 1.5;
}
}
@media only screen and (max-width: 767px) {
canvas {
width: 100%;
height: 100%;
}
}
</style>