2015-12-08 26 views
1

我使用谷歌圖表創建堆積圖。在谷歌堆積圖中添加並顯示單列中的列值

enter image description here

這裏我有2列(一個是藍等爲紅色)。在每個我顯示的價值。

我的代碼是:

var view = new google.visualization.DataView(data); 
     view.setColumns([0, 1, { 
      calc: "stringify", 
      sourceColumn: 1, 
      type: "string", 
      role: "annotation" 
     }, 2, { 
      calc: "stringify", 
      sourceColumn: 2, 
      type: "string", 
      role: "annotation" 
     }]); 

但我要顯示的總價值(藍色+紅色)。

這是我的圖形應該是這樣的:

enter image description here

我怎樣才能做到這一點?任何幫助真的很可觀...

回答

0

提供功能calc並丟失sourceColumn
像這樣的東西應該工作...

var view = new google.visualization.DataView(data); 
view.setColumns([0, 1, 2, { 
    calc: getRowTotal, 
    type: "number", 
    role: "annotation" 
}]); 

function getRowTotal(dataView, row) { 
    var val1; 
    var val2; 

    val1 = dataView.getValue(row, 1) || 0; 
    val2 = dataView.getValue(row, 2) || 0; 

    return val1 + val2;  
} 
+0

參見['setColumns'(https://developers.google.com/chart/interactive/docs/reference#DataView_setColumns)的詳細信息... – WhiteHat

+0

更改'type:「number」,'和'返回dataView.getValue(row,1)+ dataView.getValue(row,2);'。然後它的作品 – next2u

+0

作出了編輯,謝謝。有助於測試... – WhiteHat

相關問題