Javascript 如何动态构建交叉过滤器组数组?

Javascript 如何动态构建交叉过滤器组数组?,javascript,crossfilter,dc.js,Javascript,Crossfilter,Dc.js,我有一个部分未知列的交叉过滤数据结构 我想画一个复合dc.js图表,能够动态地打开和关闭y维度 因此,我尝试为每个列构建一个组数组: this.data = crossfilter(this.rawData); this.idDimension = this.data.dimension(function (d) { return d.id; }); for (var property in this.rawData[0]) { this.groups[property] =

我有一个部分未知列的交叉过滤数据结构

我想画一个复合dc.js图表,能够动态地打开和关闭y维度

因此,我尝试为每个列构建一个组数组:

this.data = crossfilter(this.rawData);

this.idDimension = this.data.dimension(function (d) {
    return d.id;
});

for (var property in this.rawData[0]) {
    this.groups[property] = this.idDimension.group().reduceSum(function (d) {
        return d[property];
    });
}
问题是每个组都处理循环中最后一个属性的列


我怎样才能避免这种情况?还是有更好的方法来实现我的目标?

问题在于变量范围

我发现的解决方案是这样的:

var createPropertyGroup = function(dimension, property) {
    return dimension.group().reduceSum(function (d) {
            return d[property];
        });
}

for (var property in this.rawData[0]) {
    this.groups[property] = createPropertyGroup(this.idDimension, property);
}

我不确定这对您的情况是否有帮助,但您也可以向同一组添加多个属性

    teamMemberGroup = teamMemberDimension.group().reduce(

    function (p, d) {
        p.count++;
        p.name = d.name;
        p.salary = d.salary;
        p.start_date = d.start_date;
        return p;
    },

    function (p, d) {
        p.count--;
        return p;
    },

    function () {
        return {count: 0, name: "", salary: 0, p.start_date = null};
    });
DCCharts的valueAccessor方法可用于区分不同图表的不同y值

teamMemberChart
        .dimension(teamMemberDimension)
        .group(teamMemberGroup)
        .valueAccessor(function (d) {
        return d.value.count;
    })
根据您的情况,这可能有助于简化代码并提高性能