2016-10-03 5 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

'year'は' .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);   
    }) 

...私は keys配列が使用されるようになっているか理解していない

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); 
    }) 

、あなたの質問は異なる別の問題は、あなたのdata.forEach(function(keys){...})である可能性があります。

+0

これは私と同じコードです。 – JGS

+0

...いくつかの違いがあります(「NEW LINES」と「NB」を参照)。最初のバージョンでjsfiddleを設定すると、その違いを表示できます... – tarulen

関連する問題