2017-02-15 65 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