spark-line.vue
2.01 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
<template>
<canvas ref="canvas" />
</template>
<script>
const F2 = require('@antv/f2/lib/core');
require('@antv/f2/lib/geom/line');
require('@antv/f2/lib/scale/time-cat');
const Animation = require('@antv/f2/lib/animation/detail');
const Tooltip = require('@antv/f2/lib/plugin/tooltip');
F2.Chart.plugins.register(Animation);
F2.Chart.plugins.register(Tooltip);
export default {
name: 'SparkLine',
methods: {
initChart() {
const chart = new F2.Chart({
el: this.$refs.canvas,
pixelRatio: window.devicePixelRatio,
});
chart.tooltip({
custom: true,
showXTip: true,
showYTip: true,
snap: true,
crosshairsType: 'xy',
crosshairsStyle: {
lineDash: [2],
},
});
chart.axis('date', {
label: function label(text, index, total) {
const textCfg = {};
if (index === 0) {
textCfg.textAlign = 'left';
} else if (index === total - 1) {
textCfg.textAlign = 'right';
}
return textCfg;
},
});
this.chart = chart;
},
changeData(...args) {
if (this.rendered) {
this.chart.changeData(...args);
} else {
this.repaint(...args);
}
},
repaint(data = [], { valueTickCount = 5, dataTickCount = 5 } = {}) {
if (!this.rendered) {
this.rendered = true;
} else {
this.chart.clear();
}
this.chart.source(data, {
value: {
tickCount: valueTickCount,
},
date: {
type: 'timeCat',
range: [0, 1],
tickCount: dataTickCount,
},
});
this.chart.line().position('date*value');
this.chart.render();
},
destroyChart() {
if (this.chart) {
this.chart.destroy();
this.chart = null;
}
},
},
mounted() {
this.initChart();
this.$emit('init', {chart: this.chart, component: this});
},
beforeDestroy() {
this.destroyChart();
},
};
</script>