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