ChartLine.vue
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
<script>
import * as echarts from 'echarts'
import { echartColors } from '@/utils/index'
import merge from 'lodash.merge'
export default {
// 折线图
name: 'ChartLine',
components: {},
props: {
// 折线背景渐变色
bgLinearGradient: {
type: Boolean,
default: false,
},
// X轴坐标数据
xData: {
type: Array,
default: () => ['2021-12-01', '2021-12-02', '2021-12-03', '2021-12-03', '2021-12-04', '2021-12-05', '2021-12-06'],
},
// 图例; 单个图例可传字符串,实际转换成单元素数组[string]
legend: {
type: [Array, String],
default: () => ['新增客户'],
},
// 展示数据集
// 注:二位数组, 其一维数组顺序需要和图例顺序一致,其二维数组顺序需要和X轴数组顺序一致
// 单条数据集:[120, 132, 101, 134, 90, 230, 210],其实会被转换成[[120, 132, 101, 134, 90, 230, 210]]
// 多条数据集:
// [
// [120, 132, 101, 134, 90, 230, 210],
// [220, 182, 191, 234, 290, 330, 310],
// [150, 232, 201, 154, 190, 330, 410],
// [320, 332, 301, 334, 390, 330, 320]
// ]
series: {
type: Array,
default: () => [120, 132, 101, 134, 90, 230, 210],
},
// 自定义图表配置项,使用loadsh.merge(origin, option)和原有的配置进行覆盖合并
// loadsh.merge: https://www.html.cn/doc/lodash/#_mergeobject-sources
option: {
type: Object,
default: null,
},
},
data() {
return {}
},
computed: {},
watch: {
series: {
deep: true,
handler() {
this.$nextTick(() => {
this.drawChart()
})
},
},
},
created() {},
mounted() {
this.drawChart()
},
methods: {
drawChart() {
if (!(this.series && this.series.length)) {
return
}
// eslint-disable-next-line
this.myChart = echarts.init(this.$refs.chart)
let option
let series = []
let legend = Array.isArray(this.legend) ? this.legend : [this.legend]
let seriesData = this.series
Array.isArray(this.series[0]) || (seriesData = [seriesData])
seriesData.forEach((data, index) => {
let obj = {
name: legend[index],
type: 'line',
smooth: true,
symbol: 'circle', // 数值点类型
symbolSize: 6, // 数值点大小
emphasis: {
focus: 'series',
},
data,
}
this.bgLinearGradient &&
Object.assign(obj, {
areaStyle: {
color: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{
offset: 0,
color: echartColors[index], // 0% 处的颜色
},
{
offset: 0.8,
color: 'white', // 100% 处的颜色
},
],
global: false, // 缺省为 false
},
},
})
series.push(obj)
})
option = {
color: echartColors,
// title: {
// text: 'Stacked Area Chart'
// },
tooltip: {
trigger: 'axis',
// textStyle: {
// color: '#FFF', // 设置文字颜色
// fontSize: 12,
// },
// backgroundColor: '#505050',
// borderWidth: 0,
// axisPointer: {
// type: 'cross',
// label: {
// backgroundColor: '#6a7985',
// },
// },
},
legend: {
data: legend,
// lineStyle: {
// width: 0
// },
// itemStyle: {
// borderWidth: 10,
// decal: {
// symbol: 'rect'
// }
// }
x: 'center',
y: 'bottom',
// selectedMode: false,
type: 'scroll', // 分页类型
icon: 'roundRect',
itemWidth: 24,
itemHeight: 4,
itemGap: 14,
tooltip: {
show: true,
},
textStyle: {
color: '#36363A',
fontSize: 12,
lineHeight: 14,
},
},
grid: {
left: '3%',
right: '3%',
bottom: '40px',
top: '30px',
containLabel: true,
},
xAxis: [
{
type: 'category',
data: this.xData,
// boundaryGap: false,
offset: 5,
axisLine: {
lineStyle: {
color: '#ccc',
},
},
axisLabel: {
// 坐标轴刻度标签的相关设置。
// interval: 0,
color: 'rgba(153, 153, 153, 1)', // 坐标字体颜色
},
axisTick: {
// 横坐标刻度
alignWithLabel: true,
show: true,
},
},
],
yAxis: [
{
type: 'value',
axisLine: {
lineStyle: {
color: '#ccc',
},
show: false,
},
axisLabel: {
// 坐标轴刻度标签的相关设置。
// interval: 0,
color: 'rgba(153, 153, 153, 1)', // 坐标字体颜色
},
},
],
series,
}
this.option && merge(option, this.option)
option && this.myChart.setOption(option)
new ResizeObserver((entries) => {
this.myChart.resize()
}).observe(this.$refs.chart)
},
},
}
</script>
<template>
<div v-if="series?.length" ref="chart" class="chart-line chart" key="1"></div>
<div v-else class="chart-line chart" key="2">
<div class="cc"><Empty /></div>
</div>
</template>
<style lang="scss" scoped>
.chart {
position: relative;
max-height: 100%;
min-width: 400px;
min-height: 400px;
}
</style>