Desi Delite
Desi Delite

Reputation: 191

D3 : How to add labels top of in each bar in grouped bar chart

I designed grouped bar chart using this. I need to add labels in top of each bars. I went through many links and I knew that I needed to use text-anchor for this case. But I still can't find how to use it.I'm newer for D3.js and sometime this may be very easy little thing. Can anyone give me a help? Thank you

Upvotes: 2

Views: 2653

Answers (1)

somename
somename

Reputation: 998

You will need to write a text function similar to rect that shows the bars. See this plnkr for working example.

  state.selectAll("text")
  .data(function(d) { return d.ages; })
        .enter().append("text")
        .attr("class","barstext")
        .attr("x", function(d) { return x1(d.name); })
        .attr("y",function(d) { return y(d.value); })
        .text(function(d){console.log(d.value/1000000); return (d.value/1000000).toFixed(2) +"M";})

Upvotes: 5

Related Questions