如何在Chart.js v2中设置刻度数字的格式 [英] How To Format Scale Numbers in Chart.js v2

查看:122
本文介绍了如何在Chart.js v2中设置刻度数字的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对于我的问题与其他问题稍有相同,我深表歉意。我搜索了一个解决方案,发现了3个类似的问题,但是对于我来说,这些问题都不起作用,因为我发现的示例使用Chart.js v1。

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.

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

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

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

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

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?

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

推荐答案

我这样做:

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

这篇关于如何在Chart.js v2中设置刻度数字的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