echarts 堆叠柱状图 顶部添加合计

文章详细描述了如何在ECharts中使用堆叠柱状图,并且展示了如何通过JavaScript动态计算并显示顶部的综合数据。关键在于series的第四项,特别是`data`属性的设置和`label`部分的自定义formatter函数。
摘要由CSDN通过智能技术生成

堆叠有3个,后面加了一个对象显示顶部的数据,

其实主要的代码还是在series 的第四项,需要注意的是

 series的第四项中的data需要为 data: [0, 0, 0]     顶部的统计才能显示出来

增加的代码如下

{
   name: '综合',
   type: 'bar',
   stack: 'total',
   label: {
     show: true,
     position: 'top',
     formatter: (p) => {
       let arr1 =[100, 302, 301]
         let arr2 =[320, 132, 101]
         let arr3 =[220, 182, 191]
       return arr1[p.dataIndex] + arr2[p.dataIndex] + arr3[p.dataIndex]
     }
   },
   emphasis: {
     focus: 'series'
   },
   data: [0, 0, 0]
 }

完整代码如下  可以在 echarts  中直接运行

option = {
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  grid: {
    left: '4%',
    right: '1%',
    bottom: '2%',
    top: '15%',
    containLabel: true
  },

  xAxis: {
    type: 'category',
    axisLine: {
      show: false
    },
    axisTick: {
      show: false
    },
    axisLabel: {
      show: true
    },
    data: ['1月', '2月', '3月']
  },
  yAxis: [
    {
      type: 'value',
      name: '(个)',
      nameTextStyle: {
        padding: [0, 0, 0, -36] // 上右下左与原位置距离
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'dashed'
        }
      },

      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      }
    }
  ],
  series: [
    {
      name: '近两个月复购客户数',
      type: 'bar',
      stack: 'total',
      barWidth: '40%',
      itemStyle: {
        color: '#3AA0FF'
      },
      label: {
        show: true,
        color: '#fff',
        fontSize: '14px',
        formatter: (params) => params.value
      },
      emphasis: {
        focus: 'series'
      },
      data: [100, 302, 301]
    },
    {
      name: '近三个月复购客户数',
      type: 'bar',
      stack: 'total',
      barWidth: '40%',
      itemStyle: {
        color: '#4ECB73'
      },
      label: {
        show: true,
        color: '#fff',
        fontSize: '14px',
        formatter: (params) => params.value
      },
      emphasis: {
        focus: 'series'
      },
      data: [320, 132, 101]
    },
    {
      show: false,
      name: '近四个月复购客户数',
      type: 'bar',
      stack: 'total',
      barWidth: '40%',
      itemStyle: {
        color: '#FF892B'
      },
      label: {
        show: true,
        color: '#fff',
        fontSize: '14px',
        formatter: (params) => params.value
      },
      emphasis: {
        focus: 'series'
      },
      data: [220, 182, 191]
    },
    {
      name: '综合',
      type: 'bar',
      stack: 'total',
      label: {
        show: true,
        position: 'top',
        formatter: (p) => {
         let arr1 =[100, 302, 301]
         let arr2 =[320, 132, 101]
         let arr3 =[220, 182, 191]
          return arr1[p.dataIndex] + arr2[p.dataIndex] + arr3[p.dataIndex];
        }
      },
      emphasis: {
        focus: 'series'
      },
      data: [0, 0, 0]
    }
  ]
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值