Skip decimal points on y-axis in chartJS

后端 未结 6 1240
说谎
说谎 2020-12-24 00:18

I am using this library to draw charts in my web app. The issue is that I am having decimal points in my y-axis. You can see that in the image below

Is there a way

6条回答
  •  礼貌的吻别
    2020-12-24 00:48

    Update: please see an updated answer from @DreamTeK that shows how this can now be done as part of the chartjs api https://stackoverflow.com/a/54006487/2737978


    in chartjs 2.x you can pass an option for a userCallback to the yaxis tick field. In this you can check if the label is a whole number

    here is an example

     options = {
         scales: {
             yAxes: [{
                 ticks: {
                     beginAtZero: true,
                     userCallback: function(label, index, labels) {
                         // when the floored value is the same as the value we have a whole number
                         if (Math.floor(label) === label) {
                             return label;
                         }
    
                     },
                 }
             }],
         },
     }
    

    fiddle example

提交回复
热议问题