官网链接:https://echarts.apache.org/zh/feature.html
Echarts配置项使用手册:https://echarts.apache.org/zh/option.html#title

什么是Echarts?它的作用是什么?

前言:Echarts是一个使用JS实现的开源可视化库,可以流畅的运行在PC和移动设备上。它兼容了绝大部分的浏览器,底层依赖矢量图形库ZRender,提供直观,交互丰富,可高度个性化定制的数据可视化图表。
作用:

  1. 丰富的可视化类型
  2. 多种数据格式无需转换直接使用
  3. 千万数据的前端展现
  4. 移动端优化
  5. 多渲染方案,可跨平台使用
  6. 深度交互式数据探索
  7. 多维数据支持以及丰富的视觉编码手段
  8. 动态数据
  9. 绚丽的特效
  10. 通过GL实现更多更强大绚丽的三维可视化
  11. 无障碍访问
    canvas完整版绘制柱形图链接:https://zhuanlan.zhihu.com/p/60285148

Echarts+vue3实现数据可视化项目

项目初始化创建

  1. 创建vue2项目

    1
    2
    3
    4
    5
    ## 安装vue/cli
    npm i -g @vue/cli
    ## 执行创建命令
    vue create 项目名称
    ## 税后可以选择式创建vue2项目还是创建vue3项目
  2. 安装echarts库

    1
    npm i --save echarts
  3. 在文件中引用echarts

    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
    <template>
    <!-- echarts容器,默认高度为0 -->
    <div ref="barEcharts" class="bars"></div>
    </template>

    <script>
    //将整个echarts库导入并赋值给名为echarts的变量
    import * as echarts from "echarts";
    export default {
    name: "HomeEcharts",
    mounted() {
    //对于进行数据可视化的dom元素进行初始化echarts实例
    let barEcharts = echarts.init(this.$refs.barEcharts);
    //指定图表的配置项
    let option = {
    //title:标题组件
    title: {
    text: "分类",
    },
    //x轴数据配置
    xAxis: {
    data: ["文学", "童话", "重生", "快穿", "玄幻"],
    },
    //y轴数据配置
    yAxis: {},
    series: [
    //系列
    {
    name: "销量",//系列名称,用于tooltip的显示,legend的图例筛选,在setOption更新数据和配置项时用于指定对应的系列
    type: "bar", //echarts图表类型,bar--柱状图
    data: [200, 456, 345, 178, 564],//系列中的数据内容数组。数组项通常为具体的数据项。
    },
    ],
    };
    //使用刚才指定的配置项和数据惊醒图表构建和显示
    barEcharts.setOption(option);
    },
    };
    </script>

    <style>
    .bars {
    width: 500px;
    height: 400px;
    }
    </style>

echarts配置项目

title配置
1
2
3
4
title:{
x:"center"//代表将标题整体水平居中,默认值是left
}
subText:"副标题"//设置副标题
tooltip配置——提示框组件

formatter可以自定义提示框内容,支持模板字符和函数回调两种形式

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
<template>
<!-- echarts容器,默认高度为0 -->
<div ref="barEcharts" class="bars"></div>
</template>

<script>
//将整个echarts库导入并赋值给名为echarts的变量
import * as echarts from "echarts";
export default {
name: "HomeEcharts",
mounted() {
//对于进行数据可视化的dom元素进行初始化echarts实例
let barEcharts = echarts.init(this.$refs.barEcharts);
//指定图表的配置项
let option = {
title: {
text: "分类",
},
xAxis: {
//在echarts中,如果不设置xAxis或yAxis的type属性,默认情况下会根据数据类型自动判断轴的类型。通常情况下,echarts会根据数据的类型来自动选择轴的类型,以最佳方式展示数据。
data: ["文学", "童话", "重生", "快穿", "玄幻"], //data类目数据
},
yAxis: {},
tooltip: {
//触发类型,默认是item图形触发
trigger: "axis",
//坐标轴指示器
axisPointer: {
type: "cross", //line显示一个实线 shadow显示阴影 cross十字准星
},
//自定义提示框信息
formatter(params){
//获取series中的所有信息
for(let i = 0;i<params.length ;i++){
return `${params[i].name}小说——销量${params[i].data.value}——日期${params[i].data.date}`
}
}
},
series: [
//系列
{
name: "销量",
type: "bar", //echarts图表类型,bar--柱状图
//data的元素可以是一个对象,里面包含多个键值对
// data: [200, 456, 345, 178, 564],
data: [
{ value: 200, date: "2022-01- 23" },
{ value: 700, date: "2023-04- 23" },
{ value: 260, date: "2022-07- 23" },
{ value: 430, date: "2022-09- 23" },
{ value: 660, date: "2024-01- 23" },
],
},
],
};
barEcharts.setOption(option);
},
};
</script>

