【问题标题】:how to change the Y-axis values from float number to integer in chartjs?如何在chartjs中将Y轴值从浮点数更改为整数?
【发布时间】:2017-12-21 17:16:17
【问题描述】:

我想把 Yaxis 从实数改为整数,这是图片,请帮我解决这个问题

这是我的代码

var lineChartData = {
  labels: time,
  datasets: [{
    label: "Số người đăng kí ủng hộ",
    borderColor: window.chartColors.red,
    pointBackgroundColor: window.chartColors.red,
    fill: false,
    data: people_isProcessing
  }, {
    label: "Số người đã ủng hộ",
    borderColor: window.chartColors.purple,
    pointBackgroundColor: window.chartColors.purple,
    fill: false,
    data: people_isReceived
  }]
};

这是我的图表选项

window.onload = function() {
  var chartEl = document.getElementById("chart");
  window.myLine = new Chart(chartEl, {
    type: 'line',
    data: lineChartData,
    options: {
      title: {
        display: true,
        text: 'Kindmate - Chart Static Donate'
      },
      tooltips: {
        enabled: true,
        mode: 'index',
        position: 'nearest',
        custom: customTooltips
      }
    }
  });
});

【问题讨论】:

标签: javascript php laravel chart.js


【解决方案1】:

在您的情况下,您可以将 y 轴刻度的 stepSize 属性设置为 1,以将 y 轴值从浮点数更改为整数.

options: {
   scales: {
      yAxes: [{
         ticks: {
            stepSize: 1
         }
      }]
   },
   ...
}

ᴅᴇᴍᴏ

var chart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr'],
      datasets: [{
         label: '# of votes',
         data: [1, 2, 3, 4]
      }]
   },
   options: {
      scales: {
         yAxes: [{
            ticks: {
               stepSize: 1
            }
         }]
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>

【讨论】:

    【解决方案2】:

    试试这个:

    window.onload = function() {
                var chartEl = document.getElementById("chart");
                window.myLine = new Chart(chartEl, {
                    type: 'line',
                    data: lineChartData,
                    options: {
                        title:{
                            display:true,
                            text:'Kindmate - Chart Static Donate'
                        },
                        tooltips: {
                            enabled: true,
                            mode: 'index',
                            position: 'nearest',
                            custom: customTooltips
                        },
                          scales: {
                             yAxes: [{
                                 ticks: {
                                     beginAtZero: true,
                                     callback: function(value) {if (value % 1   === 0) {return value;}}
                                  }
                              }]
                         }
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多