在 dc.js 中使用饼图时,如果值为 0,是否有办法隐藏饼图切片?
在我用来生成饼图的代码中,您可以看到我的键是一个包含多个指标的对象。如果给定指标的值为 0,我想从图表中隐藏饼图切片。
Summary.prototype.drawPie = function (name, metric, selector, labelFormatter) {
var chart = dc.pieChart(selector, this.chartGroup);
chart.width(200).height(200)
.slicesCap(7)
.dimension(this.dimension)
.group(this.group)
.valueAccessor(function (d) { return d.value[metric]; })
.legend(dc.legend().x(10).y(225).gap(5).horizontal(true).itemWidth(100).legendWidth(200))
.minAngleForLabel(.75)
.label(function (d) {
if (d.key == 'Others') {
return labelFormatter(d.value);
} else {
return labelFormatter(d.value[metric]);
}
})
.title (function (d) {
var value = (d.key == 'Others' ? d.value : d.value[metric]);
return d.key + ': ' + labelFormatter(value);
})
.ordering(function (d) { return 0-d.value[metric]; });
this.charts[name] = chart;
chart.render();
}
最佳答案
dc.js 2.0 在基本 mixin 上有一个 .data()
函数,它允许您在大多数情况下预先过滤组。
它适用于饼图,但会干扰上限。
请参阅此问题的示例:
https://github.com/dc-js/dc.js/issues/494
一个更通用但更丑陋的解决方案是 fake group .
关于javascript - 如果 dc.js 饼图/图例中的值为 0,则隐藏这些条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22072408/