在 ECharts 中调整柱状图的宽度(即柱子的粗细)或间距,可以通过以下几种方式实现:
barWidth
直接设置柱子宽度通过 series.barWidth
固定柱子的像素宽度,不受数据量影响:
option = {
series: [{
type: 'bar',
barWidth: 15, // 柱子宽度为 15px
data: [10, 20, 30, 40, 50]
}]
};
barMaxWidth
限制最大宽度当数据量较少时,避免柱子过宽:
option = {
series: [{
type: 'bar',
barMaxWidth: 30, // 柱子最大宽度为 30px
data: [10, 20, 30, 40, 50]
}]
};
barCategoryGap
调整类目间距控制同一类目下不同系列柱子之间的间距(百分比):
option = {
series: [{
type: 'bar',
barCategoryGap: '50%', // 类目间距为柱子宽度的 50%
data: [10, 20, 30, 40, 50]
}]
};
barWidth
和 barGap
同时控制柱子宽度和间距:
option = {
series: [{
type: 'bar',
barWidth: '30%', // 柱子宽度为类目宽度的 30%
barGap: '20%', // 不同系列之间的间距为柱子宽度的 20%
data: [10, 20, 30, 40, 50]
}]
};
option = {
xAxis: {
type: 'category',
data: ['1月', '2月', '3月', '4月', '5月']
},
yAxis: {
type: 'value'
},
series: [{
type: 'bar',
barWidth: '20%', // 柱子宽度为类目宽度的 20%(变细)
barCategoryGap: '40%', // 类目间距为柱子宽度的 40%
data: [10, 20, 30, 40, 50]
}]
};
结合 mediaQuery
根据容器尺寸动态调整:
option = {
mediaQuery: [
{
// 大屏幕:柱子稍宽
query: { minWidth: 768 },
option: {
series: [{
barWidth: '30%'
}]
}
},
{
// 小屏幕:柱子更细,避免拥挤
option: {
series: [{
barWidth: '15%'
}]
}
}
]
};
barWidth
可以是像素值(如 15
)或百分比(如 '30%'
),百分比相对于类目宽度计算。barGap
控制系列间间距。barMaxWidth
限制最大宽度。通过以上配置,你可以灵活调整柱状图的粗细和间距,让图表更加美观和易读。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。