Chart.js 2.0 使用货币和千位分隔符格式化 Y 轴

2022-01-22 00:00:00 format charts jquery javascript chart.js

我在格式化图表轴时遇到问题,我找不到更新版本 2.0 的示例.

I am having problems with formatting my chart axis and I am not able to find an example for the updated version 2.0.

我怎样才能(例如)赚到 2000000 到 2.000.000 欧元?

How can I (for example) make 2000000 to €2.000.000?

我的小提琴:https://jsfiddle.net/Hiuxing/4sLxyfya/4/

window.onload = function() {
    var ctx = document.getElementById("canvas").getContext("2d");
    window.myBar = new Chart(ctx, {
        type: 'bar',
        data: barChartData,
        options: {
            title: {
                display:true,
                text:"Figure"
            },
            legend: {
                position: "bottom"
            },
            tooltips: {
                mode: 'label',
                bodySpacing: 10,
                cornerRadius: 0,
                titleMarginBottom: 15
            },
            scales: {
                xAxes: [{
                    ticks: {}
                }],
                yAxes: [{
                    ticks: {
                        beginAtZero: true,
                        stepSize: 500000
                    }
                }]
            },
            responsive: true
        }
    });
};

推荐答案

修复

在创建配置对象时将函数分配给 userCallback.创建刻度线时会调用它.您可以在 Scales 文档底部的 Chart.js

修复问题的示例

yAxes: [{
    ticks: {
        beginAtZero: true,
        stepSize: 500000,

        // Return an empty string to draw the tick line but hide the tick label
        // Return `null` or `undefined` to hide the tick line entirely
        userCallback: function(value, index, values) {
            // Convert the number to a string and splite the string every 3 charaters from the end
            value = value.toString();
            value = value.split(/(?=(?:...)*$)/);

            // Convert the array to a string and format the output
            value = value.join('.');
            return '€' + value;
        }
    }
}]

相关文章