Echarts之series动态渲染多个折线图/面积图

由于工作中遇到的图表特别多,每次用过配置项总是想不起来,在这里记录一下,希望也可以帮到路过的你。话不多说我们上图:

在这里插入图片描述

1、根据后端返回数据(数组)动态设置series

const socSeries = this.setSeries(res.result.socFollowingList)
setSeries (data) {
      const colors = [
        {
          zero: '#91cc75',
          one: '#D3E5CB'
        },
        {
          zero: '#5470c6',
          one: '#C4CBED'
        },
        {
          zero: '#ee6666',
          one: '#E3B7B7'
        },
        {
          zero: '#7a22ef',
          one: '#C9B7E3'
        },
        {
          zero: '#3bbcd9',
          one: '#B9E0E8'
        }
      ]
      var series = []
      var series_data = data
      series_data.forEach((e, index) => {
        let value = e.list.map(item => {
          return item.val
        })
        var item = {
          name: e.name,
          data: value,
          type: 'line',
          symbol: 'none',
          itemStyle: {
            color: colors[index].zero
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colors[index].zero
              },
              {
                offset: 1,
                color: colors[index].one
              }
            ])
          },
        }
        series.push(item)
      })
      return series
    },

2、根据得到的多组series数据进行画图

this.getPowerTracking(timeData, socSeries, power, strategy)
getPowerTracking (timeData, socSeries, power, strategy) {
      let myChartDom = document.getElementById('chart-power')
      let myChart = echarts.init(myChartDom)
      var option

      option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            animation: false
          }
        },
        legend: {
          // data: ['SOC', '功率'],
          right: 50
        },
        axisPointer: {
          link: [
            {
              xAxisIndex: 'all'
            }
          ]
        },
        dataZoom: [
          {
            show: true,
            realtime: true,
            start: 30,
            end: 70,
            xAxisIndex: [0, 1]
          },
          {
            type: 'inside',
            realtime: true,
            start: 30,
            end: 70,
            xAxisIndex: [0, 1]
          }
        ],
        grid: [
          {
            left: 60,
            right: 50,
            height: '35%'
          },
          {
            left: 60,
            right: 50,
            top: '55%',
            height: '35%'
          }
        ],
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            axisLine: { onZero: true },
            data: timeData,
            axisLabel: {
              show: true,
              showMaxLabel: true,
              showMinLabel: true,
              formatter: function (value, index) {
                var date = new Date(value)
                // console.log(date)
                // console.log(date.getHours())
                // console.log(date.getMinutes())

                if (date.getHours() === 0 && date.getMinutes() === 0) {
                  // return [date.getMonth() + 1, date.getDate()].join('/')
                  return date.getDate()
                } else {
                  const hours = date.getHours().toString().padStart(2, '0')
                  const minutes = date.getMinutes().toString().padStart(2, '0')
                  return `${hours}:${minutes}`
                }
              }
            },
          },
          {
            gridIndex: 1,
            type: 'category',

            boundaryGap: false,
            axisLine: { onZero: true },
            data: timeData,
            position: 'bottom',
            axisLabel: {
              show: true,
              showMaxLabel: true,
              showMinLabel: true,
              formatter: function (value, index) {
                var date = new Date(value)
                // console.log(date)
                // console.log(date.getHours())
                // console.log(date.getMinutes())

                if (date.getHours() === 0 && date.getMinutes() === 0) {
                  // return [date.getMonth() + 1, date.getDate()].join('/')
                  return date.getDate()
                } else {
                  const hours = date.getHours().toString().padStart(2, '0')
                  const minutes = date.getMinutes().toString().padStart(2, '0')
                  return `${hours}:${minutes}`
                }
              }
            },
          }
        ],
        yAxis: [
          {
            name: 'SOC(100%)',
            type: 'value',
            min: 0,
            max: 100
          },
          {
            gridIndex: 1,
            name: '功率(kW)',
            type: 'value',
            // inverse: true
          }
        ],
        series:
          [
            ...socSeries,
            {
              name: '功率(kW)',
              type: 'line',
              symbol: 'none',
              step: 'start',
              xAxisIndex: 1,
              showSymbol: false,
              yAxisIndex: 1,
              itemStyle: {
                color: 'rgb(255,220,97)'
              },
              areaStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgb(255,220,97)'
                  },
                  {
                    offset: 1,
                    color: 'rgb(247,249,255)'
                  }
                ])
              },
              data: power
            },
            {
              name: '策略',
              type: 'line',
              step: 'start',
              xAxisIndex: 1,
              showSymbol: false,
              yAxisIndex: 1,
              itemStyle: {
                color: '#2AA523'
              },
              lineStyle: {
                color: '#2AA523',
                width: 2,
                type: 'dashed'
              },
              data: strategy
            }
          ]
      option && myChart.setOption(option)
      window.addEventListener('resize', () => {
        myChart.resize()
      })
    },
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端~初学者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值