当前位置: 首页 > news >正文

告别复杂数据对比:ECharts多柱重叠方案让洞察一目了然

还在为复杂的数据对比头疼吗?ECharts多柱重叠柱状图的终极解决方案。

通过巧妙运用不可见的第二Y轴和不同透明度,完美叠加基础数据与完成数据,结合渐变色彩与交互提示,助您轻松提升数据分析效率,快速发现业务洞察。

在数据可视化中,多柱重叠柱状图能有效展示不同数据系列的对比关系。

如何使用ECharts实现这一图表类型,包含完整的HTML/CSS/JS代码实现及原理说明。

核心实现原理

多柱重叠柱状图通过双Y轴方案实现不同系列的堆叠和重叠效果:

  1. 双Y轴设计:将基础柱状图(总数)放在第二个不可见的Y轴上
  2. 视觉区分:使用不同透明度区分重叠部分
  3. 渐变色彩:增强视觉效果
  4. 响应式设计:适配不同屏幕尺寸

完整代码实现

HTML结构

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>ECharts多柱重叠柱状图</title><script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script><link rel="stylesheet" href="style.css">
</head>
<body><div class="chart-container"><div id="mainChart" style="width: 100%; height: 600px;"></div></div><script src="script.js"></script>
</body>
</html>

CSS样式

body {margin: 0;padding: 20px;font-family: 'Arial', sans-serif;background-color: #f5f7fa;
}.chart-container {max-width: 1200px;margin: 0 auto;background: white;border-radius: 8px;box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);padding: 20px;
}h1 {text-align: center;color: #333;margin-bottom: 30px;
}

JavaScript配置

document.addEventListener('DOMContentLoaded', function() {// 初始化图表const chartDom = document.getElementById('mainChart');const myChart = echarts.init(chartDom);// 配置项const option = {title: {text: '多柱重叠柱状图示例',subtext: '本周与上周数据对比',left: 'center',textStyle: {color: '#333',fontSize: 18,fontWeight: 'bold'}},tooltip: {trigger: 'axis',axisPointer: {type: 'shadow',shadowStyle: {color: 'rgba(150, 150, 150, 0.1)'}},formatter: function(params) {let result = params[0].name + '<br/>';params.forEach(item => {result += `${item.marker} ${item.seriesName}: ${item.value}<br/>`;});return result;}},legend: {data: ['本周总数', '上周总数', '本周完成数', '上周完成数'],top: 40,textStyle: {color: '#666'}},grid: {left: '3%',right: '4%',bottom: '3%',top: '20%',containLabel: true},yAxis: [{type: 'category',data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],axisLine: {lineStyle: {color: '#ddd'}},axisLabel: {color: '#666'}},{type: 'category',show: false,data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']}],xAxis: {type: 'value',axisLine: {show: false},axisLabel: {color: '#999'},splitLine: {lineStyle: {type: 'dashed',color: '#eee'}}},series: [{name: '本周总数',type: 'bar',yAxisIndex: 1,  // 使用第二个Y轴barWidth: 30,itemStyle: {color: 'rgba(41, 188, 203, 0.2)',borderColor: 'rgba(41, 188, 203, 0.8)',borderWidth: 1,borderRadius: [4, 4, 0, 0]},data: [40, 40, 40, 40, 40, 40, 40],label: {show: true,position: 'top',formatter: '{c}',color: '#666'}},{name: '上周总数',type: 'bar',yAxisIndex: 1,barWidth: 30,itemStyle: {color: 'rgba(63, 133, 240, 0.2)',borderColor: 'rgba(63, 133, 240, 0.8)',borderWidth: 1,borderRadius: [4, 4, 0, 0]},data: [50, 50, 50, 50, 50, 50, 50],label: {show: true,position: 'top',formatter: '{c}',color: '#666'}},{name: '本周完成数',type: 'bar',barWidth: 20,itemStyle: {color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#29bccb' },{ offset: 1, color: '#1a8c99' }]),borderRadius: [4, 4, 0, 0]},data: [10, 15, 20, 20, 30, 45, 55],label: {show: true,position: 'insideTop',color: 'white'}},{name: '上周完成数',type: 'bar',barWidth: 20,itemStyle: {color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#3f85f0' },{ offset: 1, color: '#2a5caa' }]),borderRadius: [4, 4, 0, 0]},data: [9, 27, 36, 30, 22, 35, 40],label: {show: true,position: 'insideTop',color: 'white'}}]};// 应用配置myChart.setOption(option);// 响应式调整window.addEventListener('resize', function() {myChart.resize();});
});

