Chart.vue
12.6 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>
<div class="canvas-box">
<!-- 客户的折线图 -->
<canvas
id="lineCustomerChart"
class="canvas"
v-show="
(userInfo.accountLevel != 4 && reportType == 1 && tabType.index == 2 && tabIndex == 0 && trendChartList.length > 0) ||
(userInfo.accountLevel == 4 && reportType == 1 && tabType.index == 0 && tabIndex == 0 && trendChartList.length > 0)
"
></canvas>
<div class="line-tooltip flex" ref="lineCustomerTooltip" id="2" v-if="tabIndex == 0"></div>
<!-- 产值的折线图 -->
<canvas
id="lineChart"
class="canvas"
v-show="
(tabIndex == 1 && userInfo.accountLevel != 4 && reportType == 1 && tabType.index == 2 && trendList.length > 0) ||
(tabIndex == 1 && userInfo.accountLevel == 4 && reportType == 1 && tabType.index == 0 && trendList.length > 0)
"
></canvas>
<div class="line-tooltip flex" ref="lineTooltip" id="3" v-if="tabIndex == 1"></div>
<canvas class="canvas" id="pieChart" v-show="tabIndex == 1 && tabType.index == 1 && productionValueReportList.length > 0"></canvas>
<template
v-if="
(userInfo.accountLevel != 4 && reportType == 1 && tabType.index == 2 && tabIndex == 0 && trendChartList.length == 0) ||
(userInfo.accountLevel == 4 && reportType == 1 && tabType.index == 0 && tabIndex == 0 && trendChartList.length == 0) ||
(tabIndex == 1 && userInfo.accountLevel != 4 && reportType == 1 && tabType.index == 2 && trendList.length == 0) ||
(tabIndex == 1 && userInfo.accountLevel == 4 && reportType == 1 && tabType.index == 0 && trendList.length == 0) ||
(tabIndex == 1 && tabType.index == 1 && productionValueReportList.length == 0)
"
>
<Empty></Empty>
</template>
</div>
</template>
<script type='text/ecmascript-6'>
import F2 from '@antv/f2/lib/index-all'
import { mapState, mapGetters, mapActions } from 'vuex'
export default {
name: 'Chart',
computed: {
...mapState({
tabIndex: state => state.report.tabIndex,
tabType: state => state.report.tabType,
productionValueReportList: state => state.report.productionValueReportList, //饼图
reportType: state => state.report.reportType,
userInfo: state => state.report.userInfo,
trendList: state => state.report.trendList, //产值折线
trendChartList: state => state.report.trendChartList //客户趋势
})
},
watch: {
tabType: {
handler(n) {
let { userInfo } = this
this.$nextTick(() => {
this.initCanvas(userInfo.accountLevel)
})
},
deep: true
},
trendChartList(n) {
let { userInfo } = this
this.$nextTick(() => {
this.initCanvas(userInfo.accountLevel)
})
},
trendList(n) {
let { userInfo } = this
this.$nextTick(() => {
this.initCanvas(userInfo.accountLevel)
})
},
productionValueReportList(n) {
let { userInfo } = this
this.$nextTick(() => {
this.initCanvas(userInfo.accountLevel)
})
}
},
data() {
return {
chart: null,
lineChart: null,
lineChartCustomer: null,
tickInterval: 5,
orderSource: {
1: '商城订单',
2: '直播订单',
3: '活动订单',
4: '门店服务套餐',
5: '精品升级',
6: '权益提单',
7: '商城订单'
}
}
},
mounted() {
let { userInfo } = this
if (userInfo) {
this.$nextTick(() => {
this.initCanvas(userInfo.accountLevel)
})
}
},
methods: {
initCanvas(type) {
//tabIndex 0 客户 1 产值
this.$nextTick(() => {
let { reportType, tabIndex, tabType } = this
if ((tabIndex == 1 && type != 4 && reportType == 1 && tabType.index == 2) || (tabIndex == 1 && type == 4 && reportType == 1 && tabType.index == 0)) this.initLineChart() //产值
if ((tabIndex == 0 && type != 4 && reportType == 1 && tabType.index == 2) || (tabIndex == 0 && type == 4 && reportType == 1 && tabType.index == 0)) this.lineCustomerChart() //客户
if ((type == 4 && tabIndex == 1 && tabType.index == 1) || (type != 4 && tabIndex == 1 && tabType.index == 1) || reportType == 0) this.initPieChart()
})
},
initLineChart(id = 'lineChart') {
let { trendList } = this
trendList.forEach(item => {
item.type = '新增产值'
})
this.lineChart && this.lineChart.clear()
this.lineChart = null
this.lineChart = new F2.Chart({
id,
padding: [70, 50, 20, 50],
pixelRatio: window.devicePixelRatio
})
this.lineChart.source(trendList, {
dataDateStr: {
type: 'timeCat',
mask: 'MM/DD',
tickCount: 5,
range: [0, 1]
},
orderMoneyTotalSum: {}
})
this.lineChart.axis('dataDateStr', {
label: function label(text, index, total) {
var textCfg = {}
if (index === 0) {
textCfg.textAlign = 'left'
} else if (index === total - 1) {
textCfg.textAlign = 'right'
}
return textCfg
}
})
// this.lineChart.tooltip({
// showTitle: true, // 展示 tooltip 的标题
// layout: 'vertical'
// })
const tooltipEl = this.$refs.lineTooltip
this.lineChart.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.dataDateStr.replace(reg, '$1年$2月$3日')
tooltipEl.innerHTML = `<div class="time">${time}</div>
<div class="item flex"><span class="arc" style="color:#3975C6;">●</span>新增产值:${ev.items[0].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.lineChart.legend(false)
this.lineChart.line().position('dataDateStr*orderMoneyTotalSum').shape('smooth').color('type', ['#3975C6'])
const textObj = {
top: true,
position() {
return [0, 0] // 位置信息
},
style: {
fill: '#888', // 文本颜色
fontSize: '10' // 文本大小
}
}
this.lineChart.guide().text({
...textObj,
content: '日期',
offsetX: window.screen.width - 100,
offsetY: 14
})
this.lineChart.guide().text({
...textObj,
content: '产值',
offsetX: -10,
// offsetY: -`${offsetY}`
offsetY: -156
})
this.lineChart.render()
},
initPieChart(id = 'pieChart') {
let { productionValueReportList, orderSource } = this
productionValueReportList.map(e => {
e.count = 'count'
})
this.chart && this.chart.clear()
this.chart = null
this.chart = new F2.Chart({
id,
pixelRatio: window.devicePixelRatio,
padding: [40, 40, 40, 40]
})
this.chart.source(productionValueReportList)
this.chart.coord('polar', {
transposed: true,
radius: 0.9,
innerRadius: 0.6
})
this.chart.axis(false)
this.chart.legend(false)
this.chart.tooltip(false)
this.chart.guide().html({
position: ['50%', '50%'],
html: '<div style="text-align: center;width:150px;height: 50px;">\n <p style="font-size: 12px;color: #999;margin: 0" id="title"></p>\n <p style="font-size: 18px;color: #343434;margin: 0;font-weight: bold;" id="money"></p>\n </div>'
})
this.chart.interval().position('count*newProductionValue').adjust('stack').color('orderSource', ['#3975C6', '#43B899', '#FFC667', '#FF9DAA'])
this.chart.pieLabel({
sidePadding: 10,
activeShape: true,
label1(data, color) {
return {
text: `${orderSource[data.orderSource]}-占${data.newProductionValueRate}`,
fill: color
}
},
label2(data) {
return {
text: `¥${data.newProductionValue}`,
fill: '#333',
fontSize: 12,
fontWeight: 'bold'
}
},
onClick(ev) {
const data = ev.data
if (data) {
// console.log(data)
}
}
})
this.chart.render()
},
async lineCustomerChart(id = 'lineCustomerChart') {
let offsetY
offsetY = document.getElementById(id).height * 0.4
await this.dynamicTick() //动态生成各个标度点的间距
this.lineChartCustomer && this.lineChartCustomer.clear()
this.lineChartCustomer = null
this.lineChartCustomer = new F2.Chart({
id,
padding: [70, 50, 20, 50],
pixelRatio: window.devicePixelRatio
})
let trendList = []
let { trendChartList } = this
trendChartList.forEach(item => {
trendList.push(
{
dayTime: item.dayTime,
type: '新增企微客户数',
value: item.entMicroCount
},
{
dayTime: item.dayTime,
type: '新增客户',
value: item.newlyCustomerCount
}
)
})
this.lineChartCustomer.source(trendList, {
dayTime: {
type: 'timeCat',
mask: 'MM/DD',
range: [0, 1],
tickCount: 5
},
value: {
type: 'linear',
nice: true,
tickInterval: this.tickInterval
}
})
this.lineChartCustomer.axis('dayTime', {
label: function label(text, index, total) {
var textCfg = {}
if (index === 0) {
textCfg.textAlign = 'left'
} else if (index === total - 1) {
textCfg.textAlign = 'right'
}
return textCfg
}
})
// Tooltip 配置
// this.lineChartCustomer.tooltip({
// showTitle: true, // 展示 tooltip 的标题
// layout: 'vertical'
// })
const tooltipEl = this.$refs.lineCustomerTooltip
this.lineChartCustomer.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.dayTime.replace(reg, '$1年$2月$3日')
tooltipEl.innerHTML = `<div class="time">${time}</div>
<div class="item flex"><span class="arc" style="color:#3975C6;">●</span>新增企微客户:${ev.items[0].value}</div>
<div class="item flex"><span class="arc" style="color:#FA7C0B;">●</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.lineChartCustomer.line().position('dayTime*value').shape('smooth').color('type', ['#3975C6', '#FA7C0B'])
this.lineChartCustomer.legend(false)
const textObj = {
top: true,
position() {
return [0, 0] // 位置信息
},
style: {
fill: '#888', // 文本颜色
fontSize: '10' // 文本大小
}
}
this.lineChartCustomer.guide().text({
...textObj,
content: '日期',
offsetX: window.screen.width - 100,
offsetY: 0
})
this.lineChartCustomer.guide().text({
...textObj,
content: '数量',
offsetX: -10,
offsetY: -156
})
this.lineChartCustomer.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 scoped lang='scss'>
@import '@/assets/css/statistics.scss';
.canvas {
width: 345px;
height: 195px;
}
</style>