I'm using a combination of AngularJS and D3.js to dynamically draw line graphs based on data retrieved from an API.
Below is a picture of my y-axis. As you can see, I have a drawn a black vertical line representing 0 to 100%. I have 5 tick marks labelled with their associated percentages.
Here is the code that generates this:
var yScaleIntervals = 4;
var yMaxlabel = 100;
for (var i = 0; i <= yScaleIntervals; i++) {
var tickY = self.yBottom - (self.yPixSpan * (i/yScaleIntervals));
var tickLabel = ((100 * i)/yScaleIntervals).toString() + '%';
console.log("tickY = ", tickY);
self.svgContainer.append("line")
.attr("x1", self.yScaleX - 2)
.attr("y1", tickY)
.attr("x2", self.yScaleX)
.attr("y2", tickY)
.attr("stroke-width", 1)
.attr("stroke", "black");
var YfontOffset = 30;
var XfontOffset = 4;
var label = self.svgContainer.append("text")
.attr("x", yScaleX - YfontOffset)
.attr("y", tickY + XfontOffset)
.text(tickLabel)
.attr("font-family", "Courier New")
.attr("font-weight", "bold")
.attr("font-size", "10px");
.attr("fill", "black");
}
As you can see, I have hardcoded YfontOffset
and XfontOffset
to re-position the text to appear somewhat adjacent to those tick marks.
But this is not what I really want. I want each text label to appear the same number of pixels to to the left of their associated tick marks. And I want the tick marks to be exactly horizontally in the middle of the text. Thus the x
and y
attributes of each label must be computed based on the bounding box of the text. How can I get the dimensions of those bounding boxes before the text is drawn on the canvas so I can use them to in those attributes?