tui-charts-scatter.vue
8.74 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
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
<template>
<view class="tui-charts__scatter-wrap" :style="{width:width+'rpx'}">
<view class="tui-scatter__legend" v-if="legend.show">
<view class="tui-scatter__legend-item" v-for="(item,index) in dataset" :key="index">
<view class="tui-legend__circle" :style="{background:item.color}"></view>
<text
:style="{fontSize:(legend.size || 24)+'rpx',lineHeight:(legend.size || 24)+'rpx',color:legend.color || '#333'}">{{item.name}}</text>
</view>
</view>
<view class="tui-charts__scatter-box" :style="{width:width+'rpx',height:height+'rpx'}">
<view class="tui-xAxis__line" v-for="(item,index) in xAxisData" :key="item.id"
:style="{left:index*(xAxisLine.itemGap || 100) +'rpx',borderLeftStyle:index===0?'solid':splitLine.type,borderLeftColor:index===0?yAxisLine.color:splitLine.color}">
<view class="tui-xAxis__tickmarks"
:style="{height:xAxisTick.height || '12rpx',background:xAxisTick.color || '#e3e3e3'}"></view>
<view class="tui-xAxis__val" :style="{color:xAxisLabel.color,fontSize:xAxisLabel.size+'rpx'}">
{{item.value}}
</view>
</view>
<view class="tui-yAxis__line" v-for="(item,index) in yAxisData" :key="item.id"
:style="{bottom:index* (yAxisLine.itemGap || 80) +'rpx',borderBottomStyle:index===0?'solid':splitLine.type,borderBottomColor:index===0?xAxisLine.color:splitLine.color}">
<view class="tui-yAxis__tickmarks"
:style="{width:yAxisTick.width || '12rpx',background:yAxisTick.color || '#e3e3e3'}"></view>
<view class="tui-yAxis__val" :style="{color:yAxisLabel.color,fontSize:yAxisLabel.size+'rpx'}">
{{item.value}}
</view>
</view>
<view v-for="(item,index) in dataset" :key="item.id">
<view @tap.stop="onDotTap(index,idx)" class="tui-scatter__item"
:class="{'tui-scatter__item-active':activeIdx===idx && activeIndex===index && tooltipShow}"
v-for="(model,idx) in item.source" :key="idx"
:style="{width:(item.width || 12)+'rpx',height:(item.width || 12)+'rpx',background:item.color || '#5677fc',left:model.x+'rpx',bottom:model.y+'rpx'}">
</view>
</view>
</view>
<view class="tui-scatter__tooltip" v-if="tooltip" :class="{'tui-scatter__tooltip-show':tooltipShow}">
<view class="tui-tooltip__title"></view>
<view class="tui-scatter__tooltip-item">
<view class="tui-legend__circle" :style="{background:tooltips.color}"></view>
<text class="tui-tooltip__val">{{tooltips.val}}</text>
</view>
</view>
</view>
</template>
<script>
export default {
name: "tui-charts-scatter",
emits: ['click'],
props: {
//图例,说明
legend: {
type: Object,
default () {
return {
show: false,
size: 24,
color: '#333'
}
}
},
tooltip: {
type: Boolean,
default: true
},
xAxis: {
type: Object,
default () {
return {
min: 0,
max: 100,
splitNumber: 20
}
}
},
//x轴刻度线
xAxisTick: {
type: Object,
default () {
return {
height: '12rpx',
//不显示则将颜色设置为transparent
color: '#e3e3e3'
}
}
},
//x轴线条
xAxisLine: {
type: Object,
default () {
return {
color: '#e3e3e3',
itemGap: 100
}
}
},
xAxisLabel: {
type: Object,
default () {
return {
color: "#333",
size: 24
}
}
},
yAxis: {
type: Object,
default () {
return {
min: 0,
max: 100,
splitNumber: 20
}
}
},
yAxisLine: {
type: Object,
default () {
return {
//不显示则将颜色设置为transparent
color: '#e3e3e3',
//y轴item间距 rpx
itemGap: 80
}
}
},
//y轴刻度线
yAxisTick: {
type: Object,
default () {
return {
width: '12rpx',
//不显示则将颜色设置为transparent
color: '#e3e3e3'
}
}
},
yAxisLabel: {
type: Object,
default () {
return {
color: "#333",
size: 24
}
}
},
//分割线
splitLine: {
type: Object,
default () {
return {
//分割线颜色,不显示则将颜色设置为transparent
color: "#e3e3e3",
type: "dashed"
}
}
}
},
// #ifndef VUE3
beforeDestroy() {
this.clearTimer()
},
// #endif
// #ifdef VUE3
beforeUnmount() {
this.clearTimer()
},
// #endif
data() {
return {
width: 0,
height: 0,
xAxisData: [],
yAxisData: [],
activeIndex: -1,
activeIdx: -1,
dataset: [],
tooltips: {},
tooltipShow: false,
timer: null
};
},
methods: {
generateArray(start, end) {
return Array.from(new Array(end + 1).keys()).slice(start);
},
init(dataset, xAxisValFormatter) {
let xTotal = this.xAxis.max - this.xAxis.min
let yTotal = this.yAxis.max - this.yAxis.min
let xSections = Math.ceil(xTotal / this.xAxis.splitNumber)
let ySections = Math.ceil(yTotal / this.yAxis.splitNumber)
let xSectionsArr = this.generateArray(0, xSections)
let ySectionsArr = this.generateArray(0, ySections)
this.xAxisData = xSectionsArr.map((item, index) => {
let val = item * this.xAxis.splitNumber + this.xAxis.min
val = xAxisValFormatter ? xAxisValFormatter(val) : val
return {
id: 'x_' + index,
value: val
}
})
this.yAxisData = ySectionsArr.map((item, idx) => {
return {
id: 'y_' + idx,
value: item * this.yAxis.splitNumber + this.yAxis.min
}
})
this.width = (this.xAxisLine.itemGap || 100) * xSections
this.height = (this.yAxisLine.itemGap || 80) * ySections;
dataset.map((item, i) => {
item.id = 'd_' + i;
item.source = item.source.map(model => {
return {
x: (Number(model[0]) - this.xAxis.min) / xTotal * this.width,
y: (Number(model[1]) - this.yAxis.min) / yTotal * this.height,
name: model[2],
x1: model[0],
y1: model[1]
}
})
})
this.dataset = dataset
},
draw(dataset, xAxisValFormatter) {
dataset = dataset || [];
this.init(dataset, xAxisValFormatter);
},
clearTimer() {
clearTimeout(this.timer)
this.timer = null;
},
tooltipHandle(index, idx) {
let data = this.dataset[index]
let item = data.source[idx]
let tooltips = {
color: data.color,
val: item.name || `${item.x1},${item.y1}`
}
this.tooltips = tooltips;
this.clearTimer()
this.tooltipShow = true;
this.timer = setTimeout(() => {
this.tooltipShow = false
}, 5000)
},
onDotTap(index, idx) {
this.activeIndex = index;
this.activeIdx = idx;
this.tooltipHandle(index, idx);
this.$emit('click', {
datasetIndex: index,
sourceIndex: idx
})
}
}
}
</script>
<style scoped>
.tui-charts__scatter-wrap {
position: relative;
font-weight: normal;
margin-bottom: 60rpx;
}
.tui-charts__scatter-box {
position: relative;
}
.tui-xAxis__line {
position: absolute;
left: 0;
border-left-width: 1px;
top: 0;
bottom: 0;
overflow: visible;
}
.tui-xAxis__tickmarks {
width: 1px;
position: absolute;
left: -1px;
bottom: 0;
transform: translateY(100%);
}
.tui-yAxis__line {
position: absolute;
left: 0;
border-bottom-width: 1px;
bottom: 0;
right: 0;
overflow: visible;
}
.tui-yAxis__tickmarks {
width: 12rpx;
height: 1px;
position: absolute;
left: 0;
top: 0;
transform: translateX(-100%);
z-index: 10;
}
.tui-xAxis__val {
position: absolute;
left: 0;
bottom: -16rpx;
transform: translate(-50%, 100%);
}
.tui-yAxis__val {
position: absolute;
left: -16rpx;
top: 0;
transform: translate(-100%, -50%);
z-index: 11;
}
.tui-scatter__item {
border-radius: 50%;
position: absolute;
z-index: 12;
left: 0;
bottom: 0;
/* #ifdef H5 */
cursor: pointer;
/* #endif */
transform: translate(-50%, 50%) scale(1);
transition: transform .3s;
}
.tui-scatter__item-active {
transform: translate(-50%, 50%) scale(1.2);
}
.tui-scatter__legend {
display: flex;
align-items: center;
flex-wrap: wrap;
}
.tui-scatter__legend-item {
display: flex;
align-items: center;
margin-left: 24rpx;
margin-bottom: 30rpx;
}
.tui-legend__circle {
height: 20rpx;
width: 20rpx;
border-radius: 50%;
margin-right: 8rpx;
flex-shrink: 0;
}
.tui-scatter__tooltip {
padding: 30rpx;
border-radius: 12rpx;
background-color: rgba(0, 0, 0, .6);
display: inline-block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(1);
z-index: 20;
visibility: hidden;
opacity: 0;
transition: all 0.3s;
}
.tui-scatter__tooltip-show {
visibility: visible;
opacity: 1;
}
.tui-scatter__tooltip-item {
display: flex;
align-items: center;
white-space: nowrap;
transform: scale(1);
}
.tui-tooltip__val {
font-size: 24rpx;
line-height: 24rpx;
color: #fff;
margin-left: 6rpx;
}
</style>