如何在Chart.js v2中格式化比例数字

时间:2021-12-14 10:06:23

First of all, I apologize if my question is a slightly the same as others question. I searched for a solution and found 3 similar questions, yet none of them worked in my case, because the examples I found use Chart.js v1.

首先,如果我的问题与其他问题略有不同,我道歉。我搜索了一个解决方案,发现了3个类似的问题,但是我的情况都没有,因为我发现的例子使用的是Chart.js v1。

I saw a sample using a function placed in scaleLabel to format the numbers just like this:

我看到一个示例使用scaleLabel中的函数来格式化数字,如下所示:

var options = {
    animation: false,
    scaleLabel: function(label){
        return  '$' + label.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
};

The above code, however, doesn't work anymore in the new Chart.js v2.

但是,上面的代码在新的Chart.js v2中不再起作用。

How can I format the scale numbers in Chart.js v2?

如何格式化Chart.js v2中的比例数?

Here is my code: https://jsfiddle.net/2n7xc7j7/

这是我的代码:https://jsfiddle.net/2n7xc7j7/

2 个解决方案

#1


0  

When adding option configurations, you need to make sure you nest the settings in the right sections. You can format the scale numbers by adding a callback function to the yAxes ticks configuration section:

添加选项配置时,需要确保将设置嵌套在右侧部分中。您可以通过向yAxes ticks配置部分添加回调函数来格式化比例数:

options = {
    scales: {
        yAxes: [{
            ticks: {
                callback: function(value) {
                    return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                }
            }
        }]
    }
};

JSFiddle Demo: https://jsfiddle.net/2n7xc7j7/1/

JSFiddle演示:https://jsfiddle.net/2n7xc7j7/1/

#2


0  

I did this:

我这样做了:

http://profwtelles.ddns.net/grafico2.html

    var canvas = document.getElementById('chart');
    new Chart(canvas, {
        type: 'line',
        data: {
            labels: ['1', '2', '3', '4', '5'],
            datasets: [{
                label: 'A',
                yAxisID: 'A',
                data: [100, 96, 84, 76, 69]
            }, {
                label: 'B',
                yAxisID: 'B',
                data: [1, 1, 1, 1, 0]
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    id: 'A',
                    type: 'linear',
                    position: 'left',
                }, {
                    id: 'B',
                    type: 'linear',
                    position: 'right',
                    ticks: {
                        max: 1,
                        min: 0
                    }
                }]
            }
        }
    });

I hope to help you. Best Regards

我希望能帮助你。最好的祝福

#1


0  

When adding option configurations, you need to make sure you nest the settings in the right sections. You can format the scale numbers by adding a callback function to the yAxes ticks configuration section:

添加选项配置时,需要确保将设置嵌套在右侧部分中。您可以通过向yAxes ticks配置部分添加回调函数来格式化比例数:

options = {
    scales: {
        yAxes: [{
            ticks: {
                callback: function(value) {
                    return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                }
            }
        }]
    }
};

JSFiddle Demo: https://jsfiddle.net/2n7xc7j7/1/

JSFiddle演示:https://jsfiddle.net/2n7xc7j7/1/

#2


0  

I did this:

我这样做了:

http://profwtelles.ddns.net/grafico2.html

    var canvas = document.getElementById('chart');
    new Chart(canvas, {
        type: 'line',
        data: {
            labels: ['1', '2', '3', '4', '5'],
            datasets: [{
                label: 'A',
                yAxisID: 'A',
                data: [100, 96, 84, 76, 69]
            }, {
                label: 'B',
                yAxisID: 'B',
                data: [1, 1, 1, 1, 0]
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    id: 'A',
                    type: 'linear',
                    position: 'left',
                }, {
                    id: 'B',
                    type: 'linear',
                    position: 'right',
                    ticks: {
                        max: 1,
                        min: 0
                    }
                }]
            }
        }
    });

I hope to help you. Best Regards

我希望能帮助你。最好的祝福