ECharts 中如何控制柱图间距
想要实现的效果是:
两个灰色紧挨着, 两个红色紧挨着, 仅增加灰色和红色的间距, 即第二个柱形和第三个柱形的间距
演示代码
option = {
legend: {
formatter: name => {
return name.replace('_', ':');
}
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
dataset: {
dimensions: [
{ name: 'Month', type: 'ordinal' },
{ name: 'Sale: Plan', type: 'ordinal' },
{ name: 'Sale: Actual', type: 'ordinal' },
{ name: 'Profit: Plan', type: 'ordinal' },
{ name: 'Profit: Actual', type: 'ordinal' }
],
source: [
{
Month: 'Jan',
'Sale: Plan': 100,
'Sale: Actual': 110,
'Profit: Plan': 50,
'Profit: Actual': 60
},
{
Month: 'Feb',
'Sale: Plan': 100,
'Sale: Actual': 110,
'Profit: Plan': 50,
'Profit: Actual': 60
},
{
Month: 'Mar',
'Sale: Plan': 100,
'Sale: Actual': 110,
'Profit: Plan': 50,
'Profit: Actual': 60
},
{
Month: 'Apr',
'Sale: Plan': 100,
'Sale: Actual': 110,
'Profit: Plan': 50,
'Profit: Actual': 60
}
]
},
xAxis: [
{
type: 'category',
axisTick: {
alignWithLabel: true
},
axisLabel: {
margin: 25
}
},
{
type: 'category',
position: 'bottom',
axisTick: {
length: 15
},
data: [
'Sale', 'Profit',
'Sale', 'Profit',
'Sale', 'Profit',
'Sale', 'Profit'
]
}
],
yAxis: {},
series: [
{
type: 'bar',
itemStyle: { color: '#CCCCCC' },
barWidth: 20,
label: {
show: true,
position: 'top',
align: 'right',
formatter: ({ seriesName, value }) => {
return value[seriesName]
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
}
},
{
type: 'bar',
itemStyle: { color: '#757575' },
barWidth: 20,
label: {
show: true,
position: 'top',
align: 'left',
formatter: ({ seriesName, value }) => {
return value[seriesName]
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
}
},
{
type: 'bar',
itemStyle: { color: '#FFA5A5' },
barWidth: 20,
label: {
show: true,
position: 'top',
align: 'right',
formatter: ({ seriesName, value }) => {
return value[seriesName]
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
}
},
{
type: 'bar',
itemStyle: { color: '#FF0000' },
barWidth: 20,
label: {
show: true,
position: 'top',
align: 'left',
formatter: ({ seriesName, value }) => {
return value[seriesName]
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
},
barGap: '20%'
}
]
};
[echarts-barGap属性]
参考:(https://echarts.apache.org/zh/option.html#series-bar.barGap)