Adding label on a D3 bar chart

后端 未结 1 976
一向
一向 2020-12-10 14:14

I read a lot of documentation about adding label on a D3 bar chart but i can\'t figure it out. I am stuck with what to add after the \"svg.selectAll(\"text\")\".

Th

相关标签:
1条回答
  • 2020-12-10 14:59

    This should work !

    var yTextPadding = 20;
    svg.selectAll(".bartext")
    .data(data)
    .enter()
    .append("text")
    .attr("class", "bartext")
    .attr("text-anchor", "middle")
    .attr("fill", "white")
    .attr("x", function(d,i) {
        return x(i)+x.rangeBand()/2;
    })
    .attr("y", function(d,i) {
        return height-y(d)+yTextPadding;
    })
    .text(function(d){
         return d;
    });
    

    Notice how I use a class (.bartext) to identify the chart labels.

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