<style>
.bars {
width: 500px;
height: 400px;
}
</style>

lenged属性
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
<template>
<!-- echarts容器,默认高度为0 -->
<div ref="barEcharts" class="bars"></div>
</template>

<script>
//将整个echarts库导入并赋值给名为echarts的变量
import * as echarts from "echarts";
export default {
name: "HomeEcharts",
mounted() {
//对于进行数据可视化的dom元素进行初始化echarts实例
let barEcharts = echarts.init(this.$refs.barEcharts);
//指定图表的配置项
let option = {
title: {
text: "分类",
},
xAxis: {
data: ["文学", "童话", "重生", "快穿", "玄幻"], //data类目数据
},
yAxis: {},
tooltip: {},
legend: {
show: true,//设置图例的开启或关闭
icon:"circle"//设置形状
},
series: [
//系列
{
name: "销量",
type: "bar", //echarts图表类型,bar--柱状图
//data的元素可以是一个对象,里面包含多个键值对
// data: [200, 456, 345, 178, 564],
data: [
{ value: 200, date: "2022-01- 23" },
{ value: 700, date: "2023-04- 23" },
{ value: 260, date: "2022-07- 23" },
{ value: 430, date: "2022-09- 23" },
{ value: 660, date: "2024-01- 23" },
],
},
],
};
barEcharts.setOption(option);
},
};
</script>

<style>
.bars {
width: 500px;
height: 400px;
}
</style>

柱形图更多的效果图
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
<template>
<!-- echarts容器,默认高度为0 -->
<div>
<div ref="barEcharts" class="bars"></div>
<div ref="bars01" class="bar01"></div>
</div>
</template>

<!-- <script>
//将整个echarts库导入并赋值给名为echarts的变量
import * as echarts from "echarts";
export default {
name: "HomeEcharts",
mounted() {
//对于进行数据可视化的dom元素进行初始化echarts实例
let barEcharts = echarts.init(this.$refs.barEcharts);
//指定图表的配置项
let option = {
title: {
text: "分类",
},
xAxis: {
data: ["文学", "童话", "重生", "快穿", "玄幻"], //data类目数据
},
yAxis: {},
tooltip: {},
legend: {
show: true,//设置图例的开启或关闭
icon:"circle"//设置形状
},
series: [
//系列
{
name: "销量",
type: "bar", //echarts图表类型,bar--柱状图
//data的元素可以是一个对象,里面包含多个键值对
// data: [200, 456, 345, 178, 564],
data: [
{ value: 200, date: "2022-01- 23" },
{ value: 700, date: "2023-04- 23" },
{ value: 260, date: "2022-07- 23" },
{ value: 430, date: "2022-09- 23" },
{ value: 660, date: "2024-01- 23" },
],
},
],
};
barEcharts.setOption(option);
},
};
</script> -->
<script>
import * as echarts from "echarts";
export default {
name: "HomeEcharts",
mounted() {
//初始化容器
let bars01 = echarts.init(this.$refs.bars01);
//设置轴数据
let xData = ["数学", "语文", "英语", "政治"];
let yData = [100, 90, 70, 50];
//完善配置项
let option = {
title: {
text: "学科成绩",
},
xAxis: {
data: xData,
type: "category", //当type不设置或设置成category显示的是名的名字,代表坐标轴是类目轴;如果值是value则是数据轴
},
yAxis: {},
/**
如果要设置成水平柱状图,则将xAxis设置成数据轴,yAxis设置成类目轴
yAxis: {
data: xData,
type: "category", //当type不设置或设置成category显示的是名的名字,代表坐标轴是类目轴;如果值是value则是数据轴
},
xAxis: {},
**/
series: [
{
name: "学科",
type: "bar",
data: yData,
//设置轴的样式
barWidth: 20,
// color:"red",//更换轴的颜色
//单独设置每个轴的颜色
itemStyle: {
normal: {
color: function (params) {
let colorList = ["#c23531", "#2f4554", "#61a0a8", "#91c7ae"];
return colorList[params.dataIndex]
},
},
},
//最大值/最小值
markPoint: {
data: [
{
type: "max",
name: "最大值",
},
{
type: "min",
name: "最小值",
},
],
},
//图标标线
markLine: {
data: [
{
type: "average", //平均值
name: "平均值",
},
],
},
},
],
};
//绘制图表
bars01.setOption(option);
},
};
</script>
<style>
.bars {
width: 500px;
height: 400px;
}
.bar01 {
width: 500px;
height: 400px;
border: 1px solid red;
}
</style>

