我試圖找出與所有活動相關的所有活動(即按鍵提取)的所有值(即持續時間)的月平均值。D3.js:如何查找按年份和月份分組的平均值?
我的CSV是這樣的:
ID TEST_DATE TEST_DURATION TEST_ACTIVITY
10000 1/1/2014 0:00 4 Electricity Bill Payment
10001 1/2/2014 0:00 1 Water Bill Payment
10002 1/3/2014 0:00 2 Gas Bill Payment
10003 1/4/2014 0:00 1 Electricity Bill Payment
10001 1/2/2014 0:00 1 Water Bill Payment
10001 1/2/2014 0:00 1 Water Bill Payment
14878 9/12/2016 6 Statement Request
我的代碼如下:
d3.csv("test.csv", function(error,data) {
data.forEach(function(d) {
//console.log(Object.prototype.toString.call(d.TEST_DATE));
a = d.TEST_DATE.split(" ",1);
b=a[0].split("/");
f_date = b[0].concat("-").concat(b[1]).concat("-").concat(b[2]);
console.log(d.f_date);
console.log(f_date);
var date = new Date(f_date);
var day = date.getDay();
var month = date.getMonth()+1;
var year = date.getFullYear();
console.log("Day:"+day+" ,Month:"+month+" ,Year:"+year);
if(data.TEST_ACTIVITY == keys){
console.log("check:"+keys);
groupByMonth = d3.nest()
.key(function(d){return year; })
.key(function(d) {return month; })
.rollup(function(v) { return d3.mean(v, function(d) { return d.TEST_DURATION; }); })
.entries(data);
console.log("Grouped Values by Month::"+JSON.stringify(groupByMonth));
}
//finding the count of no. of ids per activity using Test_ID
countByActivties= d3.nest()
.key(function(d) { return d.TEST_ACTIVITY; })
.rollup(function(v) { return v.length; })
.entries(data);
console.log("Count of activities based on test-id::"+JSON.stringify(countByActivties));
//sorting the data in descending order and find the top keys
keys = countByActivties
.sort(function(a, b){return b.values-a.values})
.slice(0,6)
.map(function(d){ return d.key;})
console.log("Keys::"+keys);
我收到以下錯誤:
Grouped Values by Month::[{"key":"2016","values":[{"key":"9","values":2.2857142857142856}]}]
它例如輸出應該是這樣的:
Grouped Values by Month::[{"key":"2016","values":[{"key":"9","values":6}]}]
其他月份的休息時間也一樣。
任何想法??
'.key(function(d){return year;})'中定義的年份在哪裏? – tarulen
嗯?以前的版本比這個版本更接近正確,你現在有一個總體結構問題 – tarulen