如何在 Chart.js 折线图上自定义 y 轴标签?

How do I customize y-axis labels on a Chart.js line chart?

我有以下图表,想手动设置 Y 轴标签。 我不使用 1,2,3,4,5,而是使用一、二、三、四、五。
有没有办法做到这一点? 这是我的 options 结构:

    options = {
      scales: {
        yAxes: [{
          scaleLabel: { labelString: ["One", "Two", "Three", "Four", "Five"] },
          ticks: { min: 1, max: 5, stepSize: 1, suggestedMin: 0.5, suggestedMax: 5.5},
          gridLines: {display: false}
        }]
       },
     };

ticks 对象中,您可以传递一个 callback ,它将被赋予即将显示的标签。从这里您只需return一个您希望显示的字符串来代替标签。

chart.js-V2.X fiddle exampe chart.js-V3.X fiddle exampe

ticks: {
    min: 0,
    max: 5,
    stepSize: 1,
    suggestedMin: 0.5,
    suggestedMax: 5.5,
    callback: function(label, index, labels) {
        switch (label) {
            case 0:
                return 'ZERO';
            case 1:
                return 'ONE';
            case 2:
                return 'TWO';
            case 3:
                return 'THREE';
            case 4:
                return 'FOUR';
            case 5:
                return 'FIVE';
        }
    }
}