【问题标题】:How To Format Scale Numbers in Chart.js v2如何在 Chart.js v2 中格式化刻度数字
【发布时间】:2016-06-04 12:14:11
【问题描述】:

首先,如果我的问题与其他问题略有相同,我深表歉意。我搜索了一个解决方案,发现了 3 个类似的问题,但没有一个对我有用,因为我找到的示例使用 Chart.js v1。

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

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

但是,上面的代码在新的 Chart.js v2 中不再有效。

如何在 Chart.js v2 中格式化刻度数字?

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

【问题讨论】:

    标签: javascript chart.js chart.js2


    【解决方案1】:

    添加选项配置时,您需要确保将设置嵌套在正确的部分中。您可以通过将callback 函数添加到yAxes ticks configuration 部分来格式化刻度数字:

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

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

    【讨论】:

      【解决方案2】:

      在 V3 中添加了 locale 属性。这样一来,工具提示上的所有数字都会自动以该国家/地区代码的数字格式正确格式化:

      // Element
      const ctx = document.getElementById("myChart")
      
      // Data
      const data = {
        labels: ['First', 'Second', 'Third', 'Fourth'],
        datasets: [{
          label: 'Sample',
          data: [2982, 1039, 3200, 2300],
          backgroundColor: "rgba(90, 150, 220, 0.85)",
          borderColor: "rgba(70, 120, 180, 1)",
          borderWidth: 2
        }],
      }
      
      // Options
      const options = {
        locale: 'en-US'
      };
      
      const chart_trans_hari = new Chart(ctx, {
        type: 'bar',
        data: data,
        options: options
      });
      <canvas id="myChart"></canvas>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script>

      【讨论】:

        【解决方案3】:

        我这样做了:

        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
                            }
                        }]
                    }
                }
            });
        

        希望能帮到你。 最好的问候

        【讨论】:

          猜你喜欢
          • 2016-08-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-22
          相关资源
          最近更新 更多