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

五迷三道 提交于 2019-12-18 18:39:11

问题


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.



来源:https://stackoverflow.com/questions/21538336/dc-js-crossfilter-how-to-sort-data-in-dc-js-chart-like-row-ascending-x-de

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