常用配置项
<template>
<div ref="chart" style="width: 800px; height: 350px"></div>
</template>
<script>
export default {
data() {
return {
chartData: [
{ name: "Mon", value: 0 },
{ name: "Tue", value: 200 },
{ name: "Wed", value: 150 },
{ name: "Thu", value: 80 },
{ name: "Fri", value: 70 },
{ name: "Sat", value: 110 },
{ name: "Sunday", value: 130 },
],
};
},
mounted() {
this.initChart();
},
methods: {
initChart() {
const chart = echarts.init(this.$refs.chart);
const option = {
tooltip: {
show: true,
trigger: "axis",
confine: true,
axisPointer: {
type: "shadow",
shadowStyle: {
color: "rgba(86, 255, 218, 0.11)",
},
},
formatter: function (params) {
// 悬浮文字信息
let str = `${params[0].name}`;
// tooltip 过滤为 0 的数据
params.map((item) => {
if (item.data !== 0) {
str += `<br>${item.marker}<span style="margin-right:10px">${item.seriesName}</span><span style="float:right;font-weight:bold">${item.data}</span>`;
}
});
return str;
},
},
grid: {
top: "20px",
left: "40x",
right: 0,
bottom: "40px",
},
// 图例过多时展示翻页
legend: {
x: "right", // 水平居右
y: "center", // 垂直居中
icon: "rect", // 图例icon为方块
backgroundColor: "transparent",
itemHeight: 10, // 图例icon高度
itemWidth: 19, // 图例icon宽度
height: "90%",
borderColor: "rgba(91, 122, 216, 0.5)",
borderWidth: 1,
top: 4,
borderRadius: 4,
itemGap: 13,
padding: [5, 10, 10, 10],
orient: "vertical", // 垂直排列
type: "scroll", // 可滚动翻页的图例
pageIconSize: 10, //翻页按钮大小
pageIconColor: "#2C86FF", //翻页箭头颜色
pageIconInactiveColor: "rgba(44,132,251,0.40)", //翻页(即翻页到头时箭头的颜色
pageTextStyle: {
color: "#999", //翻页数字颜色
}, //开启tooltip
tooltip: {
show: true,
},
// 文字大于4个字时,切割字符,加...
formatter: function (value) {
if (value.length > 4) {
return `${value.slice(0, 4)}...`;
}
return value;
},
textStyle: {
lineHeight: 13,
},
align: "left", // 图例icon在左侧
selector: [
{
type: "all",
title: "全选",
},
{
type: "inverse",
title: "反选",
},
],
selected: {},
},
xAxis: {
type: "category",
data: this.chartData.map((x) => x.name),
axisTick: {
alignWithLabel: true,
},
axisLabel: {
interval: 0, // 强制显示所有标签
rotate: 40,
// x轴文字过长设置超出隐藏显示省略号(超过4个字显示省略号)
formatter: function (value) {
if (value.length > 4) {
return `${value.slice(0, 4)}...`;
}
return value;
},
},
},
yAxis: {
type: "value",
},
series: [
{
name: "测试",
data: this.chartData.map((x) => x.value),
type: "bar",
itemStyle: {
normal: {
label: {
show: true, //开启显示
position: "top", //在上方显示
// 柱状图顶端不展示数值为 0 的
formatter: function (val) {
if (val.value == 0) {
return "";
}
},
textStyle: {
//数值样式
color: "black",
fontSize: 12,
},
},
},
},
},
{
name: "测试名称123",
data: this.chartData.map((x) => x.value),
type: "bar",
itemStyle: {
normal: {
label: {
show: true, //开启显示
position: "top", //在上方显示
// 柱状图顶端不展示数值为 0 的
formatter: function (val) {
if (val.value == 0) {
return "";
}
},
textStyle: {
//数值样式
color: "black",
fontSize: 12,
},
},
},
},
},
],
};
chart.setOption(option);
},
},
};
</script>
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
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
显示代码 复制代码 复制代码