Preface
In this article, we will learn how to implement a line chart
Line chart features
Line charts are more commonly used to present data trends over time
Line chart implementation steps
The most basic code structure of ECharts
Prepare data for x-axis
Prepare data for y-axis
Prepare option, set the value of type in series to: line
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< meta http-equiv = " X-UA-Compatible" content = " ie=edge" >
< title> ECharts-折线图</ title>
< script src = " https://cdn.jsdelivr.net/npm/[email protected] /dist/echarts.min.js" > </ script>
</ head>
< body>
< div id = ' app' style = " width : 600px; height : 400px" > </ div>
< script>
var myCharts = echarts. init ( document. getElementById ( 'app' ) )
var xDataArr = [ '张三' , '李四' , '王五' , '闰土' , '小明' , '茅台' , '球球' ]
var yDataArr = [ 88 , 92 , 63 , 77 , 94 , 80 , 72 ]
var option = {
xAxis : {
type : 'category' ,
data : xDataArr
} ,
yAxis : {
type : 'value'
} ,
series : [
{
type : 'line' ,
data : yDataArr
}
]
}
myCharts. setOption ( option)
</ script>
</ body>
</ html>
Effect
Common effects of line charts
mark
markPoint: maximum/minimum value
markPoint : {
data : [
{
type : 'max' ,
name : '最大值'
} , {
type : 'min' ,
name : '最小值'
}
]
}
markLine : {
data : [
{
type : 'average' ,
name : '平均值'
}
]
}
markArea : {
data : [
[ {
xAxis : '2月' } , {
xAxis : '3月' } ] ,
[ {
xAxis : '8月' } , {
xAxis : '9月' } ]
]
}
Line control
var option = {
series : [
{
... ...
smooth : true
}
]
}
var option = {
series : [
{
... ...
lineStyle : {
color : 'green' ,
type : 'dashed'
}
}
]
}
fill style
var option = {
series : [
{
type : 'line' ,
data : yDataArr,
areaStyle : {
color : 'pink'
}
}
]
}
close to the edge
boundaryGap: is set to the x-axis, so that the starting point starts from the 0 coordinate of the x-axis
var option = {
xAxis : {
type : 'category' ,
data : xDataArr,
boundaryGap : false
}
}
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< meta http-equiv = " X-UA-Compatible" content = " ie=edge" >
< title> ECharts-折线图</ title>
< script src = " https://cdn.jsdelivr.net/npm/[email protected] /dist/echarts.min.js" > </ script>
</ head>
< body>
< div id = ' app' style = " width : 600px; height : 400px" > </ div>
< script>
var myCharts = echarts. init ( document. getElementById ( 'app' ) )
var xDataArr = [ '1月' , '2月' , '3月' , '4月' , '5月' , '6月' , '7月' , '8月' , '9月' ,
'10月' , '11月' , '12月' ]
var yDataArr = [ 3000 , 2800 , 900 , 1000 , 800 , 700 , 1400 , 1300 , 900 , 1000 , 800 ,
600 ]
var yDataArr1 = [ 3005 , 3003 , 3001 , 3002 , 3009 , 3007 , 3003 , 3001 , 3005 ,
3004 , 3001 , 3009 ]
var option = {
xAxis : {
type : 'category' ,
data : xDataArr,
boundaryGap : false
} ,
yAxis : {
type : 'value' ,
scale : true
} ,
series : [
{
type : 'line' ,
data : yDataArr,
scale : true ,
markPoint : {
data : [
{
type : 'max' ,
name : '最大值'
} , {
type : 'min' ,
name : '最小值'
}
]
} ,
markLine : {
data : [
{
type : 'average' ,
name : '平均值'
}
]
} ,
markArea : {
data : [
[ {
xAxis : '2月' } , {
xAxis : '3月' } ] ,
[ {
xAxis : '8月' } , {
xAxis : '9月' } ]
]
} ,
smooth : true ,
lineStyle : {
color : 'green' ,
type : 'dashed'
} ,
areaStyle : {
color : 'pink'
}
}
]
}
myCharts. setOption ( option)
</ script>
</ body>
</ html>
Scale, get out of 0 value scale
If the difference between each set of data is small and much larger than 0, then this may happen.
scale: should be configured for the y-axis
var option = {
yAxis : {
type : 'value' ,
scale : true
}
}
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< meta http-equiv = " X-UA-Compatible" content = " ie=edge" >
< title> ECharts-折线图</ title>
< script src = " https://cdn.jsdelivr.net/npm/[email protected] /dist/echarts.min.js" > </ script>
</ head>
< body>
< div id = ' app' style = " width : 600px; height : 400px" > </ div>
< script>
var myCharts = echarts. init ( document. getElementById ( 'app' ) )
var xDataArr = [ '1月' , '2月' , '3月' , '4月' , '5月' , '6月' , '7月' , '8月' , '9月' ,
'10月' , '11月' , '12月' ]
var yDataArr = [ 3000 , 2800 , 900 , 1000 , 800 , 700 , 1400 , 1300 , 900 , 1000 , 800 ,
600 ]
var yDataArr1 = [ 3005 , 3003 , 3001 , 3002 , 3009 , 3007 , 3003 , 3001 , 3005 ,
3004 , 3001 , 3009 ]
var option = {
xAxis : {
type : 'category' ,
data : xDataArr,
boundaryGap : false
} ,
yAxis : {
type : 'value' ,
scale : true
} ,
series : [
{
type : 'line' ,
data : yDataArr1,
smooth : true ,
}
]
}
myCharts. setOption ( option)
</ script>
</ body>
</ html>
Effect
stacked chart
The stacked chart means that after the series on the same category axis are configured with the same stack value, the value of the latter series will be added to the value of the previous series < /span>
var option = {
series : [
{
type : 'line' ,
data : yDataArr1,
stack : 'all'
} ,
{
type : 'line' ,
data : yDataArr2,
stack : 'all'
}
]
}
Effect
Complete code
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< meta http-equiv = " X-UA-Compatible" content = " ie=edge" >
< title> ECharts-折线图</ title>
< script src = " https://cdn.jsdelivr.net/npm/[email protected] /dist/echarts.min.js" > </ script>
</ head>
< body>
< div id = ' app' style = " width : 600px; height : 400px" > </ div>
< script>
var myCharts = echarts. init ( document. getElementById ( 'app' ) )
var xDataArr = [ '周一' , '周二' , '周三' , '周四' , '周五' , '周六' , '周日' ]
var yDataArr1 = [ 120 , 132 , 101 , 134 , 90 , 230 , 210 ]
var yDataArr2 = [ 20 , 82 , 191 , 94 , 290 , 330 , 310 ]
var option = {
xAxis : {
type : 'category' ,
data : xDataArr
} ,
yAxis : {
type : 'value' ,
scale : true
} ,
series : [
{
type : 'line' ,
data : yDataArr1,
stack : 'all' ,
areaStyle : {
color : 'pink'
}
} ,
{
type : 'line' ,
data : yDataArr2,
stack : 'all' ,
areaStyle : {
color : 'blue'
}
}
]
}
myCharts. setOption ( option)
myCharts. setOption ( option)
</ script>
</ body>
</ html>