DC.js - deselect feature or filter all but the ones that are clicked

前端 未结 2 1171
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-25 17:31

I\'m not sure if this is possible and no luck on researching this. I\'m working on a dashboard using DC.js charts and crossfilter.js. I\'m going to use a Row chart as an example

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-25 17:58

    I'm using this on the rowcharts where I need this feature.
    It's a little bit shorter than the other answer, maybe it can be useful to someone (I don't know which is better).

        // select only one category at a time
        mychart.onClick = function(_chart){ return function (d) {
            var filter = _chart.keyAccessor()(d);
            dc.events.trigger(function () {
                _chart.filter(null);
                _chart.filter(filter);
                _chart.redrawGroup();
            });
        }; }(mychart)
    

    Where mychart is the name of your dc.rowchart

        var mychart = dc.rowChart('#mychart');
    

    [EDIT]

    This one works too, is shorter, and works with barcharts

        mychart.addFilterHandler(function (filters, filter) {
            filters.length = 0; // empty the array
            filters.push(filter);
            return filters;
        });
    

提交回复
热议问题