当返回label
等于legend
时,想要从饼图的value
隐藏0
。任何人都可以在NVD3.js中指出我正确的方向吗?
nv.addGraph(function () {
var donutChart = nv.models.pieChart()
.x(function (d) {
return d.label
})
.y(function (d) {
return d.value
})
d3.select("#chart-devices svg")
.datum(data)
.transition().duration(1200)
.call(donutChart);
nv.utils.windowResize(donutChart.update);
return donutChart;
});
一个可能的答案是在renderEnd
事件中删除值为0的项目:
chart.dispatch.on('renderEnd', function () {
console.log("renderEnd");
d3.selectAll(".nv-legend .nv-series")[0].forEach(function (d) {
//get the data
var t = d3.select(d).data()[0];
// remove item from legend
if (t.value == 0)
d3.select(d).remove();
});
});
另一种可能性是在超时后删除项目:
setTimeout(function () {
d3.selectAll(".nv-legend .nv-series")[0].forEach(function (d) {
//get the data
var t = d3.select(d).data()[0];
// remove item from legend
if (t.value == 0)
d3.select(d).remove();
});
}, 1);
在这两种情况下,其余项目之间存在差距。
这是一个小提琴(第一个选项):https://jsfiddle.net/beaver71/yt7vrohk/