简体   繁体   English

如何在Chartjs中将Y轴值从浮点数更改为整数?

[英]how to change the Y-axis values from float number to integer in chartjs?

i want to change Yaxis from real number to integer and here is image, and please help me solve this problem 我想将Yaxis从实数更改为整数,这里是图像,请帮我解决这个问题

在此输入图像描述

here is my code 这是我的代码

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

and here is my option for my chart 这是我的图表选项

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

In your case, you can set stepSize property to 1 for y-axis ticks, to change the y-axis values from float number to integer. 在您的情况下,您可以将y轴刻度的stepSize属性设置为1 ,将y轴值从float number更改为integer。

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> 

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM