2013-02-14 39 views
2

我有一個谷歌的圖表上我的網站,這是一個線圖。但我的Y軸的數據值僅上去的整數,所以我不想對Y軸的「4.5」的價值觀,你可以看到:谷歌圖表上浮了1,不連續

enter image description here

如果有人可以點我在正確的方向我會很感激!

回答

2

你應該使用以下參數gridlines:{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

但最大值可以是任何東西,最小爲1,但最大可能是無窮大。 – Frank 2013-02-14 17:28:36

+0

我想在這種情況下,您需要一個函數來計算和縮放網格線的值 – 2013-02-14 17:31:06

+0

我在下面的答案中包含了一個函數。希望能幫助到你。 – jmac 2013-02-15 00:24:05

0

此問題已經回答here

而這正是它說:

如果你只是想號碼顯示爲整數,那麼它的 簡單:

function drawVisualization() { 
    // Create and populate the data table. 
    var data = google.visualization.arrayToDataTable([ 
    ['x', 'Cats', 'Blanket 1', 'Blanket 2'], 
    ['A', 1,  1,   0.5], 
    ['B', 2,  0.5,   1], 
    ['C', 4,  1,   0.5], 
    ['D', 8,  0.5,   1], 
    ['E', 7,  1,   0.5], 
    ['F', 7,  0.5,   1], 
    ['G', 8,  1,   0.5], 
    ['H', 4,  0.5,   1], 
    ['I', 2,  1,   0.5], 
    ['J', 3.5,  0.5,   1], 
    ['K', 3,  1,   0.5], 
    ['L', 3.5,  0.5,   1], 
    ['M', 1,  1,   0.5], 
    ['N', 1,  0.5,   1] 
    ]); 

    // Create and draw the visualization. 
    new google.visualization.LineChart(document.getElementById('visualization')). 
     draw(data, {curveType: "function", 
        width: 500, height: 400, 
        vAxis: {maxValue: 10, format: '0'}} 
     ); 
} 

如果你去google playground你會注意到該軸標籤 是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; 

有幾個問題在這裏(不幸)。第一個是我使用的網格線的數量來確定最小/最大值是 - 你 想弄清楚你應該有多少個網格線必須使用好的 整數。最簡單的方法來做到這一點,我想,是因爲 如下(僞代碼只):

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

然後,設置網格線選項(vAxis.gridlines)以上 變量,你的最大至newMax,你的分鐘到newMin。這應該 給你整個號碼軸標籤。

注意:如果你的數字是非常小的,然後上面的功能可能無法 工作。該功能還沒有經過測試,所以請檢查對一些 例子在你自己的時間,讓我知道,如果它不能正常工作。

1http://code.google.com/apis/ajax/playground/?type=visualization#line_chart