5

I want to create a path with d3.js that has dates for the x-axis. This is working perfectly, but my problem is, that my json-object isn't sorted so that the path is not correct: enter image description here

I couldn't find a function that sorts my data - or do I have to write one by my own? If yes, I would try it but if there is another possibility.. ;)

            var line = d3.svg.line()
                .interpolate("linear")
                .x(function(d) { return x(d.finished_at); })
                .y(function(d) { return y(d.result); });

            svg.selectAll("path")
                .data(data)
            .enter().append("path")
                .attr("class", "line")
                .attr("d", line(data));

Has anyone an idea? Thanks!

1 Answer 1

5

Have you looked at d3.ascending? https://github.com/mbostock/d3/wiki/Arrays#wiki-d3_ascending

or you can do this...

Sorting a selection in d3.js disturbs the data-join

2
  • 4
    Thanks for your reply. I think I didnt got the sense before ;) Now I just wrote a function at the beginning that sorts very easiliy: data.sort(function(a, b){ return d3.ascending(a.finished_at, b.finished_at); });
    – cruxi
    Commented Nov 30, 2012 at 13:09
  • Yeah...when playing with d3.js it took me a few days to understand the API too. Commented Nov 30, 2012 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.