关键配置说明

通过与主流ACME渠道对接,来此加密支持Let's Encrypt、Google和Zerossl等证书提供商,用户可以根据需求选择不同的证书方案。平台提供多种选择,满足不同用户的需求,让证书申请更加灵活和高效。

配置项 说明 示例值
yAxisIndex 指定使用的Y轴 1
barWidth 柱状图宽度 20/30
itemStyle 柱状图样式 渐变/边框
data 图表数据 [10,15,20...]

使用方法

  1. 直接打开HTML文件即可运行
  2. 修改series中的data数组可更新图表数据
  3. 调整barWidth可控制柱状图宽度
  4. 修改itemStyle中的颜色值可更改柱状图配色
http://www.jsqmd.com/news/659894/

相关文章:

  • DeepSeek LeetCode 1489.找到最小生成树里的关键边和伪关键边 public List<List<Integer>> findCriticalAndPseudoCritical
  • 汽车 ECU “一芯一证” 实现详解:头部车企四级密钥体系实践
  • 2026年生命科学科研试剂公司口碑排行,聚顶生物公司介绍来啦 - mypinpai
  • SLG大地图实战:从Tilemap到Shader的地表渲染与数据分层架构
  • 最全话费卡快捷回收攻略,轻松实现现金变现! - 团团收购物卡回收
  • 【Java】继承:从入门到JVM底层,一篇搞定
  • Windows Cleaner终极方案:一键解决C盘爆红难题的智能清理工具
  • 零配置部署mPLUG视觉问答:一键启动,开箱即用的图片分析工具
  • Driver Store Explorer:5分钟掌握Windows驱动管理,轻松释放10GB+磁盘空间
  • SAP 组织与核算要素关系清单(含层级、归属、数据流向、关键T-code)
  • Comics Downloader终极指南:8大漫画网站一键离线下载,打造个人漫画图书馆
  • NVIDIA Profile Inspector 2.4.0.1:解锁NVIDIA显卡隐藏性能的终极指南
  • Coze-Loop与Dify平台集成:全栈AI应用开发优化
  • 3048基于单片机的直流电机角度速度控制系统设计(数码管,矩阵键盘)
  • RWKV7-1.5B-G1A Java开发实战:集成SpringBoot构建智能微服务
  • javascript:void(0) 含义
  • 【THM-课程内容】:Privilege Escalation-Windows Privilege Escalation:Abusing dangerous privileges
  • LLM工程化实践——RAG基础入门(一)
  • Bitbucket代码仓库全流程指南:从创建到分支管理与忽略文件配置
  • GEO Monitor Toolkit:让你知道 AI 模型在背后怎么评价你
  • SAP 组织与核算要素全景梳理(含架构、关系、数据流转)
  • ComfyUI-VideoHelperSuite三阶架构设计:基于FFmpeg的模块化视频处理引擎
  • TR-B | 中南-北航团队:连续通勤走廊早高峰均衡,终于完整破解!
  • 飞书文档批量导出工具:从手动复制到自动化迁移的完整解决方案
  • C语言中将数字转换为字符串的方法
  • 013、Python条件判断:if、elif、else语句
  • 轻量模型不妥协:all-MiniLM-L6-v2在Ollama中保持92%+ STS-B准确率
  • 从原理到实战:深度剖析Apache Shiro Remember Me反序列化漏洞(CVE-2016-4437)的攻防博弈
  • GitHub中文界面插件终极指南:3分钟让你的GitHub全面中文化
  • 沈阳小程序制作终极攻略:2026 年精准锁定最佳开发团队