2017-07-17 87 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

謝謝你!它工作:)) –