dc.js sort ordinal line chart by y axis/value

前端 未结 3 1089
情话喂你
情话喂你 2020-12-10 20:04

I have a dc.js ordinal chart whose x-axis consists of things like \'Cosmetics\' and the y-axis is the number of sales. I want to sort the chart by sales decreasing, however

3条回答
  •  遥遥无期
    2020-12-10 20:25

    This is the hack I ended up doing. Be aware that it could have performance issues on large data sets as all() is faster than top(Infinity). For some reason I couldn't get Gordon's answer to work, but in theory it should.

    On my group I specified an order function

    group.order(function(p) {
        return p.myfield;
    });
    

    Then because all() doesn't use the order function I overrode the default all function

    group.all = function() {
        return group.top(Infinity);
    }
    

    And then on my chart I had to specify an order function

    chart.ordering(function(d){
        return -d.value.myfield
    }); // order by myfield descending
    

提交回复
热议问题