Integers as y-axis in a morris.js line chart

左心房为你撑大大i 提交于 2019-12-22 04:42:52

问题


I have started using morris.js line chart in my html page. Is there a way to set the y-axis to only contain integers? Now it displays decimal numbers, but my data set only contains integers.


回答1:


This feature was added by this pull request about 25 days ago on the Github repository. It added the option gridIntegers that, by default, is set to false. It is still not merged to the main repository. Therefore, if you want to use it right now, you have to download this version and configure like following:

    Morris.Line({
      element: "mydiv",
      data: mydata,
      xkey: 'time',
      ykeys: ['value'],
      labels: ['Requisições'],
      gridIntegers: true,
      ymin: 0
    });

Have you seen a ymin parameter set to 0? That is the caveat! The y-axis will work with only integers if and only if you set your custom y-min and/or y-max boundaries. In my case it is doable as I know my data will present no values inferior to 0. Therefore, it works like a charm.

You can see bellow the difference when not using and when using the patch:




回答2:


Here's the actual code I used....

new Morris.Bar({
  ...
  yLabelFormat: function(y){return y != Math.round(y)?'':y;},
  ...
});



回答3:


Another simpler solution that can be used ... This keeps the interval calculation between the lines.

yLabelFormat: function(y){return y = Math.round(y);},


来源:https://stackoverflow.com/questions/18571807/integers-as-y-axis-in-a-morris-js-line-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!