饼状图——pie

基础配置:

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
<template>
<div ref="pie1"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "CakeEcharts",
mounted() {
this.pie();
},
methods: {
pie() {
let pie1 = echarts.init(this.$refs.pie1);
let data = [
{ value: 56, name: "衬衫" },
{ value: 156, name: "上衣" },
{ value: 90, name: "裤子" },
{ value: 200, name: "裙子" },
{ value: 121, name: "外套" },
];
pie1.setOption({
title: {
text: "销量",
left: "center",
},
legend:{
left:"left",
//图例的布局朝向
orient:"verical"
},
series: [{
name: "销量统计",
type: "pie",
data,
}],
});
},
},
};
</script>
<style>
div {
width: 500;
height: 500;
}
</style>

饼状图更多效果设置:

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
<template>
<div ref="pie1"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "CakeEcharts",
mounted() {
this.pie();
},
methods: {
pie() {
let pie1 = echarts.init(this.$refs.pie1);
let data = [
{ value: 56, name: "衬衫",itemStyle:{normal:{color:"rgba(11,11,11)"}}},
{ value: 156, name: "上衣",itemStyle:{normal:{color:"rgba(55,155,51)"}}},
{ value: 90, name: "裤子",itemStyle:{normal:{color:"rgba(177,17,12)"}}},
{ value: 200, name: "裙子",itemStyle:{normal:{color:"rgba(116,61,33)"}}},
{ value: 121, name: "外套",itemStyle:{normal:{color:"rgba(0,0,0)"}}},
];
pie1.setOption({
title: {
text: "销量",
left: "center",
},
legend:{
left:"left",
//图例的布局朝向
orient:"verical"
},
series: [{
name: "销量统计",
type: "pie",
data,
// radius:["40%","70%"],//设置饼图的半径,第一个是内半径,第二个外半径
//设置环形图的文本标签
label:{
show:true,
position:"inside"//outside 外侧展示 inside内展示 center中心展示
},
roseType:"area",//是否设置成南丁格尔图
//设置统一的样式
itemStyle:{
shadowBlur:200,
shadowColor:"reba(0,0,0,0.6)"
}
}],
});
},
},
};
</script>
<style>
div {
width: 500;
height: 500;
}
</style>
折线图——line

基础配置

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
<template>
<div ref="line"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "LineEcharts",
mounted() {
this.Line();
},
methods: {
Line() {
let line = echarts.init(this.$refs.line);
let Xdata = ["an", "bn", "cn", "dn", "en"];
let data = [109, 210, 199, 78, 11];
line.setOption({
title: {
text: "销量",
},
xAxis: {
type: "category",
data: Xdata,
},
yAxis: {
type: "value",
},
series: [{
name: "销量统计",
type: "line",
data,
}],
});
},
},
};
</script>
<style>
div {
width: 500;
height: 500;
}
</style>

更多样式配置

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
<template>
<div ref="line"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "LineEcharts",
mounted() {
this.Line();
},
methods: {
Line() {
let line = echarts.init(this.$refs.line);
let Xdata = ["an", "bn", "cn", "dn", "en"];
let data = [109, 210, 199, 78, 11];
line.setOption({
title: {
text: "销量",
},
xAxis: {
type: "category",
data: Xdata,
},
yAxis: {
type: "value",
},
series: [{
name: "销量统计",
type: "line", //折线图
//使拐角初变得平滑
smooth: true, //是否平滑显示
//区域填充样式,默认颜色为蓝色
areaStyle: {},
markPoint: {
data: [
{
type: "max",
name: "最大值",
},
{
type: "min",
name: "最小值",
},
],
},
markLine: {
data: [{ type: "average", name: "平均值" }],
},
data,
}],
});
},
},
};
</script>
<style>
div {
width: 500;
height: 500;
}
</style>

