dc.js:当count为零时包括所有条形

时间:2015-10-19 14:02:22

标签: javascript d3.js dc.js

我需要在图表中包含所有条形图。示例:我有五个条形(1,2,3,4,5),得分为reduceCount。当所有柱都有值时,图表就可以了。

enter image description here

但是,当条形没有值时(在这种情况下,一个):

enter image description here

我尝试添加.y(d3.scale.ordinal().domain([1, 2, 3, 4, 5])),但此方法在行图图表中不存在。

1 个答案:

答案 0 :(得分:2)

即使没有任何值可以填充它们,您也可以使用“假组”来确保存在垃圾箱。

From the FAQ

function ensure_group_bins(source_group) { // (source_group, bins...}
    var bins = Array.prototype.slice.call(arguments, 1);
    return {
        all:function () {
            var result = source_group.all().slice(0), // copy original results (we mustn't modify them)
                found = {};
            result.forEach(function(d) {
                found[d.key] = true;
            });
            bins.forEach(function(d) {
                if(!found[d])
                    result.push({key: d, value: 0});
            });
            return result;
        }
    };
};

像这样使用:

var mod_group = ensure_group_bins(your_group, 1, 2, 3, 4, 5);
chart.group(mod_group)

我不会在这里使用.data(),因为它可以interfere with capped charts,其行图是一个。 (虽然期望它起作用是完全合理的。)

相关问题