ECharts配置语法
# ECharts 配置语法
本章节我们将为大家介绍使用 ECharts 生成图表的一些配置。
# 1. 创建HTML页面
创建一个 HTML 页面,引入echarts.min.js
。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- 引入 ECharts 文件 -->
<script src="https://cdn.staticfile.org/echarts/5.1.2/echarts.min.js"></script>
</head>
</html>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2. 为 ECharts 准备一个具备高宽的 DOM 容器
在上面的</head>
标签后面增加以下部分。
<body>
<!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
<div id="main" style="width: 600px;height:400px;"></div>
</body>
1
2
3
4
2
3
4
然后就可以通过echarts.init
方法初始化一个 echarts 实例并通过 setOption
方法生成一个简单的柱状图,下面是完整代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.js"></script>
</head>
<body>
<!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
<div id="main" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
</body>
</html>
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
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
这样你的第一个图表就诞生了!
https://echarts.apache.org/examples/zh/editor.html?c=doc-example/getting-started (opens new window)
# 3. 配置信息说明
在上面的示例成功后,我们回过头来看一下配置的部分,其中以var option
开头的部分就是图表设置选项的编写部分。我们重点来关注一下这部分的结构。这部分的内容是以json
规格化设置的,主要目录结构参考下方的注释说明。
var option = {
//标题:为图表配置标题
title: {
text: '第一个 ECharts 实例'
},
//提示信息
tooltip: {},
/*****
图例组件展现了不同系列的标记(symbol),
颜色和名字。可以通过点击图例控制哪些系列不显示。
******/
legend: {
//数据源字段名称
data:['销量']
},
//配置要在 X 轴显示的项
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
//配置要在 Y 轴显示的项
yAxis: {},
//每个系列通过 type 决定自己的图表类型
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
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
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
每个系列通过 type 决定自己的图表类型:
type: 'bar'
:柱状/条形图type: 'line'
:折线/面积图type: 'pie'
:饼图type: 'scatter'
:散点(气泡)图type: 'effectScatter'
:带有涟漪特效动画的散点(气泡)type: 'radar'
:雷达图type: 'tree'
:树型图type: 'treemap'
:树型图type: 'sunburst'
:旭日图type: 'boxplot'
:箱形图type: 'candlestick'
:K线图type: 'heatmap'
:热力图type: 'map'
:地图type: 'parallel'
:平行坐标系的系列type: 'lines'
:线图type: 'graph'
:关系图type: 'sankey'
:桑基图type: 'funnel'
:漏斗图type: 'gauge'
:仪表盘type: 'pictorialBar'
:象形柱图type: 'themeRiver'
:主题河流type: 'custom'
:自定义系列