堆叠的折线图

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
<template>
<div ref="line"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "LineEcharts",
mounted() {
this.Line();
},
methods: {
Line() {
let line = echarts.init(this.$refs.line);
let Xdata = ["an", "bn", "cn", "dn", "en"];
let data1 = [109, 210, 199, 78, 11];
let data2 = [229, 210, 139, 54, 22];
let data3 = [245, 250, 459, 134, 43];
let data4 = [324, 120, 349, 178, 222];
line.setOption({
title: {
text: "销量",
},
xAxis: {
type: "category",
data: Xdata,
},
yAxis: {
type: "value",
},
series: [
{
name: "蔬菜",
type: "line",
data: data1,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
{
name: "水果",
type: "line",
data: data2,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
{
name: "饮料",
type: "line",
data: data3,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
{
name: "炸鸡",
type: "line",
data: data4,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
],
});
},
},
};
</script>
<style>
div {
width: 500;
height: 500;
}
</style>

散点图——scatter

应用场景:当存在大量的数据点时,散点图与折线图相似,而不同之处在于折线图通过将点或数据点相连来显示每一个变化。
例子:

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
<template>
<div ref="scatter" id="scatter"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "ScatterEcharts",
data() {
return {};
},
mounted() {
this.fn();
},
methods: {
fn() {
let scat = echarts.init(this.$refs.scatter);
scat.setOption({
title: {
text: "散点图",
},
xAxis: {},
yAxis: {},
series: [
{
data: [
[10.0, 8.04],
[8.07, 6.95],
[13.0, 7.58],
[9.05, 8.81],
[11.0, 8.33],
[14.0, 7.66],
[13.4, 6.81],
[10.0, 6.33],
[14.0, 8.96],
[12.5, 6.82],
[9.15, 7.2],
[11.5, 7.2],
[3.03, 4.23],
[12.2, 7.83],
[2.02, 4.47],
[1.05, 3.33],
[4.05, 4.96],
[6.03, 7.24],
[12.0, 6.26],
[12.0, 8.84],
[7.08, 5.82],
[5.02, 5.68],
],
name: "散点",
type: "scatter",
symbolSize: 20,
// color:'red'//设置圆点的颜色
//线性渐变效果——前四个参数分别时x0,y0,x2,y2,范围从0 - 1,相当于在图形包围盒中的百分比,如果globalColor为"true",则该四个位置是绝对的像素位
color: {
type: "linear", //线性渐变;radial径向渐变
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{ offset: 0, color: "red" },//0%处的颜色
{ offset: 1, color: "blue" },//100%处的颜色
],
},
},
],
});
},
},
};
</script>
<style scoped>
#scatter {
width: 500px;
height: 500px;
}
</style>


grid配置项

grid为直角坐标系内绘图网格。可以在网格上面绘制折线图,柱状图,散点图(气泡图)也就是设图标容器的距离,样式。
例子:

  • show:是否显示直角坐标系网格——————————————值:true?false
  • left,right,top,bootom:设置图表离容器相对应一侧的距离————值:number?百分比
  • borderColor设置边框颜色
  • backgroundColor设置图表的背景颜色
    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
    <template>
    <div ref="scatter" id="scatter"></div>
    </template>
    <script>
    import * as echarts from "echarts";
    export default {
    name: "ScatterEcharts",
    data() {
    return {};
    },
    mounted() {
    this.fn();
    },
    methods: {
    fn() {
    let scat = echarts.init(this.$refs.scatter);
    scat.setOption({
    title: {
    text: "散点图",
    },
    xAxis: {},
    yAxis: {},
    grid:{
    left:'15%',
    right:'15%',
    top:'15%',
    bottom:'15%',
    show:"true",
    borderColor:'yellow',
    backgroundColor:'pink'
    },
    series: [
    {
    data: [
    [10.0, 8.04],
    [8.07, 6.95],
    [13.0, 7.58],
    [9.05, 8.81],
    [11.0, 8.33],
    [14.0, 7.66],
    [13.4, 6.81],
    [10.0, 6.33],
    [14.0, 8.96],
    [12.5, 6.82],
    [9.15, 7.2],
    [11.5, 7.2],
    [3.03, 4.23],
    [12.2, 7.83],
    [2.02, 4.47],
    [1.05, 3.33],
    [4.05, 4.96],
    [6.03, 7.24],
    [12.0, 6.26],
    [12.0, 8.84],
    [7.08, 5.82],
    [5.02, 5.68],
    ],
    name: "散点",
    type: "scatter",
    symbolSize: 20,
    // color:'red'//设置圆点的颜色
    //线性渐变效果——前四个参数分别时x0,y0,x2,y2,范围从0 - 1,相当于在图形包围盒中的百分比,如果globalColor为"true",则该四个位置是绝对的像素位
    color: {
    type: "linear", //线性渐变;radial径向渐变
    x: 0,
    y: 0,
    x2: 0,
    y2: 1,
    colorStops: [
    { offset: 0, color: "red" },//0%处的颜色
    { offset: 1, color: "blue" },//100%处的颜色
    ],
    },
    },
    ],
    });
    },
    },
    };
    </script>
    <style scoped>
    #scatter {
    width: 500px;
    height: 500px;
    border: 1px solid black;
    }
    </style>

