过滤掉维

时间:2018-03-27 10:36:47

标签: crossfilter

以下是我的csv数据文件格式化的方法,缺少一些值:

runtime,genres,production_companies,release_date
105,Action|Comedy|Science Fiction,Columbia Pictures|Happy Madison Productions,7/16/15,1575
105,,Columbia Pictures|Happy Madison Productions,7/16/15,1575

这是我用来按总运行时分组所有唯一类型的代码:

    var genres = data.dimension(function(d) { return d.genres.split("|"); }, true).filter(!"")

    var group = genres.group().reduceSum(function(d){return d.runtime});

当我在console.log中播放流派的长度:

console.log(genres.top(Infinity)

它从维度中排除了空值(即26960而不是26983),但是当我在console.log组中时:

console.log(group.all());

它包含空值作为打印出的数组中的第一个对象:

(21) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0:
{key: "", value: 1625}
1:
{key: "Action", value: 250216}
2:
{key: "Adventure", value: 156181}

我可以过滤组中的空键,如下所示:

    console.log(group.all().filter(function(d){return d.key !== ""}));

它返回正确的数组,但这仅适用于控制台。我正在尝试使用dc.js创建一个条形图,我不知道如何过滤非空键的组,而不使用我无法使用的组上的过滤功能。

我是javascript和数据可视化的新手,并且很难掌握crossfilter库,所以请原谅我可能犯的任何错误。

以下是我如何解决它:

var group = genres.group().reduce(add_item, remove_item, initialise);

    function remove_empty_bins(source_group) {
        return {
            all:function () {
                return source_group.all().filter(function(d) {
                    //return Math.abs(d.value) > 0.00001; // if using floating-point numbers
                    return d.key !== ""; // if integers only
                });
            }
        };
    }

var filtered = remove_empty_bins(group);

找到一个函数来创建一个假组,方法是将原始组传递给一个函数,然后对其进行过滤。

0 个答案:

没有答案