固定宽度200px
- 通过画布的
measureText
方法计算文字的宽度来增加换行符
yAxis: [
{
type: "category",
axisLabel: {
textStyle: {
fontSize: 16
},
formatter: (val) => {
let c = document.createElement("canvas");
const ctx = c.getContext("2d");
ctx.font = "16px normal";
const arr = val.split("");
arr.map((item) => ctx.measureText(item).width)
.reduce((pre, next, index) => {
const nLen = pre + next;
if (nLen > 200) {
arr[index - 1] += "\n";
return next;
} else {
return nLen;
}
});
c = null;
return arr.join("");
}
}
]
左对齐
- 通过
grid.left
和yAxis.axisLabel.margin
来调整文字与左边界限和坐标轴的距离
grid: {
right: "3%",
left: -180,
bottom: 0,
containLabel: true
},
yAxis: [
axisLabel: {
align: "left",
margin: 200,
}
]
- 效果图
