d3 bar chart is upside down

前端 未结 3 1211
甜味超标
甜味超标 2021-01-02 03:11

I have a simple bar chart drawn in d3, with vertical bars: http://jsfiddle.net/philgyford/LjxaV/2/

However, it\'s drawing the bars down, with the baseline at the top

相关标签:
3条回答
  • 2021-01-02 03:35

    Per the d3 basic bar chart : http://bl.ocks.org/mbostock/3885304

    You are correct in inverting the range.

    Additionally, your rectangles should be added like this:

        .attr('y', function(d) { return y(d.percent); } )
        .attr('height', function(d,i){ return height - y(d.percent); });
    
    0 讨论(0)
  • 2021-01-02 03:37

    The x and y coordinates for svg start in the top left. You want the y to start on the bottom. The code below assumes you're appending to some function along the lines of:

    svg.selectAll('rect')
        .data(dataset)
        .enter()
        .append('rect')
    

    To make the bar plot act as you desire, set the y attribute to begin at distance data[i] above the axis:

    .attr('y', function(d) { return height - d; })
    

    Then, you must make the distance extend the remaining data[i] to the axis.

    .attr('height', function(d) { return d; }) 
    

    And that's it!

    0 讨论(0)
  • 2021-01-02 03:42

    Setting the y attribute seems to work:

    .attr('y', function(d){ return (height - parseInt(y(d.percent))); })
    

    jsfiddle here

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