echarts 堆叠柱状图 在顶部显示数据总数

本文介绍了如何在Echarts的堆叠柱状图中实现在每个柱子顶部显示数据的总和。通过在series的最后一项添加一个全零的data数组,并在外部计算并设置最后一项为前几项数据的总和来达到目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

echarts 堆叠柱状图 在顶部显示数据总数

1.效果图

在这里插入图片描述

2.实现步骤:

const myChart = echarts.init(this.leakTypeDivide)
    const option = {
      title: {
        text: '漏洞类型划分',
        x: 'center',
        y: 'bottom',
        textStyle: {    // 副标题样式
          color: '#838080',
          fontWeight: 900,
          fontSize:15,
          lineHeight: 20,
          fontFamily: 'Microsoft Yahei'
        }
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow' 
        }
      },
      legend: {},
      grid: {
        left: '3%',
        right: '4%',
        bottom: '10%',
        containLabel: true
      },
      xAxis: {
        type: 'value'
      },
      yAxis: {
        type: 'category',
        data: ['Code Execution','Denial-of-Service','Information Exposure', 'Directory Traversal', 'Malicious Packages', 'AccessRestriction Bypass', 'CSRF', 'XXE', 'Authentication Bypass']
      },
      series: [
        {
          name: '2014',
          type: 'bar',
          stack: 'x',
          //label: { // 是否显示每一个柱状图的数据
            //show: true
          //},
          emphasis: {
            focus: 'series'
          },
          data: [100, 500, 100, 500, 300, 700, 500, 100, 500]
        },
        {
          name: '2015',
          type: 'bar',
          stack: 'x',
          emphasis: {
            focus: 'series'
          },
          data: [500, 100, 100, 100, 500, 200, 200, 200, 200]
        },
        {
          name: '2016',
          type: 'bar',
          stack: 'x',
          emphasis: {
            focus: 'series'
          },
          data: [100, 300, 100, 200, 200, 300, 300, 200, 200]
        },
        {
          name: '2017',
          type: 'bar',
          stack: 'x',
          emphasis: {
            focus: 'series'
          },
          data: [50, 200, 200, 100, 100, 300, 400, 200, 200]
        },
        {
          name: '2018',
          type: 'bar',
          stack: 'x',
          emphasis: {
            focus: 'series'
          },
          data: [300, 800, 200, 100, 100, 500, 200, 200, 200]
        },
        // 在series的最后一项,添加一个data都为零的数组
        {
          name: '总和',
          type: 'bar',
          stack: 'x',
          label: {
            show: true,
            color: '#4ba034',
            position:'right',
          },
          emphasis: {
            focus: 'series'
          },
          data: [0, 0, 0, 0, 0, 0, 0, 0, 0],
        }
      ]
    };
    
    // 接着在option外部对数据进行操作,使得最后一项的数据是前几项的总和
    var series = option.series
    function getSum (params) {
      let dataValue = 0
      for (let i = 0; i < series.length; i++) {
        dataValue += series[i].data[params.dataIndex]
      }
      return dataValue
    }
    series[series.length - 1].label.formatter = getSum
    myChart.setOption(option, true);

3.总结:
(1)在series的最后一项,添加一个data都为零的数组
(2)接着在option外部对数据进行操作,使得最后一项的数据是前几项的总和

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值