2017-02-15 64 views
1

我一直在試圖創造出每天統計一個dc.js rowchart,我維和組是d3.time/crossfilter天是關閉一個

var dayNameFormat = d3.time.format("%A"); 
    var weekDayFormat = d3.time.format('%w'); //weekday as a decimal number [0(Sunday),6]. 

    var dayOfWeek = ndx.dimension(function(d) { 
    return weekDayFormat(d.date) + '.' + dayNameFormat(d.date); 
    }); 

    var dayOfWeekGroup = dayOfWeek.group().reduce(
    function(p, d) { 
     ++p.count; 
     p.totalPoints += +d.points_per_date; 
     p.averagePoints = (p.totalPoints/p.count); 
     if (d.student_name in p.studentNames) { 
     p.studentNames[d.student_name] += 1 
     } else { 
     p.studentNames[d.student_name] = 1; 
     p.studentCount++; 
     } 
     return p; 
    }, 
    function(p, d) { 
     --p.count; 
     p.totalPoints -= +d.points_per_date; 
     p.averagePoints = (p.totalPoints/p.count); 
     if (p.studentNames[d.student_name] === 0) { 
     delete p.studentNames[d.student_name]; 
     p.studentCount--; 
     } 
     return p; 
    }, 
    function() { 
     return { 
     count: 0, 
     totalPoints: 0, 
     averagePoints: 0, 
     studentNames: {}, 
     studentCount: 0 
     }; 
    }); 

和圖表

dayOfWeekChart 
    .width(250) 
    .height(180) 
    .margins({ 
     top: 20, 
     left: 20, 
     right: 10, 
     bottom: 20 
    }) 
    .dimension(dayOfWeek) 
    .group(dayOfWeekGroup) 
    .valueAccessor(function(d) { 
     return d.value.totalPoints 
    }) 
    .renderLabel(true) 
    .label(function(d) { 
     return d.key.split('.')[1] + '(' + d.value.totalPoints + ' points)'; 
    }) 
    .renderTitle(true) 
    .title(function(d) { 
     return d.key.split('.')[1]; 
    }) 
    .elasticX(true); 

我希望的結果,以配合我的那些數據庫查詢

enter image description here

的到TAL值是正確的,但是天已經由天偏移(星期日有周一的總)

enter image description here

我的小提琴https://jsfiddle.net/santoshsewlal/txrLw9Lc/ 我一直在做我的頭,試圖得到這個權利,任何幫助將很棒。 感謝

回答

0

這似乎是一個UTC日期/時間問題。處理來自多個時區的數據總是令人困惑!

您的時間戳的所有都是非常接近的第二天 - 他們都是時間戳22:00。所以這取決於他們應該被解釋爲哪一天的時區。我想你可能會在東半球,當你在電子表格中閱讀這些時間戳時,這些時間戳會增加幾個小時?

你斬去時間substr

d.date = dateFormat.parse(d.activity_date.substr(0, 10)); 

我建議試圖分析整個時間改爲:

var dateFormat = d3.time.format('%Y-%m-%dT%H:%M:%S.%LZ'); 
    data.forEach(function(d, i) { 
    d.index = i; 
    d.date = dateFormat.parse(d.activity_date); 

不過,我不是專家,在這樣的時區我無法承諾任何事情。只是指出問題可能出在哪裏。

+0

謝謝@Gordon。使用d3.time.format.utc(「%Y-%m-%dT%H:%M:%S.%LZ」)做到了這一點。我發現在這裏,http://stackoverflow.com/questions/33755972/d3-datetime-parser-takes-into-account-timezone –

+0

啊,是我忘了說了'.utc'變種,這是一個很好的點。 – Gordon