Javascript 谷歌图表上升了1,而不是连续上升

Javascript 谷歌图表上升了1,而不是连续上升,javascript,api,numbers,google-visualization,Javascript,Api,Numbers,Google Visualization,我的网站上有一个谷歌图表,它是一个折线图。但是我的Y轴数据值只会以整数形式递增,因此我不希望Y轴上有“4.5”值,正如您所看到的: 如果有人能给我指出正确的方向,我将不胜感激 您应该使用以下参数网格线:{count},例如 hAxis: { minValue: 1.00, maxValue: 5.00,

我的网站上有一个谷歌图表,它是一个折线图。但是我的Y轴数据值只会以整数形式递增,因此我不希望Y轴上有“4.5”值,正如您所看到的:


如果有人能给我指出正确的方向,我将不胜感激

您应该使用以下参数
网格线:{count}
,例如

                  hAxis: {
                           minValue: 1.00,
                           maxValue: 5.00,
                           baseline: 3.00,
                           viewWindowMode:'explicit',
                           viewWindow:
                           {
                              max: 5.00,
                              min: 1.00
                           },
                           gridlines:{count:11}
                   },
                   vAxis: {
                           minValue: 1.00,
                           maxValue:5.00,
                           baseline:3.00,
                           viewWindowMode:'explicit',
                           viewWindow:
                           {
                              max:5.00,
                              min:1.00
                           },
                           gridlines:{count:11}
                   }

这个问题已经得到了回答

它是这样说的:

如果您只希望数字显示为整数,那么 简单:

如果转到,您将注意到轴标签 是0,2.5,5,7.5,10。通过将格式“0”添加到vAxis,它 将只显示整数,因此我的标签将变为0、3、5、8、10。 但很明显,这并不理想,因为8个显示为一半 在5到10之间,事实并非如此,因为这个数字实际上是7.5 只是被四舍五入

更改轴比例/标签的能力受到限制。该怎么办 您所要求的将需要一点特殊的javascript来创建 适当的网格线比例和数量,以防止破坏 下面是一些时髦的数字

基本上,你要确保你的最大值和最小值, 和网格线的数量,便于划分,以便 只得到整数。要做到这一点,您需要创建一些时髦的新功能 逻辑。下面是一些示例代码,可以让您获得 适当的最小/最大轴值:

// Take the Max/Min of all data values in all graphs
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;
这里有几个问题(不幸的是)。首先,我是 使用网格线的数量来确定最小/最大值——您可以 想知道应该使用多少网格线吗 整数。我认为,最简单的方法是 如下所示(仅限伪代码):

//获取所有图形中所有数据值的最大/最小值
var totalMax=3;
var totalMin=-1;
//算出最大的数字(正数或负数)
var biggestNumber=Math.max(Math.abs(totalMax)、Math.abs(totalMin));
//四舍五入为10的指数,适用于最大的数字
var roundingExp=Math.floor(Math.log(biggestNumber)/Math.LN10);
var roundingDec=数学功率(10,roundingExp);
//将最大值和最小值四舍五入到最接近的指数10
var newMax=数学单元(totalMax/roundingDec)*roundingDec;
var newMin=数学楼层(总最小值/舍入dec)*舍入dec;
//确定您的值的范围
var范围=新最大值-新最小值;
//计算网格线数量的最佳因子(2-5条网格线)
//如果数字的范围除以2或5是一个整数,请使用它

对于(var i=2;i,但最大值可以是任何值,最小值将是1,但最大值可以是无穷大。我想在这种情况下,您需要一个函数来计算和缩放网格线的值。我在下面的答案中包含了一个函数。希望能有所帮助。
// Take the Max/Min of all data values in all graphs
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;
// Take the Max/Min of all data values in all graphs
var totalMax = 3;
var totalMin = -1;

// 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;

// Calculate the best factor for number of gridlines (2-5 gridlines)
// If the range of numbers divided by 2 or 5 is a whole number, use it
for (var i = 2; i <= 5; ++i) {
    if ( Math.round(range/i) = range/i) {
        var gridlines = i
    }
}