dc.js piechart legend - hide if result is 0

家住魔仙堡 提交于 2019-12-24 01:47:33

问题


Is it possible to remove/hide the legends for a piechart if the result is 0?

I have a piechart that has quite a few items in the legend, when there has been some filtering it would be great to remove the legends that are not available.

Any help would be appreciated.


回答1:


Legends do render when their charts are redrawn, but the problem here is that the legend is drawn from the data, and crossfilter doesn't automatically eliminate empty groups.

It would be really great if legends were a chart type, so we could just use a fake group (a.k.a. "data transform"). But no, we need to update .legendables() to filter out the empty bins:

dc.override(pieactChart, 'legendables', function() {
    var legendables = this._legendables();
    return legendables.filter(function(l) {
        return l.data > 0;
    });
});

Fork of your fiddle: http://jsfiddle.net/gordonwoodhull/13t804u6/5/

Note: this only modifies the one (left) chart, you'll have to copy/paste it for each chart (or wrap it in a function) to apply it to other charts.

[I am very stubborn about not wanting such data filtering stuff inside the charts, so I'm not going to suggest this as a feature. Instead, legend should be a chart which takes its data from another chart, and there should be a way to transform that data.]



来源:https://stackoverflow.com/questions/29371256/dc-js-piechart-legend-hide-if-result-is-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!