如何删除Google柱形图中的小数位数

How can I show only integers on vertical axis ?

I have this : http://jsbin.com/oxodab/1

But I want to have only 1 and 2 on vertical axis.

I managed to do what I wanted by adding this line in options:

vAxis: {maxValue: x} (x > 5)

Not perfect... but it works.

For the column charts, there is no vAxis.showTextEvery property!

You can trunk the decimal digit (check here) with the vAxis.format: '#' property. But you would get just discrete values.

It sounds crazy but it works like charm for my case. Feel free to share your use case that won't work with this little magic trick.

Things to do to eliminate the bugs:

  • Limit the number of grid lines on vAxis to 4 (which is good enough for most of us).
  • Find a maximum value maxValue from your chart data.
  • Then divide the maximum value maxValue by 4 to find out if the result is a decimal number. e.g. 20/ 4 = 0.5
  • In this way, we can conditionally add maxValue to the vAxis like so:

    const maxValue = 20; // Say, the maximum value is 20.
    
    ...
    vAxis: Object.assign({}, { ... }, noDecimals(maxValue)),
    ...
    
    /**
     * Check if the divided value has decimals.
     * If it's decimals, increment the 'maxValue' by 1.
     */
    function noDecimals(maxValue) {
      return /\d+\.\d+/i.test(maxValue / 4)
        ? { maxValue: maxValue + 1 }
        : {};
    }
    

Initially, it didn't work as expected when you're trying to assign the maximum value that is found from your chart data. By incrementing the maximum value by 1 will make Google chart to fit it well.

Hope this helps for those running into this issue!