如何在 Chart.JS 逗号中给出条形图上自动生成的 y 轴值?

How can I give the automatically generated y-axis values on bar charts in Chart.JS commas?

我正在为我的数据值添加逗号,如下所示:

ctx.fillText(addCommas(dataset.data[i]), model.x, y_pos);
. . .
function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '' + ',' + '');
    }
    return x1 + x2;
}

...但是条形图 y 轴上的 "contextual" 值为 "raw"(无逗号),如下所示:

如何通过添加逗号来使这些值也更加人性化:

1,900,000
1,800,000
1,700,000
1,600,000
1,500,000

?

使用您手工制作的 addCommas 函数应该可以解决问题:

options: {
    scales: {
        yAxes: [{
            ticks: {
                userCallback: function(item) {
                    return addCommas(item);
                },
            }
        }]
    }
}