需求

  • 柱子需设置barMinHeight
  • 在堆叠柱状图的最上面显示当前堆叠的总和

直接上代码吧

需要注意:设置barMinHeight时为了让0不显示,只能将0设置为null;

设置为null的柱子label是不显示的。当null在最上面时label就没得了

const series = [{ name: '张三', type: 'bar', stack: '总量', barWidth: 50, color: '#2457b3', barMinHeight: 5, data: [0, 1700, 1400, 1200, 300, 1] }, { name: '李四', type: 'bar', stack: '总量', barWidth: 50, color: '#2b91c3', barMinHeight: 5, data: [2900, 1200, 300, 200, 900, null] }, { name: '王五', type: 'bar', stack: '总量', barWidth: 50, color: '#2b9FF3', barMinHeight: 5, label: { color: '#333', position: 'top' }, data: [800, 900, null, 400, 80, 60] }] const option = { title: { text: '深圳月最低生活费组成(单位:元)', subtext: 'From ExcelHome', sublink: 'http://e.weibo.com/1341556070/AjQH99che' }, tooltip: { trigger: 'axis', axisPointer: { // 坐标轴指示器,坐标轴触发有效 type: 'shadow' // 默认为直线,可选为:'line' | 'shadow' }, }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: { type: 'category', splitLine: { show: false }, data: ['总费用', '房租', '水电费', '交通费', '伙食费', '日用品数'] }, yAxis: { type: 'value' }, }; let sums = []; let stacks = ['张三', '李四', '王五']; let labelInfo = { show: true, color: '#333', position: 'top' } series.forEach((item, sIdx) => { item.data.forEach((val, idx) => { sums[idx] = (sums[idx] || 0) + (val || 0); }) labelInfo.series = JSON.parse(JSON.stringify(series)); labelInfo.formatter = function(param) { // 此处可以写个函数,param.seriesIndex + 1(2,3,4,5等等) if (labelInfo.series && ((labelInfo.series[param.seriesIndex + 1] && labelInfo.series[param.seriesIndex + 1].data[param.dataIndex]) || (labelInfo.series[param.seriesIndex + 2] && labelInfo.series[param.seriesIndex + 2].data[param.dataIndex]) ) ) { return ''; } else { return sums[param.dataIndex] } } item.label = labelInfo; }) option.series = series; console.log(sums, option); var chartDom = document.getElementById('chart'); var myChart = echarts.init(chartDom); option && myChart.setOption(option);

最新文章

  1. 关于Java中的final关键字
  2. 移动端开发之APP消息推送
  3. EL表达式怎么获取Map的动态key?
  4. Android之View和SurfaceView
  5. fancybox 最基本的使用步骤
  6. redmine添加自定义属性
  7. <javascript高级程序设计>笔记
  8. 10款很好用的 jQuery 图片滚动插件
  9. Spring4新特性简述
  10. [BILL.WEI]stimulsoft reports ,巧用关系,简化sql语句
  11. Windows 已在 DImageProcess.exe 中触发一个断点。
  12. 设置角色遗留问题和为权限设置角色以及EasyUI Tabs的使用
  13. .NetCore~Json代替了Xml
  14. 【php】strtr与str_replace的区别
  15. react ( 二 )
  16. jpa table主键生成策略
  17. git fetch 更新远程代码到本地仓库
  18. Mac系统在Pycharm中切换解释器
  19. delphi代码实现窗口最小化,最大化,关闭消息发送
  20. HDU 4681 string 求最长公共子序列的简单DP+暴力枚举

热门文章

  1. 第7讲 | ICMP与ping:投石问路的侦察兵
  2. jackjson学习2+CVE-2019-14379漏洞分析
  3. 在 Android 使用 QuickJS JavaScript 引擎教程
  4. 如何在Python中加速信号处理
  5. 如何使用TensorCores优化卷积
  6. Spring Cloud Alibaba(14)---SpringCloudAlibaba整合Sleuth
  7. java后端知识点梳理——Redis
  8. 深入理解java虚拟机笔记Chapter12
  9. pytest 给用例打标签
  10. Redis:我是如何与客户端进行通信的