d3.time/crossfilter 天减一

d3.time/crossfilter days are off by one

我一直在尝试创建一个 dc.js 行图来显示每天的统计数据,我的维度和组是

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

我希望结果与我的数据库查询相匹配

合计值正确,但天数偏移了一天(周日有周一的合计)

我的fiddlehttps://jsfiddle.net/santoshsewlal/txrLw9Lc/ 我一直在竭尽全力努力做到这一点,任何帮助都会很棒。 谢谢

这似乎是一个 UTC date/time 问题。处理来自多个时区的数据总是令人困惑!

您所有的时间戳都非常接近第二天 - 它们的时间戳都在 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);

但是,我不是时区方面的专家,所以我不能保证什么。只是指出问题可能出在哪里。