2016-10-03 94 views
0

我試圖找出與所有活動相關的所有活動(即按鍵提取)的所有值(即持續時間)的月平均值。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}]}] 

其他月份的休息時間也一樣。

任何想法??

+0

'.key(function(d){return year;})'中定義的年份在哪裏? – tarulen

+0

嗯?以前的版本比這個版本更接近正確,你現在有一個總體結構問題 – tarulen

回答

1

[這個答案是對代碼的第一個版本]

假設你的第一解析功能正常工作,你應該在你的數據直接記得日期字段。隨意更改字段名稱。

data.forEach(function(d) { 
     a = d.TEST_DATE.split(" ",1); 
     b=a[0].split("/"); 
     f_date = b[1].concat("-").concat(b[0]).concat("-").concat(b[2]); 
     //NEW LINES: add year, month and day fields to the data (*1 to convert to number) 
     d.year = b[2]*1; 
     d.month = b[0]*1; 
     d.day = b[1]*1; 
     console.log(d.TEST_DATE);   
     console.log(f_date);   
    }) 

然後...

groupByMonth = d3.nest() 
    .key(function(d){return d.year; }) //NB: use "d.year", not "year" 
    .key(function(d) {return d.month; }) // idem with month 
    .rollup(function(v) { return d3.mean(v, function(d) { return d.TEST_DURATION; }); }) 
    .entries(data); 
    }) 

我不明白keys陣列應該如何使用,有可能是另外一個問題,從你的問題不同,你data.forEach(function(keys){...})

+0

這是和我的代碼相同的代碼。 – JGS

+0

...有一些不同(參見「新線」和「NB」)。如果你設置了第一個版本的jsfiddle,我可以顯示出不同之處...... – tarulen