2013-12-19 41 views
1

創建Google折線圖時,我最近遇到了一個問題,無法在Stack Overflow上找到答案。在我從休克中恢復過來並在其他地方找到答案後,我想我會在這裏發佈Q & A的解決方案。Google可視化折線圖,Logorithmic Display,數據中值爲0

當你的系列包含0數據,那麼,如果你使用的是標準的LineGraph一切都看起來不錯:

Line graph, standard scale

(請注意,我換了號碼,此圖,從而你可以看到多行不對數尺度)。

如果你的圖形有很大的差異,那麼一個解決方案就是打開logScale。然而logScale只是完全跳過爲0或以下的任何值,導致在你的圖表孤立線:

Line Graph, log scale

的問題是如何使用對數標度以具有線圖顯示和包括0值。

回答

1

這個問題的解決其實很簡單,但是我發現有很多人在網上推薦的東西像所有的0值更改爲0.001,甚至改變實際值0.001和顯示的值設置爲0

有雖然是一個非常簡單的選項,但它依賴於未公開的功能,但它在當前版本中完美運行。只需打開scaleType:「mirrorLog」爲軸線,現在預期它會顯示0值:

enter image description here

的示例中的最後代碼如下所示:

function drawVisualization() { 

      var data = new google.visualization.DataTable(); 
      data.addColumn('date', 'Date'); 

        data.addColumn('number', 'create'); 

        data.addColumn('number', 'delete'); 

        data.addColumn('number', 'modify'); 

        data.addColumn('number', 'recycle'); 

        data.addColumn('number', 'view'); 

      data.addRows([ 

         [new Date(1386236040783),10477,8,28,2,2], 

         [new Date(1386322440783),13202,0,18,0,5], 

         [new Date(1386581640783),23856,0,105,0,6], 

         [new Date(1386668040783),9218,0,22,0,0], 

         [new Date(1386754440783),1441,0,14,0,0], 

         [new Date(1386840840783),832,2,7,0,0], 

         [new Date(1386927240783),240,22,41,4,4], 

         [new Date(1387186440783),2032,0,12,0,0], 

         [new Date(1387272840783),667,0,11,2,1], 

         [new Date(1387359240783),26535,50,69,11,16], 

      ]); 
      // Set chart options 
      var options = { 
       'title':'Activity over time', 
       'animation.easing':'out', 
       'animation.duration':1000, 
       'pointSize':2, 
       'vAxes': {0: {logScale: true, scaleType:"mirrorLog"}}, 
      }; 
      // Instantiate and draw our chart, passing in some options. 
      var chart = new google.visualization.LineChart(document.getElementById('visualization')); 
      chart.draw(data, options); // Create and populate the data table. 

} 

你可以試試它在谷歌操場,只是整個代碼複製到方法:

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

重要的部分是啓用scaleTypemirrorLog的vAxes配置,沒有必要將logScale設置爲true,儘管這樣做沒有任何壞處。

更改'vAxes': {0: {logScale: true, scaleType:"mirrorLog"}}中的選項可讓您查看不同的行爲。只需刪除vAxes的一個或兩個配置條目即可查看圖形如何變化。

+1

[問題984:錯誤:Google LineChart ...](https://code.google.com/p/google-visualization-api-issues/issues/detail?id=984)_但是, *無證的功能**可能適合你。您可以將'scale'設置爲'mirrorLog',它支持0和負值。對於負值,它會否定每個值,計算其日誌,並將負值放回。對於接近於零的值,正值和負值,它使用線性比例。_ –

+1

小的更正:您不需要同時使用logScale和scaleType。如果scaleType是「mirrorLog」或「log」,那就足夠了。 – dlaliberte

+0

謝謝,都好點。我已經更新了答案。 –