Can I get the vAxis.maxValue from a Google Column

2019-07-30 00:04发布

问题:

I have drawn a column chart using Google charts. Now I want to know what actual vAxis.maxValue the chart is using. Note, I have not set this value, the chart has set it itself. But I want to know what value it has used. How can I do this?

回答1:

I haven't found anywhere that explains how google determines its max and min axis values (sometimes they are very weird). In Excel it is impossible to calculate (it depends on the font size, chart size, line thickness, etc.).

The easiest way to handle issues like this is to write a function to determine your own max/min axis values. The following code may help (this is the logic to do it one way, adjust as needed for your application):

// Take the Max/Min of all data values in all graphs (these are just arbitrary numbers)
var totalMax = 345;
var totalMin = -123;

// Figure out the largest number (positive or negative)
var biggestNumber = Math.max(Math.abs(totalMax),Math.abs(totalMin));

// Round to an exponent of 10 appropriate for the biggest number
var roundingExp = Math.floor(Math.log(biggestNumber) / Math.LN10);
var roundingDec = Math.pow(10,roundingExp);

// Round your max and min to the nearest exponent of 10
var newMax = Math.ceil(totalMax/roundingDec)*roundingDec;
var newMin = Math.floor(totalMin/roundingDec)*roundingDec;

// Determine the range of your values
var range = newMax - newMin;

// Define the number of gridlines (default 5)
var gridlines = 5;

// Determine an appropriate gap between gridlines
var interval = range / (gridlines - 1);

// Round that interval up to the exponent of 10
var newInterval = Math.ceil(interval/roundingDec)*roundingDec;

// Re-round your max and min to the new interval
var finalMax = Math.ceil(totalMax/newInterval)*newInterval;
var finalMin = Math.floor(totalMin/newInterval)*newInterval;