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

堆叠有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. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-25 05:20:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-25 05:20:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-25 05:20:02       82 阅读
  4. Python语言-面向对象

    2024-04-25 05:20:02       91 阅读

热门阅读

  1. 打开jupyter Notebook闪退,怎么解决

    2024-04-25 05:20:02       32 阅读
  2. Promise面试题

    2024-04-25 05:20:02       32 阅读
  3. c++常见面试题及答案

    2024-04-25 05:20:02       34 阅读
  4. 毕业论文写作指南

    2024-04-25 05:20:02       41 阅读
  5. 【软件工程与实践】(第四版)第6章习题答案详解

    2024-04-25 05:20:02       34 阅读
  6. 【Python打包exe文件】

    2024-04-25 05:20:02       39 阅读
  7. Gitea详细介绍

    2024-04-25 05:20:02       100 阅读