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

前端 未结 3 1076
情话喂你
情话喂你 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
    
    0 讨论(0)
  • 2020-12-10 20:36

    No doubt this is a bug.

    As a workaround, you could sort the data yourself instead of using the ordering function, as described in the FAQ.

    I filed a bug report: https://github.com/dc-js/dc.js/issues/598

    0 讨论(0)
  • 2020-12-10 20:42

    It does appear to be a bug. I too couldn't make it work as intended. I implemented a workaround; I added an example in a Plunker, link is (also) in the dc.js issue at github. It is based upon a recent snapshot of dc.js 2.0.0-dev. (So for now I guess this could be considered an answer)

    http://embed.plnkr.co/VItIQ4ZcW9abfzI13z64/

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