ECharts 柱状图与饼图组合实现产品销售可视化
2025-04-29 22:45:46作者:滑思眉Philip
echarts
Apache ECharts is a powerful, interactive charting and data visualization library for browser
数据可视化需求分析
在实际业务场景中,我们经常需要同时展示产品销售数据的整体分布和详细构成。ECharts作为一款强大的数据可视化库,能够通过组合不同类型的图表来实现这一需求。本文将介绍如何使用ECharts创建柱状图和饼图的组合图表,完整展示产品销售数据。
基础柱状图实现
首先,我们来看基础的柱状图实现。ECharts的柱状图配置主要包括以下几个部分:
- 标题配置:设置图表的主标题和副标题
- 提示框配置:定义鼠标悬停时的交互提示
- 坐标轴配置:X轴为分类轴,Y轴为数值轴
- 数据系列配置:定义柱状图的样式和数据
option = {
title: {
text: 'Product Sales',
subtext: '2025 年销售情况'
},
tooltip: {
trigger: 'item',
formatter: '{b}: {c}'
},
xAxis: {
type: 'category',
data: ['Product A', 'Product B', 'Product C', 'Product D'],
axisLabel: {
interval: 0,
rotate: 45,
align: 'right'
}
},
yAxis: {
type: 'value',
name: 'Sales',
axisLabel: {
formatter: '{value}'
}
},
series: [{
name: 'Sales Amount',
type: 'bar',
data: [
{ value: 120, itemStyle: { color: '#FF6347' } },
{ value: 200, itemStyle: { color: '#32CD32' } },
{ value: 150, itemStyle: { color: '#1E90FF' } },
{ value: 80, itemStyle: { color: '#FFD700' } }
],
itemStyle: {
borderRadius: 5
}
}]
};
添加图例功能
为了使图表更加直观,我们需要添加图例功能。ECharts通过legend配置项来实现图例显示:
legend: {
data: ['Product A', 'Product B', 'Product C', 'Product D']
}
需要注意的是,当使用单一系列柱状图时,图例会默认显示系列名称而非分类名称。要实现分类名称作为图例,可以采用多系列叠加的方式。
多系列柱状图实现
通过创建多个数据系列,每个系列只包含一个产品的数据,可以实现分类名称作为图例的效果:
series: [
{
name: 'Product A',
type: 'bar',
barGap: '-100%',
itemStyle: { color: '#FF6347' },
data: [120, 0, 0, 0]
},
{
name: 'Product B',
type: 'bar',
barGap: '-100%',
itemStyle: { color: '#32CD32' },
data: [0, 200, 0, 0]
},
// 其他产品类似配置
]
这种方法的原理是:
- 每个系列对应一个产品
- 使用
barGap: '-100%'让柱体重叠 - 每个系列只在对应产品位置有数据,其他位置为0
组合饼图实现
为了更直观地展示产品销售占比,我们可以添加一个饼图:
{
name: 'Sales Distribution',
type: 'pie',
radius: '28%',
center: ['75%', '35%'],
data: [
{value:120, name:'Product A', itemStyle: {color: '#FF6347'}},
{value:200, name:'Product B', itemStyle: {color: '#32CD32'}},
// 其他产品数据
],
itemStyle: {
emphasis: {
blur: 10,
offsetX: 0,
color: 'rgba(0, 0, 0, 0.5)'
}
}
}
关键配置说明:
radius:控制饼图大小center:控制饼图位置data中的itemStyle:保持与柱状图颜色一致emphasis:定义鼠标悬停效果
完整配置示例
结合上述技术点,完整的配置如下:
option = {
title: {
text: 'Product Sales',
subtext: '2025 年销售情况'
},
tooltip: {
trigger: 'item',
formatter: '{b}: {c}'
},
legend: {
data: ['Product A', 'Product B', 'Product C', 'Product D']
},
xAxis: {
type: 'category',
data: ['Product A', 'Product B', 'Product C', 'Product D'],
axisLabel: {
interval: 0,
rotate: 45,
align: 'right'
}
},
yAxis: {
type: 'value',
name: 'Sales',
axisLabel: {
formatter: '{value}'
}
},
series: [
{
name: 'Product A',
type: 'bar',
barGap: '-100%',
itemStyle: { color: '#FF6347' },
data: [120, 0, 0, 0]
},
{
name: 'Product B',
type: 'bar',
barGap: '-100%',
itemStyle: { color: '#32CD32' },
data: [0, 200, 0, 0]
},
{
name: 'Product C',
type: 'bar',
barGap: '-100%',
itemStyle: { color: '#1E90FF' },
data: [0, 0, 150, 0]
},
{
name: 'Product D',
type: 'bar',
barGap: '-100%',
itemStyle: { color: '#FFD700' },
data: [0, 0, 0, 80]
},
{
name: 'Sales Distribution',
type: 'pie',
radius: '28%',
center: ['75%', '35%'],
data: [
{value:120, name:'Product A', itemStyle: {color: '#FF6347'}},
{value:200, name:'Product B', itemStyle: {color: '#32CD32'}},
{value:150, name:'Product C', itemStyle: {color: '#1E90FF'}},
{value:80, name:'Product D', itemStyle: {color: '#FFD700'}}
],
itemStyle: {
emphasis: {
blur: 10,
offsetX: 0,
color: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
技术要点总结
- 图例实现:通过多系列叠加方式实现分类名称作为图例
- 颜色统一:保持柱状图和饼图中相同产品的颜色一致
- 布局优化:合理调整饼图大小和位置,避免与柱状图重叠
- 交互一致性:确保提示框和悬停效果在整个图表中表现一致
这种组合图表方式既展示了各产品的具体销售数值,又直观呈现了销售占比情况,非常适合产品销售分析场景。开发者可以根据实际需求调整图表布局、颜色和交互效果,创建更加丰富的数据可视化展示。
echarts
Apache ECharts is a powerful, interactive charting and data visualization library for browser
登录后查看全文
热门项目推荐
相关项目推荐
PaddleOCR-VLPaddleOCR-VL 是一款顶尖且资源高效的文档解析专用模型。其核心组件为 PaddleOCR-VL-0.9B,这是一款精简却功能强大的视觉语言模型(VLM)。该模型融合了 NaViT 风格的动态分辨率视觉编码器与 ERNIE-4.5-0.3B 语言模型,可实现精准的元素识别。Python00- DDeepSeek-OCR暂无简介Python00
openPangu-Ultra-MoE-718B-V1.1昇腾原生的开源盘古 Ultra-MoE-718B-V1.1 语言模型Python00
HunyuanWorld-Mirror混元3D世界重建模型,支持多模态先验注入和多任务统一输出Python00
AI内容魔方AI内容专区,汇集全球AI开源项目,集结模块、可组合的内容,致力于分享、交流。03
Spark-Scilit-X1-13BFLYTEK Spark Scilit-X1-13B is based on the latest generation of iFLYTEK Foundation Model, and has been trained on multiple core tasks derived from scientific literature. As a large language model tailored for academic research scenarios, it has shown excellent performance in Paper Assisted Reading, Academic Translation, English Polishing, and Review Generation, aiming to provide efficient and accurate intelligent assistance for researchers, faculty members, and students.Python00
GOT-OCR-2.0-hf阶跃星辰StepFun推出的GOT-OCR-2.0-hf是一款强大的多语言OCR开源模型,支持从普通文档到复杂场景的文字识别。它能精准处理表格、图表、数学公式、几何图形甚至乐谱等特殊内容,输出结果可通过第三方工具渲染成多种格式。模型支持1024×1024高分辨率输入,具备多页批量处理、动态分块识别和交互式区域选择等创新功能,用户可通过坐标或颜色指定识别区域。基于Apache 2.0协议开源,提供Hugging Face演示和完整代码,适用于学术研究到工业应用的广泛场景,为OCR领域带来突破性解决方案。00- HHowToCook程序员在家做饭方法指南。Programmer's guide about how to cook at home (Chinese only).Dockerfile013
Spark-Chemistry-X1-13B科大讯飞星火化学-X1-13B (iFLYTEK Spark Chemistry-X1-13B) 是一款专为化学领域优化的大语言模型。它由星火-X1 (Spark-X1) 基础模型微调而来,在化学知识问答、分子性质预测、化学名称转换和科学推理方面展现出强大的能力,同时保持了强大的通用语言理解与生成能力。Python00- PpathwayPathway is an open framework for high-throughput and low-latency real-time data processing.Python00
项目优选
收起
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
248
2.46 K
deepin linux kernel
C
24
6
仓颉编译器源码及 cjdb 调试工具。
C++
116
89
React Native鸿蒙化仓库
JavaScript
217
297
暂无简介
Dart
547
119
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.02 K
596
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
1.02 K
410
Ascend Extension for PyTorch
Python
87
118
仓颉编程语言运行时与标准库。
Cangjie
124
102
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
592
123