在 JavaScript 里校验 JSON 格式,直接用 JSON.parse 就对了
用正则去匹配 JSON 嵌套结构很容易踩坑,JS 自带的 JSON.parse 加 try catch 才是稳妥方案。
支持通配符SSL证书、多域名证书、IP证书。适配ACME接口, 支持Zerossl、Let's Encrypt和Google等渠道。登录已有账号
2026-04-21 12:15:17 ECharts 数据可视化 柱状图 前端开发 JavaScript
探索ECharts如何将数据可视化提升到新高度!实现视觉效果卓越的多柱重叠柱状图的完整方案,通过双Y轴、色彩渐变和巧妙的透明度控制,打造出兼具美观与实用性的数据图表,让数据对比不仅清晰,更具艺术感和吸引力。
在数据可视化中,多柱重叠柱状图能有效展示不同数据系列的对比关系。如何使用ECharts实现这一图表类型,包含完整的HTML/CSS/JS代码实现及原理说明。
多柱重叠柱状图通过双Y轴方案实现不同系列的堆叠和重叠效果:
<!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>
来此加密通过与多个知名SSL证书供应商ACME对接,确保平台能够为用户提供可靠的证书选择。支持Let's Encrypt、Google和Zerossl等证书来源,让用户在保障网站安全的同时,享受到最好的证书服务。
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;
}
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();
});
});
| 配置项 | 说明 | 示例值 |
|---|---|---|
| yAxisIndex | 指定使用的Y轴 | 1 |
| barWidth | 柱状图宽度 | 20/30 |
| itemStyle | 柱状图样式 | 渐变/边框 |
| data | 图表数据 | [10,15,20...] |
series中的data数组可更新图表数据barWidth可控制柱状图宽度itemStyle中的颜色值可更改柱状图配色上一篇: 腾讯云消息队列RocketMQ集群TLS通信安全配置指南
下一篇: IBM Business Automation Workflow服务器SSL访问与管理控制台登录指南(针对旧版本配置)
用正则去匹配 JSON 嵌套结构很容易踩坑,JS 自带的 JSON.parse 加 try catch 才是稳妥方案。
还在纠结Ubuntu上如何开发微信小程序?本文详尽拆解,手把手教你如何利用VS Code结合Windows虚拟机或远程主机,搭建一套高效稳定的开发环境。告别开发困境,享受Linux的自由与小程序的便捷,我们一起攻克技术壁垒!
还在为前端跨域问题头疼?本文将全面解析CORS、JSONP和Nginx反向代理三大核心解决方案。从原理到实践,详细对比各自优劣、适用场景与潜在风险,并提供生产环境下的安全性、性能优化及异常监控最佳实践,助你轻松攻克跨域难题,构建更稳健的前端应用。
还在为背单词烦恼?本文揭秘如何开发一款智能、高效的微信小程序单词记忆助手。从用户痛点出发,结合科学记忆法,设计沉浸式学习体验。我们将带你了解如何实现个性化学习路径、趣味互动功能和实时学习反馈,让单词记忆变得轻松有趣,真正提升学习效率。
深度解析HTML a标签如何通过设置`target="_blank"`属性,实现点击链接在新标签页打开,同时不关闭当前页面的功能。文章强调了`rel="noopener noreferrer"`在增强安全性方面的关键作用,并提供了绝对URL、相对路径和带参数链接的完整代码示例,助你轻松掌握。