I draw line charts with d3 and everything works fine. However, I should leave enough margin to the left of the chart area to fit what, in my opinion, might be the widest text label of the y axis. I would like to customize this space for each chart, depending on the widest label.
Initially, I thought that I could find the maximum value of y, create a hidden text object, find out how wide it is, and use this value for the left margin when creating the chart. A little disgusting, but it brings me meaning.
However, if the maximum y value is, say, β1598.538β, the topmost label of the Y axis can be β1500β ... i.e. much narrower.
So, I think I want to find the width of what will actually be the brightest label. But I canβt think how to do this without drawing a diagram and an axis, measuring this width and not drawing it again for real. What sounds disgusting! Is there an unpleasant way to do this?
UPDATE
Here is part of my code using Lars' suggestion to show where it fits:
Phil gyford
source share