[Echarts] Detailed explanation of line chart/area chart! ! ! Come take a look! !

Realization of line chart/area chart

Look at the effect first

Insert picture description here

File Directory

Insert picture description here

Get Echarts

Insert picture description here

Introduce Echarts

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <!-- 引入 ECharts 文件 -->
    <script src="../incubator-echarts-5.0.0-alpha.2/dist/echarts.min.js"></script>
</head>
</html>

Draw a chart

Before drawing, we need to prepare a DOM container with height and width for ECharts

<body style="background: black;">
    <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
    <div id="main" style="width: 835px; height: 670px"></div>
</body>

Then you can initialize an echarts instance through the echarts.init method and generate a line chart/area chart through the setOption method

 <script type="text/javascript">
   // 基于准备好的dom, 初始化echarts实例
   var myChart = echarts.init(document.getElementById('main'));
   // 使用刚指定的配置项和数据显示图表
   myChart.setOption(option)
 </script>

Code step split

var option = {} // Specify the configuration and data of the icon
data source
 // 数据集
var dataList = [
  {
    
    date: '08/01', value: 16},
  {
    
    date: '08/02', value: 56},
  {
    
    date: '08/03', value: 46},
  {
    
    date: '08/04', value: 11},
  {
    
    date: '08/05', value: 116},
  {
    
    date: '08/06', value: 146},
  {
    
    date: '08/07', value: 116},
  {
    
    date: '08/08', value: 56},
  {
    
    date: '08/09', value: 36},
  {
    
    date: '08/10', value: 129},
  {
    
    date: '08/11', value: 145},
  {
    
    date: '08/12', value: 115},
  {
    
    date: '08/13', value: 105},
  {
    
    date: '08/14', value: 45},
  {
    
    date: '08/15', value: 87},
  {
    
    date: '08/16', value: 82},
  {
    
    date: '08/17', value: 24},
  {
    
    date: '08/18', value: 78},
  {
    
    date: '08/19', value: 56},
  {
    
    date: '08/20', value: 52},
  {
    
    date: '08/21', value: 78},
  {
    
    date: '08/26', value: 123},
  {
    
    date: '08/31', value: 130}
]

var xKeys = dataList.map((item) => item.date);
var values = dataList.map((item) => item.value);
title
title: {
    
    
  text: '球队近30日盈利情况',
  show: true,
  textStyle: {
    
    
    color: '#fff',
    fontSize: '36',
    fontFamily: 'Microsoft YaHei',
    fontWeight: 400
  },
  top: '42',
  left: '40'
}

Insert picture description here

legend (Only displayed in conjunction with series)
legend: {
    
    
  top: 150,
  right: 0,
  z: 4,
  textStyle: {
    
    
    fontSize: "24px",
    fontFamily:
      "Microsoft YaHei",
    fontWeight: 400,
    color: "#c2cbf2",
  },
}

Insert picture description here

grid
grid: {
    
    
  left: 70,
  top: 200,
  right: 40,
  bottom: 80
}

Insert picture description here

xAxis (the x axis in the rectangular coordinate system grid)
xAxis: [
  {
    
    
    type: 'category',
    data: xKeys,
    color:'#323e52',
    axisLabel: {
    
    
      margin: 20,
      interval: 'auto',
      textStyle: {
    
    
        fontSize: 24,
        fontFamily: 'Microsoft YaHei',
        fontWeight: 400,
        textAlign: 'center',
        color: '#c2cbf2'
      }
    },
    position: 'bottom',
    axisLine: {
    
    
      lineStyle: {
    
    
        color: '#b7ccff',
        type: 'solid'
      }
    },
    splitLine: {
    
    
      show: false
    }
  }
]

Insert picture description here

yAxis (the y axis in the rectangular coordinate system grid)
yAxis: {
    
    
  type: 'value',
  silent: true,
  interval: 30,
  min: 0,
  max: 150,
  axisLabel: {
    
    
    textStyle: {
    
    
      fontSize: 24,
      fontFamily: 'Microsoft YaHei',
      fontWeight: 400,
      textAlign: 'right',
      color: '#c2cbf2'
    }
  },
  axisLine: {
    
    
    show: false
  },
  axisTick: {
    
    
    show: false
  },
  splitLine: {
    
    
    show: true,
    lineStyle: {
    
    
      color: '#232842',
      type: 'solid'
    }
  }
}

Insert picture description here

series (series list. Each series determines its own chart type by type)
series: [
  {
    
    
    name: '盈利资金(万)',
    type: 'line', // 折现/面积图
    data: values,
    itemStyle: {
    
    
      color: '#24def3'
    },
    symbol: 'emptyCircle', // 几何圆
    symbolSize: 10,
    areaStyle: {
    
     // 区域填充样式
      color: {
    
     // 填充的颜色 // 线性渐变,前四个参数分别是 x0, y0, x2, y2, 范围从 0 - 1,相当于在图形包围盒中的百分比,如果 globalCoord 为 `true`,则该四个值是绝对的像素位置
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
    
    
            offset: 0,
            color: '#25eaff', // 0% 处的颜色
          },
          {
    
    
            offset: 1,
            color: '#121f35' // 100% 处的颜色
          }
        ],
        global: false, // 缺省为 false
      }
    },
    xAxisIndex: 0 
  }
]

Insert picture description here


Insert picture description here


Insert picture description here
Insert picture description here

Modify some issues (bug)

Example: Question One

grid: {
    
    
          left: 'auto',
          top: 346, 
          right: 'auto',
          containLabel:true,
          bottom: 20,
        },
        
//grid这样写,左右都写成auto,还要加containLabel,不然就有可能挡住y轴的标签

Insert picture description here
Insert picture description here

Insert picture description here


Example: Question Two

yAxis里面的max不能写死,不然最大值永远不会变,比如max=100,实际的值超过是200,那就会挡住

Insert picture description here
Insert picture description here


Insert picture description here

I believe that after reading this article, you should be able to build a line chart/area chart. If you find it useful, give me a compliment! ! !

Guess you like

Origin blog.csdn.net/weixin_43352901/article/details/108489921