使用dc.js渲染行图时丢失数据

时间:2017-01-07 13:02:10

标签: javascript dc.js crossfilter

创建dc.js行图时丢失了数据。

  var ndx = crossfilter(data);

  var emailDimemsion = ndx.dimension(function(d) {
    return d.email;
  });

  var emailGroup = emailDimemsion.group().reduce(
    function(p, d) {
      ++p.count;
      p.totalWordCount += +d.word_count;
      p.studentName = d.student_name;
      return p;
    },
    function(p, d) {
      --p.count;
      p.totalWordCount -= +d.word_count;
      p.studentName = d.student_name;
      return p;
    },
    function() {
      return {
        count: 0,
        totalWordCount: 0,
        studentName: ""
      };
    });


  leaderRowChart
    .width(600)
    .height(300)
    .margins({
      top: 0,
      right: 10,
      bottom: 20,
      left: 5
    })
    .dimension(emailDimemsion)
    .group(emailGroup)
    .elasticX(true)
    .valueAccessor(function(d) {
      return +d.value.totalWordCount;
    })
    .rowsCap(15)
    .othersGrouper(false)
    .label(function(d) {
      return (d.value.studentName + ": " + d.value.totalWordCount);
    })
    .ordering(function(d) {
      return -d.value.totalWordCount
    })
    .xAxis()
    .ticks(5);

  dc.renderAll();

小提琴在这里,https://jsfiddle.net/santoshsewlal/6vt8t8rn/

我的图表是这样的: enter image description here

但我期待我的结果

enter image description here

我是否以某种方式搞砸了reduce函数以省略数据?

由于

1 个答案:

答案 0 :(得分:0)

不幸的是,使用crossfilter对dc.js图表​​进行了两个级别的排序。

首先,dc.js使用group.top(N)拉出前N个,其中N是rowsCap值。 Crossfilter根据group.order function对这些项目进行排序。

然后使用chart.ordering函数再次对项目进行排序。

在这样的情况下,第二种类型可以掩盖第一种排序无法正常工作的事实。 Crossfilter不知道如何对象进行排序,除非你告诉它要查看哪个字段,所以group.top(N)会返回一些随机项。

您可以通过使crossfilter组order与图表ordering匹配来修复图表:

emailGroup.order(function(p) {
  return p.totalWordCount;
})

你小提琴的叉子:https://jsfiddle.net/gordonwoodhull/6vt8t8rn/6/

看起来有一个学生的单词数量更长,但是这与您的电子表格一致:

fixed row chart

我们计划将来停止使用group.top,因为当前的行为非常不一致。

https://github.com/dc-js/dc.js/issues/934

更新:如果您愿意使用不稳定的最新版本,则dc.js 2.1.4及更高版本不使用group.top - 加盖由{{确定仅限1}},capMixin.capcapMixin.takeFront