-->

dc.js/crossfilter -> How to sort data in dc.js cha

2019-02-11 06:31发布

问题:

How to sort data in dc.js chart (like row) - Ascending x Descending

I want to reorder the chart (row/column) by specified attribute (like 'avg' -> ascending)

I'm trying to use ".top()"... but unsuccessfully

Thanks

draft below jsfiddle -> ewr5Z/2/

回答1:

You can use the ordering method:

        chart.ordering(function(d){ return -d.value })

If you write a custom reduce, you can have more flexibility:

        priceDimension  = ndx.dimension(function(d) {return d.part_number; });
        priceGroup = priceDimension.group().reduce(
            function (p, v) {
                ++p.count;
                p.sumPrice += v.price;
                p.avgPrice = p.sumPrice/p.count;
                return p;
            },
            function (p, v) {
                --p.count;
                p.sumPrice -= v.price;
                p.avgPrice = p.sumPrice/p.count;
                return p;
            },
            function () {
                return { count:0, sumPrice:0, avgPrice};
            });
    ....
    chart.ordering(function(d){ return d.value.avgPrice});

If you want the user to be able to change the sort order, you'd need to build a sort button that changes the ordering and rerenders the chart.