2017-07-17 86 views
2

我想Y轴从实数更改为整数,这里是形象,请帮我解决这个问题如何在chartjs中将浮点数的Y轴值更改为整数?

enter image description here

这里是我的代码

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

请检查这个问题[https://stackoverflow.com/questions/25388901/chart-js-x-axis](https ://stackoverflow.com/questions/25388901/chart-js-x-axis) –

回答

3

在你的情况,你可以设置stepSize属性1为y轴的刻度,来改变从浮点数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>

+0

感谢您的支持! :)) –

+0

不客气! –

+0

对不起,我再次接受 –

0

试试这个:

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

谢谢你!它工作:)) –