Updating dc.js data and reapplying original filters

前端 未结 1 1032
猫巷女王i
猫巷女王i 2021-01-06 01:58

I\'m trying to build a reproducible example of this question about how to replace a crossfilter data restoring dimensions and groups - i.e. reapplying any filters created by

1条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 03:00

    Here is your example working: http://jsfiddle.net/pm12xf3z/

    There were several problems that are corrected, but most importantly, don't rebuild your Crossfilter, your dimensions, or your groups. That is unnecessary. The existing dimensions and groups will be updated with the new data.

    Just remove the old data from your Crossfilter (ndx.remove() with no filters in place), then add your new data (ndx.add(data2)), then tell dc.js to update itself (dc.redrawAll()). That's all you need to do to your Crossfilter.

    Then the question is how you remove all filters and maintain your dc.js filters in the process? The key is to interact with your dc.js charts, not your dimensions directly. For your ordinal selection charts, you can do the following:

    function resetData(ndx, dimensions) {
        var yearChartFilters = yearRingChart.filters();
        var spenderChartFilters = spenderRowChart.filters();
        yearRingChart.filter(null);
        spenderRowChart.filter(null);
        ndx.remove();
        yearRingChart.filter([yearChartFilters]);
        spenderRowChart.filter([spenderChartFilters]);
    }
    

    That is, grab the filters from the charts, set the filters on the charts to null, do your Crossfilter data removal, then add the filters back to the charts. The exact filter format is kind of screwy, and the fact that you have to put the output of .filters() in an array to make it work with .filter() is a bit strange, but things are getting better in dc.js 2.0 betas.

    0 讨论(0)
提交回复
热议问题