k线图——candlestick

定义:K线图又称为蜡烛图或阴阳图,是一种用于显示股票或其他证券价格变化的图表。K线是以分析周期的开盘价,最高价,最低价和收盘价绘制成的。它代表的是一段时间内股票价格的涨跌情况。

雷达图——radar
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
<template>
<div ref="radar"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "RadarEchart",
mounted() {
this.rradar();
},
methods: {
rradar() {
let radar = echarts.init(this.$refs.radar);
radar.setOption({
title: {
text: "雷达图",
},
legend: {
data: ["Allocated Budget", "Actual Spending"],
},
radar: {
// shape: 'circle',
indicator: [
{ name: "Sales", max: 6500 },
{ name: "Administration", max: 16000 },
{ name: "Information Technology", max: 30000 },
{ name: "Customer Support", max: 38000 },
{ name: "Development", max: 52000 },
{ name: "Marketing", max: 25000 },
],
//坐标系的起始角度
startAngle: 180,
},
series: [
{
name: "Budget vs spending",
type: "radar",
data: [
{
value: [4200, 3000, 20000, 35000, 50000, 18000],
name: "Allocated Budget",
},
{
value: [5000, 14000, 28000, 26000, 42000, 21000],
name: "Actual Spending",
},
],
},
],
});
},
},
};
</script>
<style scoped>
div {
width: 500px;
height: 500px;
}
</style>

漏斗图——funnel
仪表图——gauge
关系图——graph
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
<template>
<div ref="graph"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "GraphEchart",
data() {
return {
list: [
{
name: "唐僧",
id: "1",
symbolSize: 20, //设置圆点大小
},
{
name: "孙悟空",
id: "2",
symbolSize: 20,
},
{
name: "猪八戒",
id: "3",
symbolSize: 20,
},
{
name: "沙和尚",
id: "4",
symbolSize: 20,
},
{
name: "菩提老祖",
id: "5",
symbolSize: 20,
},
],
nums: [
{
source: "1", //源节点名称
target: "2",
relation: {
name: "师徒",
},
},
{
source: "1", //源节点名称
target: "3",
relation: {
name: "师徒",
},
},
{
source: "1", //源节点名称
target: "4",
relation: {
name: "师徒",
},
},
{
source: "2", //源节点名称
target: "5",
relation: {
name: "师徒",
},
},
{
source: "2", //源节点名称
target: "3",
relation: {
name: "同门",
},
},
{
source: "2", //源节点名称
target: "4",
relation: {
name: "同门",
},
},
{
source: "3", //源节点名称
target: "4",
relation: {
name: "同门",
},
},
],
};
},
mounted() {
this.graphs();
},
methods: {
graphs() {
let graphk = echarts.init(this.$refs.graph);
graphk.setOption({
title: {
text: "西游记关系图",
},
series: [
{
name: "关系图",
type: "graph",
data: this.list,
layout: "force", //引导布局force circular:采用环形布局 none:不采用任何布局,使用节点中提供的x,y作为节点的位置
itemStyle: {
color: "#95dcb2",
},
label: {
show: true,
position: "bottom",
distance: 6, //距离图形的位置
fontSize: 14,
align: "center",
},
//设置间距
force: {
repulsion: 100,
qravity: 0.01,
edgeLength: 200,
},
links: this.nums,//节点之间的关系
//设置标签内容,样式
edgeLabel:{
show:true,
position:'middle',
formatter:(params)=>{
return params.data.relation.name
}
}
},
],
});
},
},
};
</script>
<style scoped>
div {
width: 500px;
height: 500px;
}
</style>

