从 dc js 聚合数据中删除 0 个值 table

Removing 0 values from dc js aggregated data table

我有一个 table,其中的聚合数据按两列分组,并从伪造的交叉过滤器维度获取数据:

var  groupedDimension = ndx.dimension(function(d) {return d.date + "/./" +d.Supplier})
    .group().reduceSum(function (d) {return +d.total;}); 

不过,我无法让 table 在交叉过滤时隐藏具有 0 值的行。例如,我试图将另一个假组附加到假维度上以摆脱 0,但这引发了错误。

这是 jsfiddle:https://jsfiddle.net/zks59ndm/2/非常感谢任何帮助。

此外,有人知道为什么按升序对 table 进行排序会引发错误吗?

这些实际上是相关的问题:数据 table 需要其 dimension() 参数的交叉过滤器维度,并且组和假组不提供数据 [=53] 的所有方法=] 预计。

查看浏览器调试控制台是诊断这些问题的好方法。例如,添加:

    .dimension(remove_empty_bins(groupedDimension))     

导致错误

Uncaught TypeError: _chart.dimension(...).top is not a function

所以它想要 .top() 以及 .all(),很高兴帮忙:

function remove_empty_bins(source_group) {
    function non_zero_pred(d) {
        return d.value != 0;
    }
    return {
        all: function () {
            return source_group.all().filter(non_zero_pred);
        },
        top: function(n) {
            return source_group.top(Infinity)
                .filter(non_zero_pred)
                .slice(0, n);
        }
    };
}

这是from the FAQ...我知道,现在它已经变成一本厚书了。

您几乎不需要提供 .top() - 直到 2.0。* 仅上限图表和数据 table 需要它。从 2.1.* 开始,它只需要用于数据 tables.

这让我们想到了另一个问题:为什么 d3.ascending 不起作用?

同样,调试控制台将显示方式:

Uncaught TypeError: _chart.dimension(...).bottom is not a function

对了,为了升序显示,要取最低的N项,所以现在希望维度支持.bottom()Here is the issue describing this problem,得出的结论是我们最好将其记录在某处或将其添加到大量的常见问题解答中。

它还提供了另一种解决方法,其核心是:

    bottom: function(N) {
        return group.top(Infinity).slice(-N).reverse();
    }

也就是把所有数据降序拉取,取出数组的最后N个,将得到的数组取反。如果我们在 .top(Infinity) 之后但在 .slice(-n) 之前坚持我们的非零过滤器,我们将获得有史以来最强大的 remove_empty_bins

function remove_empty_bins(source_group) {
    function non_zero_pred(d) {
        return d.value != 0;
    }
    return {
        all: function () {
            return source_group.all().filter(non_zero_pred);
        },
        top: function(n) {
            return source_group.top(Infinity)
                .filter(non_zero_pred)
                .slice(0, n);
        },
          bottom: function(n) {
            return source_group.top(Infinity)
                .filter(non_zero_pred)
                .slice(-n).reverse();
        }
    };
}

你的 fiddle 的工作叉:https://jsfiddle.net/gordonwoodhull/5ntxvsfb/5/