echarts series的遍历生成

文章目录


前言

多种类柱状图的遍历,如果是按照echarts上面的写法,4种需要series写四个对象,代码就会很冗余。如下图

在这里插入图片描述

代码

这是在vue中使用的,echarts全局注册的

let color = [
        { s: '#2184F4', e: '#92BAFD' },
        { s: '#F88528', e: '#FFB77D' },
        { s: '#FF5563', e: '#FF8A94' },
        { s: '#66B3D8', e: '#BFE7FA' }
      ]
let dataObj = [
        { name: '雨天', data: [63, 38, 40] },
        { name: '阴天', data: [20, 32, 10] },
        { name: '多云', data: [68, 33, 86] },
        { name: '晴天', data: [18, 36, 86] },
      ]
      let chart = self.$echarts.init(document.getElementById('chart'));

      chart.setOption({
        title: {
          show: false
        },
        grid: {
          top: 60,
          left: 0,
          right: 15,
          bottom: 0,
          containLabel: true
        },
        xAxis: {
          data: ['2014年', '2015年', '2016年'],
          axisLabel: {
            textStyle: {
              color: '#76C0FF',
              fontSize: 20,
              fontWeight: 400
            }
          },
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#02567A',
              width: 2
            }
          },
          z: 10
        },
        yAxis: {
          splitLine: {
            lineStyle: {
              color: "#003C56",
              width: 2
            },
          },
          axisLine: {
            lineStyle: {
              color: '#02567A',
              width: 2
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#76C0FF',
              fontSize: 18,
              fontWeight: 400
            }
          },
          max: 100,
        },
        legend: {
          itemHeight: 16,
          itemWidth: 16,
          itemGap: 8,
          textStyle: {
            fontSize: 16,
            fontWeight: 400,
            color: '#939BDA',
            lineHeight: 36
          }
        },
        series: function () {
          let serie = [];
          dataObj.map((item, index) => {
            let data = {
              name: item.name,
              type: 'bar',
              barWidth: 16,
              barGap: 0.5,  // 这是以barWidth为倍数的
              itemStyle: {
                color: new self.$echarts.graphic.LinearGradient(
                  0, 0, 0, 1,
                  [
                    { offset: 0, color: color[index].e },
                    { offset: 0.5, color: color[index].s },
                    { offset: 1, color: color[index].s }
                  ]
                )
              },
              data: item.data
            }
            serie.push(data);
          });
          return serie;
        }()
      }, true);
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值