1
我正在編寫一個代碼,以根據ID號找到前6個活動。 CSV是這樣的: 10000電力繳費 10001繳水費 10002燃氣繳費 10003電力繳費 10001繳水費 10001繳水費如何從d3.v3.min.js中的nest()中檢索密鑰?
我的代碼是:
d3.csv("activities.csv", function(error,data) {
//fetching and storing the activities as key and their individual count as value
countByActivties= d3.nest()
.key(function(d) { return d.ACTIVITY; })//.sortKeys(d3.ascending)
.rollup(function(v) { return v.length; }).sortValues(d3.descending)
//.sortValues(d3.ascending)
//.sortValues(function(v, d){ return d3.ascending(d.length); })
.entries(data);
console.log(JSON.stringify(countByActivties));
輸出如下:
[{"key":"Electricity Bill Payment","values":2},{"key":"Water Bill Payment","values":3},{"key":"Gas Bill Payment","values":1}]
爲了找到t根據它們的數量進行6次活動(即按計數降序排列)。我已經嘗試了getKeys函數,但沒有工作。下面是相同的代碼片段:
for(i=0;i<6;i++){
countByActivties.getKeys(function(d){ return d.key;})
}
它引發錯誤是:
testchartfunction.html:56 Uncaught TypeError: countByActivties.getKeys is not a function.
我所試圖做的是沿着鍵值進行迭代,以獲取數據。
任何人都有同樣的想法?
感謝Cyril的回覆。但我必須獲得前6名的活動。即具有最多計數的6個活動。它應該是「[{」key「:」水費支付「,」價值「:3},{」key「:」電費單支付「,」價值觀「:2},{」key「:」Gas Bill付款「,」值「:1}]」 – JGS
更改我的答案來處理排序。 – Cyril
Cyril。我已經進一步添加的持續時間科拉姆到我的CSV和我試圖計算前六activites的平均值:'actvtsByAvgdrtn = d3.nest() \t \t \t .KEY(函數(d){ \t \t \t \t爲(INT I = 0; I <= 5;我++){ \t \t \t \t \t \t如果(鍵[I] == d.ACTIVITY){返回d.ACTIVITY;} \t \t \t \t \t \t否則返回「你好「 \t \t \t \t \t} \t \t \t \t}) \t \t \t .rollup(函數(V){返回d3.mean(V,函數(d){返回d。期限; }); }) \t \t \t .entries(data);})' – JGS