2013-06-23 19 views
1

假設我有dc.js創建一個DataTable,看起來像:dc.js - 如何將總計添加到數據表?

╔═══╦════════════╦═════════════╗ 
║ ║ Name  ║ Amount  ║ 
╠═══╬════════════╬═════════════╣ 
║ 1 ║ Bob  ║ 25   ║ 
║ 2 ║ Sam  ║ 25   ║ 
║ 3 ║ Steve  ║ 50   ║ 
╚═══╩════════════╩═════════════╝ 

如何創建一個在底部的小計?

╔═══╦════════════╦═════════════╗ 
║ ║ Name  ║ Amount  ║ 
╠═══╬════════════╬═════════════╣ 
║ 1 ║ Bob  ║ 25   ║ 
║ 2 ║ Sam  ║ 25   ║ 
║ 3 ║ Steve  ║ 50   ║ 
╚═══╩════════════╩═════════════╝ 
    Total is equal to 100 
════════════════════════════════ 

任何幫助將不勝感激。

答:
我最終加入以下代碼到數據表,使其工作。

.renderlet(function (d) { return UpdateSummary(d); }); 

功能「UpdateSummary」裏面是我更新文本「總數等於100」

+0

你願意添加一個jsFiddle來說明你的解決方案嗎?儘管我現在即將推出自己的產品,但如果能夠取得領先優勢,我們感到非常高興。謝謝! –

回答

2

如果您共享代碼,我也許能幫助更具體。這是我的建議。

dc.js使用交叉過濾組來設置表的數據。無論你組設置此表(在.group()法)說

var helperArray = myGroup.all(), 
total = 0; 

helperArray.forEach(function(d) {total += d.value.amount;}) 
// console.log(d) to see what d is. it might need to just be total+= d.value 

d3.select("body").append("p") //this should be changed to make it the right spot 
    .text("Total is equal to " + total) 

大意的東西。

+0

不確定是否有效,但文檔說「數據表不使用交叉過濾器組而是使用閉包作爲分組函數」。 – mmmdreg

+0

是否有任何自定義d3代碼將顯示數據表中的聚合數字? – Nisha