数据区域缩放——dataZoom
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
<template>
<div ref="line"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "LineEcharts",
mounted() {
this.Line();
},
methods: {
Line() {
let line = echarts.init(this.$refs.line);
let Xdata = ["an", "bn", "cn", "dn", "en"];
let data1 = [109, 210, 199, 78, 11];
let data2 = [229, 210, 139, 54, 22];
let data3 = [245, 250, 459, 134, 43];
let data4 = [324, 120, 349, 178, 222];
line.setOption({
title: {
text: "销量",
},
xAxis: {
// type: "category",
data: Xdata,
},
yAxis: {
type: "value",
},
grid: {
left: "15%",
right: "15%",
top: "15%",
bottom: "15%",
show: "true",
borderColor: "yellow",
backgroundColor: "pink",
},
series: [
{
name: "蔬菜",
type: "line",
data: data1,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
{
name: "水果",
type: "line",
data: data2,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
{
name: "饮料",
type: "line",
data: data3,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
{
name: "炸鸡",
type: "line",
data: data4,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
],
//设置数据的缩放
dataZoom: [
{
type: "slider",//滑动条形数据区域缩放组件 inside内置型数据区域缩放组件
xAxisIndex: 0,
fliterMode: "none", //不需要过滤
},
{
type: "slider",
yAxisIndex: 0,
fliterMode: "none", //不需要过滤
},
],
});
},
},
};
</script>
<style scoped>
div {
width: 500px;
height: 500px;
}
</style>

树形图——tree

因为树形图上存在叶子节点,所以可以单独对叶子节点做额外的配置

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
<template>
<div ref="tree"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "TreeEcharts",
data() {
return {
list: [
{
name: "商品",
children: [
{
name: "服饰",
children: [
{
name: "上衣",
},
{ name: "鞋子" },
],
},
{
name: "电器",
children: [
{
name: "冰箱",
},
{ name: "微波炉" },
],
},
],
},
],
};
},
mounted() {
this.fn();
},
methods: {
fn() {
let tree = echarts.init(this.$refs.tree);
tree.setOption({
title: {
text: "树形图",
},
tooltip: {
trigger: "item",
},
series: [
{
type: "tree",
data: this.list,
symbolSize: 15,//设置标记的大小
//设置布局方向
orient:'TB',//默认值是LR以为从左(Left)到右(Right)
label: {//描述了每个节点所对应的文本标签的样式
position: "left",
verticalAlign: "middle",
align: "center",
},
//叶子节点的特殊配置
leaves:{
label:{//描述了叶子节点所对应的文本标签的样式
position:"right",
verticalAlign:'middle',
align:"left"
}
}
},
],
});
},
},
};
</script>
<style scoped>
div {
width: 500px;
height: 500px;
}
</style>

dataset——数据集组件

可可实现对数据的单独管理,被多个组件复用,对数据快速排序等等

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
<template>
<div ref="conten"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
data() {
return {};
},
mounted() {
let datas = echarts.init(this.$refs.conten);
datas.setOption({
title: {
text: "排序后",
},
dataset: [
{
//设置每一个维度的信息
dimensions: ["类别", "数量"],
//数据
source: [
["水果", 100],
["蔬菜", 245],
["数码", 567],
["服饰", 432],
["文具", 54],
],
},
{
transform: {
type: "sort",
config: {
dimension: "数量",
order: "desc", //基于数量按降序排序
},
},
},
],
xAxis: {
type: "category",
axisLabel: {
//坐标轴刻度标签相关设置
interval: 0, //坐标轴刻度标签的显示问题,在类目轴中有效
rotate: 30, //刻度标签旋转角度
},
},
yAxis: {},
series: [
{
type: "bar",
encode: {
x: "类别",
y: "数量",
},
datasetIndex: 1, //选择数据集中的第几个元素
},
],
});
},
};
</script>
<style scoped>
div {
width: 500px;
height: 500px;
}
</style>

echarts主题设置
  • 内置主题

    1
    2
    //echarts的内置主题分为light和dark两种,在初始化的时候设置——echarts.init["图表的dom设置","主题设置"]
    let datas = echarts.init(this.$refs.conten,"dark");
  • 自定义主题

  • image.png选择主题下载,可以点击定制主题,然后将定制好的内容下载或复制配置放在静态资源的js文件中,导出相关配置,在图表文件中应用,将原来的内置主题换成存放自定义主题配置的变量。

中国地图展示
  1. 准备echarts基本结构
  2. 设置中国地图的矢量数据创建js文件(在其中创建变量接收json数据并暴露)

地图数据下载地址:https://datav.aliyun.com/portal/school/atlas/area_selector

  1. 在组件中获取地图的矢量数据(引用json数据)
  2. 使用地图数据创建地图
    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
    <template>
    <div ref="china"></div>
    </template>
    <script>
    import * as echarts from "echarts";
    //引入地图数据
    import {mapData} from "@/assets/data"
    export default {
    name: "MapChina",
    data() {
    return {};
    },
    mounted() {
    let china = echarts.init(this.$refs.china);
    //注册当前使用的地图名
    echarts.registerMap("中国地图",mapData)
    china.setOption({
    geo:{//地理坐标组件,用于地图的绘制
    type:'map',
    map:"中国地图",//与上方注册的地图名一致
    //设置平移和缩放的效果
    roam:true,//允许平移和缩放
    zoom:5,//设置初始化时地图的缩放比例
    center:[113.945234,35.286109]//设置初始化时中心点的位置
    }
    });
    },
    };
    </script>
    <style scoped>
    div {
    width: 500px;
    height: 500px;
    }
    </style>

省份地图展示

与上方的操作步骤相同

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
<template>
<div ref="china"></div>
</template>
<script>
import * as echarts from "echarts";
//引入地图数据
import { heNan } from "@/assets/data";
export default {
name: "HeNan",
data() {
return {};
},
mounted() {
let china = echarts.init(this.$refs.china);
//注册当前使用的地图名
echarts.registerMap("河南省图", heNan);
china.setOption({
geo: {
//地理坐标组件,用于地图的绘制
type: "map",
map: "河南省图", //与上方注册的地图名一致
//设置平移和缩放的效果
roam: true, //允许平移和缩放
// zoom:5,//设置初始化时地图的缩放比例
// center:[113.945234,35.286109]//设置初始化时中心点的位置
//设置文字的提示信息,当show为true的时候文字提示信息显示有效
label: {
show:true,
color:"#ff6600",
fontSize:10
},
itemStyle:{
areaColor:"#ff6600"
}
},
});
},
};
</script>
<style scoped>
div {
width: 500px;
height: 500px;
}
</style>

设置地图标记位置
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
<template>
<div ref="china"></div>
</template>
<script>
import * as echarts from "echarts";
//引入地图数据
import { mapData } from "@/assets/data";
export default {
name: "MapChina",
data() {
return {};
},
mounted() {
let china = echarts.init(this.$refs.china);
//注册当前使用的地图名
echarts.registerMap("中国地图", mapData);
china.setOption({
geo: {
//地理坐标组件,用于地图的绘制
type: "map",
map: "中国地图", //与上方注册的地图名一致
//设置平移和缩放的效果
roam: true, //允许平移和缩放
zoom: 5, //设置初始化时地图的缩放比例
center: [113.945234, 35.286109], //设置初始化时中心点的位置
},
//利用散点图设置标识点,在实际应用中可以标识一个人的位置
series: [
{
type: "scatter",
data: [
{
name: "北京市", //数据项的名称
value: [116.46, 39.92],
},
],
//因为不是存在在原来常规的带有x轴和y轴的坐标系中,所以要设置一个基准,在这里以地图坐标为基准
coordinateSystem: "geo",
symbolSize: 20,
},
{
type: "effectScatter", //带有涟漪效果的散点图
data: [
{
name: "新乡市", //数据项的名称
value: [113.945234, 35.286109],
},
],
//因为不是存在在原来常规的带有x轴和y轴的坐标系中,所以要设置一个基准,在这里以地图坐标为基准
coordinateSystem: "geo",
//设置涟漪效果相关的配置
rippleEffect: {
number: 2, //波纹的数量
scale: 4,//设置波纹的大小
},
itemStyle: {
color: "red",
},
},
],
});
},
};
</script>
<style scoped>
div {
width: 500px;
height: 500px;
}
</style>

图表自适应

采用window自带的resize设置

1
2
3
4
5
window.addEventListener("resize",()=>{
console.log("监听页面浏览器的变化");
let line = echarts.init(this.$refs.line)
line.resize()
})

例子:

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
<template>
<div ref="line"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
name: "LineEcharts",
mounted() {
this.Line();
window.addEventListener("resize",()=>{
console.log("监听页面浏览器的变化");
let line = echarts.init(this.$refs.line)
line.resize()
})
},
methods: {
Line() {
let line = echarts.init(this.$refs.line);
let Xdata = ["an", "bn", "cn", "dn", "en"];
let data1 = [109, 210, 199, 78, 11];
let data2 = [229, 210, 139, 54, 22];
let data3 = [245, 250, 459, 134, 43];
let data4 = [324, 120, 349, 178, 222];
line.setOption({
title: {
text: "销量",
},
xAxis: {
// type: "category",
data: Xdata,
},
yAxis: {
type: "value",
},
grid: {
left: "15%",
right: "15%",
top: "15%",
bottom: "15%",
show: "true",
borderColor: "yellow",
backgroundColor: "pink",
},
series: [
{
name: "蔬菜",
type: "line",
data: data1,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
{
name: "水果",
type: "line",
data: data2,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
{
name: "饮料",
type: "line",
data: data3,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
{
name: "炸鸡",
type: "line",
data: data4,
areaStyle: {},
stack: "num", //数据堆叠,同类目轴上系列配置相同的stack值可以堆叠放置
//选中高亮状态
emphasis: {
focus: "series", //聚焦当前的高亮区
},
},
],
//设置数据的缩放
dataZoom: [
{
type: "slider",//滑动条形数据区域缩放组件 inside内置型数据区域缩放组件
xAxisIndex: 0,
fliterMode: "none", //不需要过滤
},
{
type: "slider",
yAxisIndex: 0,
fliterMode: "none", //不需要过滤
},
],
});
},
},
};
</script>
<style scoped>
div {
width: 100%;
height: 500px;
}
</style>

如何模拟数据——json-server
  1. 全局下载 json-server

    1
    npm install json -g json-server
  2. 新建文件夹与文件用来模拟数据

    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
    {
    "one": [
    {
    "value": 56,
    "name": "衬衫",
    "itemStyle": { "normal": { "color": "rgba(11,11,11)" } }
    },
    {
    "value": 156,
    "name": "上衣",
    "itemStyle": { "normal": { "color": "rgba(55,155,51)" } }
    },
    {
    "value": 90,
    "name": "裤子",
    "itemStyle": { "normal": { "color": "rgba(177,17,12)" } }
    },
    {
    "value": 200,
    "name": "裙子",
    "itemStyle": { "normal": { "color": "rgba(116,61,33)" } }
    },
    {
    "value": 121,
    "name": "外套",
    "itemStyle": { "normal": { "color": "rgba(0,0,0)" } }
    }
    ]
    }
  3. 启动json-server

注意:启动时目录需要是当前json文件所在的文件夹下

1
json-server --watch data.json --port 8888//port后是设置的端口号
  1. 下载axios

    1
    npm install --save axios
  2. 获取模拟数据

  3. 设置动画

pie1.showLoading()加载动画
pie1.hideLoading()关闭动画

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
<template>
<div ref="pie1"></div>
</template>
<script>
import axios from "axios";
import * as echarts from "echarts";
export default {
name: "CakeEcharts",
data() {
return {
pieData: [],
};
},
mounted() {
let pie1 = echarts.init(this.$refs.pie1);
pie1.showLoading()
this.linkData().then((res) => {
console.log("res", res);
pie1.hideLoading()
pie1.setOption({
title: {
text: "销量",
left: "center",
},
legend: {
left: "left",
//图例的布局朝向
orient: "verical",
},
series: {
name: "销量统计",
type: "pie",
data: this.pieData,
// radius:["40%","70%"],//设置饼图的半径,第一个是内半径,第二个外半径
//设置环形图的文本标签
label: {
show: true,
position: "inside", //outside 外侧展示 inside内展示 center中心展示
},
roseType: "area", //是否设置成南丁格尔图
//设置统一的样式
itemStyle: {
shadowBlur: 200,
shadowColor: "reba(0,0,0,0.6)",
},
},
});
});
},
methods: {
async linkData() {
let data = await axios({ url: "http://localhost:8888/one" });
this.pieData = data.data;
return data.data;
},
},
};
</script>
<style scoped>
div {
width: 500px;
height: 500px;
}
</style>