line.vue
4.73 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
<template>
<div class="line-container">
<canvas class="canvas" id="orderRef"></canvas>
<div class="line-tooltip flex" ref="lineTooltip"></div>
</div>
</template>
<script>
import F2 from '@antv/f2/lib/index-all'
export default {
name: 'lineChart',
data() {
return {
chart: null,
// tickInterval: 5,
chartColor: this.color,
refId: this.id
}
},
props: {
color: {
type: Array,
default: () => []
},
list: {
type: Array,
default: () => []
},
id: {
type: String,
default: () => ''
},
lineData: {
type: Array,
default: () => []
}
},
watch: {
lineData: {
handler() {
this.initCanvas()
},
deep: true
}
},
mounted() {
this.initCanvas()
},
methods: {
initCanvas(id = 'orderRef') {
// let offsetY
// offsetY = document.getElementById(id).height * 0.4
// await this.dynamicTick() //动态生成各个标度点的间距
const data = this.lineData
let tickIntervalNum = 0
// 判断lineData是否有值,根据barList中最大值计算tickInterval
if (data.length) {
let s = data[0].value
for (var i = 1; i < data.length; i++) {
if (s < data[i].value) {
s = data[i].value
}
}
tickIntervalNum = s
}
this.chart && this.chart.clear()
this.chart = null
this.chart = new F2.Chart({
id,
padding: [30, 40, 30, 40],
pixelRatio: window.devicePixelRatio
})
this.chart.source(data, {
date: {
type: 'timeCat',
mask: 'MM/DD',
range: [0, 1],
tickCount: 5
},
value: {
type: 'linear',
nice: true,
tickCount: 4,
tickInterval: Math.ceil(tickIntervalNum / 3)
}
})
this.chart.axis('date', {
label: function label(text, index, total) {
var textCfg = {}
if (index === 0) {
textCfg.textAlign = 'left'
} else if (index === total - 1) {
textCfg.textAlign = 'right'
}
return textCfg
}
})
const tooltipEl = this.$refs.lineTooltip
this.chart.tooltip({
custom: true,
showCrosshairs: false,
triggerOn: ['touchstart', 'touchmove'],
triggerOff: 'touchend',
onChange(ev) {
const currentData = ev.items[0]
// let reg = /(\d{4})-(\d{1,2})-(\d{1,2})/
// const time = currentData.origin.date.replace(reg, '$1年$2月$3日')
tooltipEl.innerHTML = `<div class="time">${currentData.origin.date}</div>
<div class="item flex"><span class="arc" style="color:#FA7C0B;">●</span>预约到店:${ev.items[0].value}</div>
<div class="item flex"><span class="arc" style="color:#3975C6;">●</span>实际到店:${ev.items[1].value}</div>`
tooltipEl.style.opacity = 1
tooltipEl.style.left = currentData.x - 140 / 2 + 'px'
tooltipEl.style.top = currentData.y - 45 - 15 + 'px'
},
onHide() {
tooltipEl.style.opacity = 0
}
})
this.chart.line().position('date*value').shape('smooth').color('type', [ '#FA7C0B','#3975C6'])
this.chart.legend(false)
this.chart.render()
},
dynamicTick() {
let entMicroCountData = [] //新增企微客户数
let newlyCustomerCountData = []
let entMicroCount
let newlyCustomerCount
let tickData = [...this.trendChartList]
tickData.forEach(i => {
entMicroCountData.push(i.entMicroCount)
newlyCustomerCountData.push(i.newlyCustomerCount)
})
entMicroCountData.sort((a, b) => {
return b - a
})
newlyCustomerCountData.sort((a, b) => {
return b - a
})
entMicroCount = entMicroCountData[0]
newlyCustomerCount = newlyCustomerCountData[0]
this.tickInterval = Math.ceil(entMicroCount > newlyCustomerCount ? entMicroCount / 5 : newlyCustomerCount / 5)
}
}
}
</script>
<style lang="scss" scoped>
.line-container {
position: relative;
.canvas {
width: 100%;
height: 180px;
}
.line-tooltip {
opacity: 0;
position: absolute;
width: 140px;
height: 70px;
border-radius: 6px;
box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.05);
background: #ffffff;
z-index: 99;
flex-direction: column;
font-size: 12px;
box-sizing: border-box;
padding: 10px;
left: 0;
top: 0;
/deep/.time {
color: #a9a9a9;
}
/deep/.item,
/deep/.time {
width: 100%;
text-align: left;
font-size: 12px;
}
/deep/.arc {
border-radius: 100%;
font-size: 5px;
margin-right: 4px;
}
}
}
</style>