有时候我们会遇到x轴标签过于长的情况,会导致显示不全
示例代码
option: {
xAxis: {
type: 'category',
data: ['长长的标签', '长长长的标签', '长长长长长的标签', '长的标签', '长长长长的标签', '长长长的标签', '长长长长长长的标签']
},
yAxis: {
type: 'value'
},
series: [
{
type: 'bar',
data: [80, 67, 42, 58, 40, 30, 56]
}
]
}
方案1:文字旋转45°
xAxis: {
type: 'category',
data: ['长长的标签', '长长长的标签', '长长长长长的标签', '长的标签', '长长长长的标签', '长长长的标签', '长长长长长长的标签'],
axisLabel: {
rotate: 45
}
}
方案2:文字换行
xAxis: {
type: 'category',
data: ['长长的标签', '长长长的标签', '长长长长长的标签', '长的标签', '长长长长的标签', '长长长的标签', '长长长长长长的标签'],
axisLabel: {
formatter: val => {
const max = 4
const valLength = val.length
const rowNum = valLength / 4
if (valLength > 1) {
let target = ''
for (let i = 0; i < rowNum; i++) {
const start = i * max
const end = start + max
target += val.substring(start, end) + '\n'
}
return target
} else {
return val
}
}
}
}
方案3:在柱条中显示
xAxis: {
show: false,
type: 'category',
data: ['长长的标签', '长长长的标签', '长长长长长的标签', '长的标签', '长长长长的标签', '长长长的标签', '长长长长长长的标签']
}
series: [
{
type: 'bar',
data: [80, 67, 42, 58, 40, 10, 56],
label: {
show: true,
position: 'insideBottom',
formatter: params => {
return params['name'].split('').join('\n')
}
}
}
]
|