I am trying to append a text to a line created on bar chart. The problem is that I can see that the text element is created. But it does not show anywhere on the svg. part of code where I append text
var line = svg.append("line")
.attr("x1", function(){ return x(lineEnd)})
.attr("x2", function(){ return x(lineEnd)})
.attr("y1", 0)
.attr("y2", height)
.attr("stroke-width", 6)
.attr("stroke", "black")
.attr("stroke-dasharray", "8,8")
line.append('text')
.attr('class', 'barsEndlineText')
.attr('text-anchor', 'middle')
.attr("x", 0)
.attr("y", ".35em")
.text('I am label')
I have seen this before but cant seem to find it. Basically you need a container for both the line and text so when you translate the line the text moves with it :
Updated PLNKR : http://plnkr.co/edit/lASjq4ysrYGqWUGtMgRd?p=preview
You can't append a
text
to aline
. Just create another variable for the text: