I have a couple of years experience with d3.js and have run into this kind of problem frequently.
I can't se your code, but I"m new to freelancer, so maybe I'm just missing where it's posted!
Anyway, if your "rectangles" are part of an svg, then it's probably possible to fix this by adding groups and then appending the text.
I hope I can help you out.
All my best, Elise