引言:混合图表的价值与应用场景
在数据可视化领域,单一类型的图表往往难以满足复杂的业务需求。当我们需要同时展示不同量级的数据、对比分类数据与趋势变化,或者需要在同一视图中呈现多种度量指标时,柱状图与折线图的混合使用就显得尤为重要。这种组合不仅能够充分利用空间,还能通过视觉对比增强数据的可读性。
想象这样一个场景:你是一名电商数据分析师,需要向管理层展示”季度销售额(柱状图)”与”销售增长率(折线图)”的对比关系。如果使用两个独立的图表,不仅占用空间,还难以直观地展示两者之间的关联。而通过ECharts的混合图表功能,我们可以轻松实现这一需求。
混合图表的核心优势
1. 多维度数据展示
混合图表最大的优势在于能够在同一坐标系中展示不同量级和类型的数据。例如:
柱状图适合展示绝对值(如销售额、用户数量)
折线图适合展示相对值(如增长率、转化率、趋势变化)
2. 空间利用效率
将两种图表组合在一起,可以节省50%以上的展示空间,特别适合仪表盘和报表场景。
3. 数据关联性增强
通过共享X轴或Y轴,可以直观地展示不同指标之间的相关性,帮助决策者发现潜在规律。
ECharts混合图表基础配置
基础代码结构
// 基础混合图表配置
const mixedChart = echarts.init(document.getElementById(‘chart-container’));
const option = {
// 工具提示配置
tooltip: {
trigger: ‘axis’,
axisPointer: {
type: ‘cross’
}
},
// 图例配置
legend: {
data: [‘销售额’, ‘增长率’]
},
// X轴配置
xAxis: {
type: ‘category’,
data: [‘Q1’, ‘Q2’, ‘Q3’, ‘Q4’]
},
// Y轴配置(双Y轴)
yAxis: [
{
type: ‘value’,
name: ‘销售额’,
position: ‘left’
},
{
type: ‘value’,
name: ‘增长率’,
position: ‘right’,
axisLabel: {
formatter: ‘{value}%’
}
}
],
// 系列配置
series: [
{
name: ‘销售额’,
type: ‘bar’,
data: [120, 200, 150, 80],
itemStyle: {
color: ‘#5470c6’
}
},
{
name: ‘增长率’,
type: ‘line’,
yAxisIndex: 1, // 使用第二个Y轴
data: [2.5, 4.8, 3.2, 1.8],
itemStyle: {
color: ‘#91cc75’
},
lineStyle: {
width: 3
},
symbol: ‘circle’,
symbolSize: 8
}
]
};
mixedChart.setOption(option);
实战案例:电商销售数据分析
场景描述
假设我们有2023年四个季度的销售数据,包括:
季度销售额(万元)
季度环比增长率(%)
季度目标完成率(%)
完整代码实现
// 1. 数据准备
const salesData = {
quarters: [‘Q1’, ‘Q2’, ‘Q3’, ‘Q4’],
sales: [120, 200, 150, 80], // 销售额(万元)
growthRate: [2.5, 4.8, 3.2, 1.8], // 增长率(%)
completionRate: [85, 120, 95, 60] // 完成率(%)
};
// 2. 图表初始化
const chartDom = document.getElementById(‘sales-chart’);
const myChart = echarts.init(chartDom);
// 3. 配置项
const option = {
backgroundColor: ‘#ffffff’,
// 标题配置
title: {
text: ‘2023年电商销售数据分析’,
subtext: ‘销售额与增长率对比’,
left: ‘center’,
textStyle: {
fontSize: 18,
fontWeight: ‘bold’
}
},
// 工具提示
tooltip: {
trigger: ‘axis’,
axisPointer: {
type: ‘cross’,
crossStyle: {
color: ‘#999’
}
},
backgroundColor: ‘rgba(50,50,50,0.9)’,
borderWidth: 0,
textStyle: {
color: ‘#fff’
},
formatter: function(params) {
let result = params[0].name + ‘
‘;
params.forEach(item => {
const unit = item.seriesName === ‘销售额’ ? ‘万元’ : ‘%’;
result += `${item.marker} ${item.seriesName}: ${item.value}${unit}
`;
});
return result;
}
},
// 图例配置
legend: {
data: [‘销售额’, ‘增长率’, ‘完成率’],
top: 35,
textStyle: {
fontSize: 12
}
},
// X轴配置
xAxis: {
type: ‘category’,
data: salesData.quarters,
axisLine: {
lineStyle: {
color: ‘#333’
}
},
axisLabel: {
fontSize: 12
}
},
// Y轴配置(双Y轴)
yAxis: [
{
type: ‘value’,
name: ‘销售额(万元)’,
position: ‘left’,
axisLine: {
lineStyle: {
color: ‘#5470c6’
}
},
axisLabel: {
formatter: ‘{value}’
},
splitLine: {
lineStyle: {
type: ‘dashed’,
color: ‘#eee’
}
}
},
{
type: ‘value’,
name: ‘百分比(%)’,
position: ‘right’,
axisLine: {
lineStyle: {
color: ‘#91cc75’
}
},
axisLabel: {
formatter: ‘{value}%’
},
splitLine: {
show: false
}
}
],
// 网格配置
grid: {
left: ‘8%’,
right: ‘8%’,
bottom: ‘10%’,
top: ‘15%’,
containLabel: true
},
// 数据缩放
dataZoom: [
{
type: ‘inside’,
start: 0,
end: 100
},
{
start: 0,
end: 100,
handleIcon: ‘M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z’,
handleSize: ‘80%’,
handleStyle: {
color: ‘#fff’,
shadowBlur: 3,
shadowColor: ‘rgba(0, 0, 0, 0.6)’,
shadowOffsetX: 2,
shadowOffsetY: 2
},
textStyle: {
color: ‘#333’
},
borderColor: ‘#ccc’,
backgroundColor: ‘#f7f7f7’,
dataBackground: {
lineStyle: {
color: ‘#8392A5’,
width: 2
},
areaStyle: {
color: ‘#8392A5’
}
},
selectedDataBackground: {
lineStyle: {
color: ‘#8392A5’,
width: 2
},
areaStyle: {
color: ‘#8392A5’
}
}
}
],
// 系列配置
series: [
{
name: ‘销售额’,
type: ‘bar’,
data: salesData.sales,
barWidth: ‘30%’,
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: ‘#5470c6’ },
{ offset: 0.5, color: ‘#7bb3f0’ },
{ offset: 1, color: ‘#9eb8f8’ }
]),
borderRadius: [4, 4, 0, 0]
},
label: {
show: true,
position: ‘top’,
formatter: ‘{c}万’,
fontSize: 11,
color: ‘#333’
},
emphasis: {
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: ‘#3d5ca3’ },
{ offset: 1, color: ‘#5470c6’ }
])
}
}
},
{
name: ‘增长率’,
type: ‘line’,
yAxisIndex: 1,
data: salesData.growthRate,
smooth: true,
symbol: ‘circle’,
symbolSize: 8,
lineStyle: {
width: 3,
color: ‘#91cc75’,
shadowBlur: 5,
shadowColor: ‘rgba(145, 204, 117, 0.5)’,
shadowOffsetX: 0,
shadowOffsetY: 2
},
itemStyle: {
color: ‘#91cc75’,
borderWidth: 2,
borderColor: ‘#fff’
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: ‘rgba(145, 204, 117, 0.4)’ },
{ offset: 1, color: ‘rgba(145, 204, 117, 0.1)’ }
])
},
label: {
show: true,
position: ‘top’,
formatter: ‘{c}%’,
fontSize: 11,
color: ‘#91cc75’
},
emphasis: {
lineStyle: {
width: 4
},
symbolSize: 10
}
},
{
name: ‘完成率’,
type: ‘line’,
yAxisIndex: 1,
data: salesData.completionRate,
smooth: true,
symbol: ‘diamond’,
symbolSize: 8,
lineStyle: {
width: 2,
type: ‘dashed’,
color: ‘#fac858’
},
itemStyle: {
color: ‘#fac858’,
borderWidth: 2,
borderColor: ‘#fff’
},
label: {
show: true,
position: ‘bottom’,
formatter: ‘{c}%’,
fontSize: 11,
color: ‘#fac858’
},
emphasis: {
lineStyle: {
width: 3,
type: ‘solid’
},
symbolSize: 10
}
}
]
};
// 4. 渲染图表
myChart.setOption(option);
// 5. 响应式处理
window.addEventListener(‘resize’, function() {
myChart.resize();
});
进阶技巧:多Y轴与多系列混合
场景扩展:加入成本与利润分析
当数据维度进一步增加时,我们可能需要:
左侧Y轴:销售额、成本(万元)
右侧Y轴:利润率(%)
可能需要第三个Y轴(虽然ECharts支持最多两个独立Y轴,但可以通过数据转换实现更多维度)
多Y轴配置代码
// 多维度数据
const advancedData = {
quarters: [‘Q1’, ‘Q2’, ‘Q3’, ‘Q4’],
sales: [120, 200, 150, 80],
cost: [80, 120, 100, 60],
profitRate: [33.3, 40, 33.3, 25],
target: [100, 180, 140, 90]
};
// 高级混合图表配置
const advancedOption = {
tooltip: {
trigger: ‘axis’,
axisPointer: {
type: ‘cross’
},
backgroundColor: ‘rgba(50,50,50,0.9)’,
borderWidth: 0,
textStyle: {
color: ‘#fff’
},
formatter: function(params) {
let result = params[0].name + ‘
‘;
params.forEach(item => {
let unit = ”;
if (item.seriesName.includes(‘率’)) unit = ‘%’;
else if (item.seriesName.includes(‘额’) || item.seriesName.includes(‘本’) || item.seriesName.includes(‘标’)) unit = ‘万’;
result += `${item.marker} ${item.seriesName}: ${item.value}${unit}
`;
});
return result;
}
},
legend: {
data: [‘销售额’, ‘成本’, ‘目标’, ‘利润率’],
top: 35,
textStyle: {
fontSize: 12
}
},
xAxis: {
type: ‘category’,
data: advancedData.quarters,
axisLabel: {
fontSize: 12
}
},
yAxis: [
{
type: ‘value’,
name: ‘金额(万元)’,
position: ‘left’,
axisLabel: {
formatter: ‘{value}’
},
splitLine: {
lineStyle: {
type: ‘dashed’,
color: ‘#eee’
}
}
},
{
type: ‘value’,
name: ‘利润率(%)’,
position: ‘right’,
axisLabel: {
formatter: ‘{value}%’
},
splitLine: {
show: false
}
}
],
grid: {
left: ‘8%’,
right: ‘8%’,
bottom: ‘10%’,
top: ‘15%’,
containLabel: true
},
series: [
{
name: ‘销售额’,
type: ‘bar’,
data: advancedData.sales,
barWidth: ‘15%’,
itemStyle: {
color: ‘#5470c6’,
borderRadius: [3, 3, 0, 0]
},
label: {
show: true,
position: ‘top’,
formatter: ‘{c}万’,
fontSize: 10
}
},
{
name: ‘成本’,
type: ‘bar’,
data: advancedData.cost,
barWidth: ‘15%’,
barGap: ‘10%’, // 柱状图之间的间距
itemStyle: {
color: ‘#ee6666’,
borderRadius: [3, 3, 0, 0]
},
label: {
show: true,
position: ‘top’,
formatter: ‘{c}万’,
fontSize: 10
}
},
{
name: ‘目标’,
type: ‘line’,
data: advancedData.target,
symbol: ‘rect’,
symbolSize: 8,
lineStyle: {
width: 2,
type: ‘dashed’,
color: ‘#fac858’
},
itemStyle: {
color: ‘#fac858’
},
label: {
show: true,
position: ‘bottom’,
formatter: ‘{c}万’,
fontSize: 10
}
},
{
name: ‘利润率’,
type: ‘line’,
yAxisIndex: 1,
data: advancedData.profitRate,
smooth: true,
symbol: ‘circle’,
symbolSize: 8,
lineStyle: {
width: 3,
color: ‘#91cc75’
},
itemStyle: {
color: ‘#91cc75’,
borderWidth: 2,
borderColor: ‘#fff’
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: ‘rgba(145, 204, 117, 0.4)’ },
{ offset: 1, color: ‘rgba(145, 204, 117, 0.1)’ }
])
},
label: {
show: true,
position: ‘top’,
formatter: ‘{c}%’,
fontSize: 10
}
}
]
};
常见问题与解决方案
1. 数据量过大导致渲染卡顿
问题描述
:当数据点超过1000个时,图表渲染变慢,交互卡顿。
解决方案
:
// 使用dataZoom进行数据区域缩放
dataZoom: [
{
type: ‘inside’, // 内部缩放,通过鼠标滚轮或拖拽
start: 0,
end: 100
},
{
type: ‘slider’, // 外部滑块缩放
start: 0,
end: 100,
height: 20,
bottom: 10
}
],
// 同时可以使用采样策略
series: [{
type: ‘line’,
sampling: ‘lttb’, // Largest-Triangle-Three-Buckets算法采样
// 其他配置…
}]
2. 数据量级差异过大导致图表失真
问题描述
:当销售额是百万级别,而增长率是百分比级别时,折线图可能变成一条直线。
解决方案
:
// 使用双Y轴(前面已展示)
yAxis: [
{ type: ‘value’, name: ‘销售额’, position: ‘left’ },
{ type: ‘value’, name: ‘增长率’, position: ‘right’ }
],
// 或者使用数据归一化(需要手动处理数据)
// 在数据准备阶段进行转换
const normalizedData = {
sales: [120, 200, 150, 80],
growthRate: [2.5, 4.8, 3.2, 1.8].map(v => v * 10) // 放大10倍
};
// 然后在tooltip中显示原始值
formatter: function(params) {
// …
if (item.seriesName === ‘增长率’) {
result += `${item.seriesName}: ${(item.value/10).toFixed(1)}%
`;
}
}
3. 移动端显示问题
问题描述
:在小屏幕上,图表元素重叠,标签显示不全。
解决方案
:
// 响应式配置
const mobileOption = {
// 根据屏幕宽度调整配置
grid: {
left: window.innerWidth < 768 ? '5%' : '8%',
right: window.innerWidth < 768 ? '5%' : '8%',
bottom: window.innerWidth < 768 ? '15%' : '10%',
top: window.innerWidth < 768 ? '20%' : '15%'
},
// 移动端隐藏部分标签
series: [
{
name: '销售额',
type: 'bar',
label: {
show: window.innerWidth >= 768, // 仅在大屏显示
position: ‘top’
}
}
],
// 移动端简化图例
legend: {
show: window.innerWidth >= 768,
top: window.innerWidth < 768 ? 5 : 35
}
};
// 监听窗口变化
window.addEventListener('resize', function() {
myChart.resize();
// 重新计算配置
myChart.setOption(mobileOption);
});
4. 数据更新与动画优化
问题描述
:实时数据更新时,动画效果不理想或性能问题。
解决方案
:
// 使用notMerge参数避免配置合并
myChart.setOption(newOption, true); // true表示notMerge
// 或者使用notMerge的精细控制
myChart.setOption(newOption, {
notMerge: false, // 是否不跟之前设置的option进行合并
lazyUpdate: false, // 是否在下个帧再更新
silent: false // 是否阻止触发事件
});
// 动画优化
const animationOption = {
animation: true,
animationDuration: 1000,
animationEasing: 'cubicOut',
animationDelay: function(idx) {
return idx * 100;
},
series: [{
// 禁用单个系列的动画
animation: false,
// 或者设置更细致的动画控制
animationDuration: function(idx) {
return 1000 + idx * 100;
},
animationEasing: 'elasticOut'
}]
};
性能优化建议
1. 大数据量优化
// 1. 使用渐进式渲染
series: [{
progressive: 100, // 每帧渲染100个点
progressiveThreshold: 1000 // 超过1000点启用渐进式渲染
}]
// 2. 关闭不必要的动画
animation: false
// 3. 使用WebGL渲染(ECharts 5+)
renderer: 'canvas' // 或 'svg',canvas性能更好
2. 内存优化
// 及时销毁图表实例
function destroyChart() {
if (myChart && !myChart.isDisposed()) {
myChart.dispose();
}
}
// 在数据更新时避免重复创建
function updateChart(newData) {
if (!myChart) {
myChart = echarts.init(document.getElementById('chart-container'));
}
// 更新数据...
}
总结
柱状图与折线图的混合使用是ECharts中解决多维数据可视化的强大工具。通过合理配置双Y轴、数据缩放、样式优化和性能调优,我们可以创建出既美观又实用的图表。关键要点包括:
明确数据维度
:确定哪些数据适合柱状图,哪些适合折线图
合理使用双Y轴
:解决量级差异问题
优化交互体验
:通过tooltip、dataZoom增强用户体验
关注性能
:大数据量时采用采样、渐进式渲染等策略
响应式设计
:确保在不同设备上都能良好展示
掌握这些技巧后,你将能够轻松应对各种复杂的数据可视化需求,让数据讲述更生动的故事。
# ECharts实战技巧 柱状图与折线图混合使用教程 解决多维数据可视化难题
引言:混合图表的价值与应用场景
在数据可视化领域,单一类型的图表往往难以满足复杂的业务需求。当我们需要同时展示不同量级的数据、对比分类数据与趋势变化,或者需要在同一视图中呈现多种度量指标时,柱状图与折线图的混合使用就显得尤为重要。这种组合不仅能够充分利用空间,还能通过视觉对比增强数据的可读性。
想象这样一个场景:你是一名电商数据分析师,需要向管理层展示”季度销售额(柱状图)”与”销售增长率(折线图)”的对比关系。如果使用两个独立的图表,不仅占用空间,还难以直观地展示两者之间的关联。而通过ECharts的混合图表功能,我们可以轻松实现这一需求。
混合图表的核心优势
1. 多维度数据展示
混合图表最大的优势在于能够在同一坐标系中展示不同量级和类型的数据。例如:
柱状图适合展示绝对值(如销售额、用户数量)
折线图适合展示相对值(如增长率、转化率、趋势变化)
2. 空间利用效率
将两种图表组合在一起,可以节省50%以上的展示空间,特别适合仪表盘和报表场景。
3. 数据关联性增强
通过共享X轴或Y轴,可以直观地展示不同指标之间的相关性,帮助决策者发现潜在规律。
ECharts混合图表基础配置
基础代码结构
// 基础混合图表配置
const mixedChart = echarts.init(document.getElementById('chart-container'));
const option = {
// 工具提示配置
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross'
}
},
// 图例配置
legend: {
data: ['销售额', '增长率']
},
// X轴配置
xAxis: {
type: 'category',
data: ['Q1', 'Q2', 'Q3', 'Q4']
},
// Y轴配置(双Y轴)
yAxis: [
{
type: 'value',
name: '销售额',
position: 'left'
},
{
type: 'value',
name: '增长率',
position: 'right',
axisLabel: {
formatter: '{value}%'
}
}
],
// 系列配置
series: [
{
name: '销售额',
type: 'bar',
data: [120, 200, 150, 80],
itemStyle: {
color: '#5470c6'
}
},
{
name: '增长率',
type: 'line',
yAxisIndex: 1, // 使用第二个Y轴
data: [2.5, 4.8, 3.2, 1.8],
itemStyle: {
color: '#91cc75'
},
lineStyle: {
width: 3
},
symbol: 'circle',
symbolSize: 8
}
]
};
mixedChart.setOption(option);
实战案例:电商销售数据分析
场景描述
假设我们有2023年四个季度的销售数据,包括:
季度销售额(万元)
季度环比增长率(%)
季度目标完成率(%)
完整代码实现
// 1. 数据准备
const salesData = {
quarters: ['Q1', 'Q2', 'Q3', 'Q4'],
sales: [120, 200, 150, 80], // 销售额(万元)
growthRate: [2.5, 4.8, 3.2, 1.8], // 增长率(%)
completionRate: [85, 120, 95, 60] // 完成率(%)
};
// 2. 图表初始化
const chartDom = document.getElementById('sales-chart');
const myChart = echarts.init(chartDom);
// 3. 配置项
const option = {
backgroundColor: '#ffffff',
// 标题配置
title: {
text: '2023年电商销售数据分析',
subtext: '销售额与增长率对比',
left: 'center',
textStyle: {
fontSize: 18,
fontWeight: 'bold'
}
},
// 工具提示
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
crossStyle: {
color: '#999'
}
},
backgroundColor: 'rgba(50,50,50,0.9)',
borderWidth: 0,
textStyle: {
color: '#fff'
},
formatter: function(params) {
let result = params[0].name + '
‘;
params.forEach(item => {
const unit = item.seriesName === ‘销售额’ ? ‘万元’ : ‘%’;
result += `${item.marker} ${item.seriesName}: ${item.value}${unit}
`;
});
return result;
}
},
// 图例配置
legend: {
data: [‘销售额’, ‘增长率’, ‘完成率’],
top: 35,
textStyle: {
fontSize: 12
}
},
// X轴配置
xAxis: {
type: ‘category’,
data: salesData.quarters,
axisLine: {
lineStyle: {
color: ‘#333’
}
},
axisLabel: {
fontSize: 12
}
},
// Y轴配置(双Y轴)
yAxis: [
{
type: ‘value’,
name: ‘销售额(万元)’,
position: ‘left’,
axisLine: {
lineStyle: {
color: ‘#5470c6’
}
},
axisLabel: {
formatter: ‘{value}’
},
splitLine: {
lineStyle: {
type: ‘dashed’,
color: ‘#eee’
}
}
},
{
type: ‘value’,
name: ‘百分比(%)’,
position: ‘right’,
axisLine: {
lineStyle: {
color: ‘#91cc75’
}
},
axisLabel: {
formatter: ‘{value}%’
},
splitLine: {
show: false
}
}
],
// 网格配置
grid: {
left: ‘8%’,
right: ‘8%’,
bottom: ‘10%’,
top: ‘15%’,
containLabel: true
},
// 数据缩放
dataZoom: [
{
type: ‘inside’,
start: 0,
end: 100
},
{
start: 0,
end: 100,
handleIcon: ‘M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z’,
handleSize: ‘80%’,
handleStyle: {
color: ‘#fff’,
shadowBlur: 3,
shadowColor: ‘rgba(0, 0, 0, 0.6)’,
shadowOffsetX: 2,
shadowOffsetY: 2
},
textStyle: {
color: ‘#333’
},
borderColor: ‘#ccc’,
backgroundColor: ‘#f7f7f7’,
dataBackground: {
lineStyle: {
color: ‘#8392A5’,
width: 2
},
areaStyle: {
color: ‘#8392A5’
}
},
selectedDataBackground: {
lineStyle: {
color: ‘#8392A5’,
width: 2
},
areaStyle: {
color: ‘#8392A5’
}
}
}
],
// 系列配置
series: [
{
name: ‘销售额’,
type: ‘bar’,
data: salesData.sales,
barWidth: ‘30%’,
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: ‘#5470c6’ },
{ offset: 0.5, color: ‘#7bb3f0’ },
{ offset: 1, color: ‘#9eb8f8’ }
]),
borderRadius: [4, 4, 0, 0]
},
label: {
show: true,
position: ‘top’,
formatter: ‘{c}万’,
fontSize: 11,
color: ‘#333’
},
emphasis: {
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: ‘#3d5ca3’ },
{ offset: 1, color: ‘#5470c6’ }
])
}
}
},
{
name: ‘增长率’,
type: ‘line’,
yAxisIndex: 1,
data: salesData.growthRate,
smooth: true,
symbol: ‘circle’,
symbolSize: 8,
lineStyle: {
width: 3,
color: ‘#91cc75’,
shadowBlur: 5,
shadowColor: ‘rgba(145, 204, 117, 0.5)’,
shadowOffsetX: 0,
shadowOffsetY: 2
},
itemStyle: {
color: ‘#91cc75’,
borderWidth: 2,
borderColor: ‘#fff’
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: ‘rgba(145, 204, 117, 0.4)’ },
{ offset: 1, color: ‘rgba(145, 204, 117, 0.1)’ }
])
},
label: {
show: true,
position: ‘top’,
formatter: ‘{c}%’,
fontSize: 11,
color: ‘#91cc75’
},
emphasis: {
lineStyle: {
width: 4
},
symbolSize: 10
}
},
{
name: ‘完成率’,
type: ‘line’,
yAxisIndex: 1,
data: salesData.completionRate,
smooth: true,
symbol: ‘diamond’,
symbolSize: 8,
lineStyle: {
width: 2,
type: ‘dashed’,
color: ‘#fac858’
},
itemStyle: {
color: ‘#fac858’,
borderWidth: 2,
borderColor: ‘#fff’
},
label: {
show: true,
position: ‘bottom’,
formatter: ‘{c}%’,
fontSize: 11,
color: ‘#fac858’
},
emphasis: {
lineStyle: {
width: 3,
type: ‘solid’
},
symbolSize: 10
}
}
]
};
// 4. 渲染图表
myChart.setOption(option);
// 5. 响应式处理
window.addEventListener(‘resize’, function() {
myChart.resize();
});
进阶技巧:多Y轴与多系列混合
场景扩展:加入成本与利润分析
当数据维度进一步增加时,我们可能需要:
左侧Y轴:销售额、成本(万元)
右侧Y轴:利润率(%)
可能需要第三个Y轴(虽然ECharts支持最多两个独立Y轴,但可以通过数据转换实现更多维度)
多Y轴配置代码
// 多维度数据
const advancedData = {
quarters: [‘Q1’, ‘Q2’, ‘Q3’, ‘Q4’],
sales: [120, 200, 150, 80],
cost: [80, 120, 100, 60],
profitRate: [33.3, 40, 33.3, 25],
target: [100, 180, 140, 90]
};
// 高级混合图表配置
const advancedOption = {
tooltip: {
trigger: ‘axis’,
axisPointer: {
type: ‘cross’
},
backgroundColor: ‘rgba(50,50,50,0.9)’,
borderWidth: 0,
textStyle: {
color: ‘#fff’
},
formatter: function(params) {
let result = params[0].name + ‘
‘;
params.forEach(item => {
let unit = ”;
if (item.seriesName.includes(‘率’)) unit = ‘%’;
else if (item.seriesName.includes(‘额’) || item.seriesName.includes(‘本’) || item.seriesName.includes(‘标’)) unit = ‘万’;
result += `${item.marker} ${item.seriesName}: ${item.value}${unit}
`;
});
return result;
}
},
legend: {
data: [‘销售额’, ‘成本’, ‘目标’, ‘利润率’],
top: 35,
textStyle: {
fontSize: 12
}
},
xAxis: {
type: ‘category’,
data: advancedData.quarters,
axisLabel: {
fontSize: 12
}
},
yAxis: [
{
type: ‘value’,
name: ‘金额(万元)’,
position: ‘left’,
axisLabel: {
formatter: ‘{value}’
},
splitLine: {
lineStyle: {
type: ‘dashed’,
color: ‘#eee’
}
}
},
{
type: ‘value’,
name: ‘利润率(%)’,
position: ‘right’,
axisLabel: {
formatter: ‘{value}%’
},
splitLine: {
show: false
}
}
],
grid: {
left: ‘8%’,
right: ‘8%’,
bottom: ‘10%’,
top: ‘15%’,
containLabel: true
},
series: [
{
name: ‘销售额’,
type: ‘bar’,
data: advancedData.sales,
barWidth: ‘15%’,
itemStyle: {
color: ‘#5470c6’,
borderRadius: [3, 3, 0, 0]
},
label: {
show: true,
position: ‘top’,
formatter: ‘{c}万’,
fontSize: 10
}
},
{
name: ‘成本’,
type: ‘bar’,
data: advancedData.cost,
barWidth: ‘15%’,
barGap: ‘10%’, // 柱状图之间的间距
itemStyle: {
color: ‘#ee6666’,
borderRadius: [3, 3, 0, 0]
},
label: {
show: true,
position: ‘top’,
formatter: ‘{c}万’,
fontSize: 10
}
},
{
name: ‘目标’,
type: ‘line’,
data: advancedData.target,
symbol: ‘rect’,
symbolSize: 8,
lineStyle: {
width: 2,
type: ‘dashed’,
color: ‘#fac858’
},
itemStyle: {
color: ‘#fac858’
},
label: {
show: true,
position: ‘bottom’,
formatter: ‘{c}万’,
fontSize: 10
}
},
{
name: ‘利润率’,
type: ‘line’,
yAxisIndex: 1,
data: advancedData.profitRate,
smooth: true,
symbol: ‘circle’,
symbolSize: 8,
lineStyle: {
width: 3,
color: ‘#91cc75’
},
itemStyle: {
color: ‘#91cc75’,
borderWidth: 2,
borderColor: ‘#fff’
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: ‘rgba(145, 204, 117, 0.4)’ },
{ offset: 1, color: ‘rgba(145, 204, 117, 0.1)’ }
])
},
label: {
show: true,
position: ‘top’,
formatter: ‘{c}%’,
fontSize: 10
}
}
]
};
常见问题与解决方案
1. 数据量过大导致渲染卡顿
问题描述
:当数据点超过1000个时,图表渲染变慢,交互卡顿。
解决方案
:
// 使用dataZoom进行数据区域缩放
dataZoom: [
{
type: ‘inside’, // 内部缩放,通过鼠标滚轮或拖拽
start: 0,
end: 100
},
{
type: ‘slider’, // 外部滑块缩放
start: 0,
end: 100,
height: 20,
bottom: 10
}
],
// 同时可以使用采样策略
series: [{
type: ‘line’,
sampling: ‘lttb’, // Largest-Triangle-Three-Buckets算法采样
// 其他配置…
}]
2. 数据量级差异过大导致图表失真
问题描述
:当销售额是百万级别,而增长率是百分比级别时,折线图可能变成一条直线。
解决方案
:
// 使用双Y轴(前面已展示)
yAxis: [
{ type: ‘value’, name: ‘销售额’, position: ‘left’ },
{ type: ‘value’, name: ‘增长率’, position: ‘right’ }
],
// 或者使用数据归一化(需要手动处理数据)
// 在数据准备阶段进行转换
const normalizedData = {
sales: [120, 200, 150, 80],
growthRate: [2.5, 4.8, 3.2, 1.8].map(v => v * 10) // 放大10倍
};
// 然后在tooltip中显示原始值
formatter: function(params) {
// …
if (item.seriesName === ‘增长率’) {
result += `${item.seriesName}: ${(item.value/10).toFixed(1)}%
`;
}
}
3. 移动端显示问题
问题描述
:在小屏幕上,图表元素重叠,标签显示不全。
解决方案
:
// 响应式配置
const mobileOption = {
// 根据屏幕宽度调整配置
grid: {
left: window.innerWidth < 768 ? '5%' : '8%',
right: window.innerWidth < 768 ? '5%' : '8%',
bottom: window.innerWidth < 768 ? '15%' : '10%',
top: window.innerWidth < 768 ? '20%' : '15%'
},
// 移动端隐藏部分标签
series: [
{
name: '销售额',
type: 'bar',
label: {
show: window.innerWidth >= 768, // 仅在大屏显示
position: ‘top’
}
}
],
// 移动端简化图例
legend: {
show: window.innerWidth >= 768,
top: window.innerWidth < 768 ? 5 : 35
}
};
// 监听窗口变化
window.addEventListener('resize', function() {
myChart.resize();
// 重新计算配置
myChart.setOption(mobileOption);
});
4. 数据更新与动画优化
问题描述
:实时数据更新时,动画效果不理想或性能问题。
解决方案
:
// 使用notMerge参数避免配置合并
myChart.setOption(newOption, true); // true表示notMerge
// 或者使用notMerge的精细控制
myChart.setOption(newOption, {
notMerge: false, // 是否不跟之前设置的option进行合并
lazyUpdate: false, // 是否在下个帧再更新
silent: false // 是否阻止触发事件
});
// 动画优化
const animationOption = {
animation: true,
animationDuration: 1000,
animationEasing: 'cubicOut',
animationDelay: function(idx) {
return idx * 100;
},
series: [{
// 禁用单个系列的动画
animation: false,
// 或者设置更细致的动画控制
animationDuration: function(idx) {
return 1000 + idx * 100;
},
animationEasing: 'elasticOut'
}]
};
性能优化建议
1. 大数据量优化
// 1. 使用渐进式渲染
series: [{
progressive: 100, // 每帧渲染100个点
progressiveThreshold: 1000 // 超过1000点启用渐进式渲染
}]
// 2. 关闭不必要的动画
animation: false
// 3. 使用WebGL渲染(ECharts 5+)
renderer: 'canvas' // 或 'svg',canvas性能更好
2. 内存优化
// 及时销毁图表实例
function destroyChart() {
if (myChart && !myChart.isDisposed()) {
myChart.dispose();
}
}
// 在数据更新时避免重复创建
function updateChart(newData) {
if (!myChart) {
myChart = echarts.init(document.getElementById('chart-container'));
}
// 更新数据...
}
总结
柱状图与折线图的混合使用是ECharts中解决多维数据可视化的强大工具。通过合理配置双Y轴、数据缩放、样式优化和性能调优,我们可以创建出既美观又实用的图表。关键要点包括:
明确数据维度
:确定哪些数据适合柱状图,哪些适合折线图
合理使用双Y轴
:解决量级差异问题
优化交互体验
:通过tooltip、dataZoom增强用户体验
关注性能
:大数据量时采用采样、渐进式渲染等策略
响应式设计
:确保在不同设备上都能良好展示
掌握这些技巧后,你将能够轻松应对各种复杂的数据可视化需求,让数据讲述更生动的故事。