条形图的子集序数域

时间:2014-09-05 16:31:16

标签: javascript d3.js dc.js

我在条形图中有三个图表,因此有三个条形图。但是,我想只绘制两个类别,两个条形图。我有类别A,B,C,我想要绘制子集A,B的图形。这是一个有效的jsfiddle。问题是,当我将["A","B"]放入.x(d3.scale.ordinal().domain(["A","B"]))时,第三个条形图仍然是其中一个条形图。帮助赞赏。

HTML:

<body> 
    <div id='bar1'><div><h3>Bar 1</h3></div></div>
</body>

JS

var data = [{
    Category: "A",
    ID: "1"
}, {
    Category: "A",
    ID: "1"
}, {
    Category: "A",
    ID: "1"
}, {
    Category: "A",
    ID: "2"
}, {
    Category: "A",
    ID: "2"
}, {
    Category: "B",
    ID: "1"
}, {
    Category: "B",
    ID: "1"
}, {
    Category: "B",
    ID: "1"
}, {
    Category: "B",
    ID: "2"
}, {
    Category: "B",
    ID: "3"
}, {
    Category: "B",
    ID: "3"
}, {
    Category: "B",
    ID: "3"
}, {
    Category: "B",
    ID: "4"
}, {
    Category: "C",
    ID: "1"
}, {
    Category: "C",
    ID: "2"
}, {
    Category: "C",
    ID: "3"
}, {
    Category: "C",
    ID: "4"
}, {
    Category: "C",
    ID: "4"
}, {
    Category: "C",
    ID: "5"
}];

var ndx = crossfilter(data);

var XDimension = ndx.dimension(function (d) {
    return d.Category;
});

dc.barChart("#bar1")
    .width(400).height(150)
    .dimension(XDimension)
    .group(XDimension.group().reduceSum(function(d){return d.ID}))
    .x(d3.scale.ordinal().domain(["A","B"]))
    .xUnits(dc.units.ordinal)

dc.renderAll();

1 个答案:

答案 0 :(得分:2)

此处有关于缩放或关注序数图表的请求:

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

然而,在实施之前,我建议使用&#34;假组&#34;这里描述的技术:

https://github.com/dc-js/dc.js/wiki/FAQ#how-do-i-filter-the-data-before-its-charted

通用过滤适配器可能看起来像(未经测试):

function filtered_group(group, bins) {
  return {
    all:function () {
      return group.all().filter(function(d) {
        return bins.indexOf(d.key) != -1;
      });
    }
  }
};

然后像这样使用它:

var group0 = XDimension.group().reduceSum(function(d){return d.ID}),
    xgroup = filtered_group(group0, ["A", "B"]);
chart.group(xgroup);