Pythoneer
Pythoneer

Reputation: 165

Google Charts: Stop from rounding up?

I have a simple problem, but maybe not a simple solution. I do not have much experience with javascript and the google charts api.

I have an array with numbers in the range of 0.0006 to 0.0009. But if I plot it with a chart, it rounds that up to 0.001, on the vAxis and on the tooltip when I go with the mouse on the chart line.

How do I get to show x places after the decimal point instead of rounding up? Or change the point where it rounds up.

thx.

Upvotes: 2

Views: 1234

Answers (1)

WhiteHat
WhiteHat

Reputation: 61222

to format the number in the tooltip,
you need to format the data in the data table

you can use a number formatter,

formatPattern = '#,##0.0000';
var formatNumber = new google.visualization.NumberFormat({
  pattern: formatPattern
});

then use the format method, pass in the data table and the column index to be formatted...

formatNumber.format(data, 1);

see following working snippet...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    [0, 0.0006],
    [1, 0.0007],
    [2, 0.0008],
    [3, 0.0009],
    [4, 0.0005]
  ], true);

  formatPattern = '#,##0.0000';
  var formatNumber = new google.visualization.NumberFormat({
    pattern: formatPattern
  });
  formatNumber.format(data, 1);

  var options = {
    pointSize: 4,
    vAxis: {
      format: formatPattern
    }
  };

  var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
  chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Upvotes: 1

Related Questions