I am creating a legend for my chart. I need to dynamically select x values for the different elements, based on the width of the preceding element. I am using getBoundingClientRect() for this. The problem is that it is returning the same values for all the different elements(The value of the first element). I am logging it to console.
var primaryTotal = legend.append('text')
.attr("x", function () {
console.log(nameText.node().getBoundingClientRect());
return ((nameText.node().getBoundingClientRect().right)+100);
})
.attr("y", 0)
.text(function (d) {
return d["total"] + " " + parameter[DS1].units_scaled;
})
.style("fill", function (d) {
return d["colour"];
})
nameText is the element that I am drawing before primaryTotal.
var legend = groups.selectAll('.legend')
.data(rowObjectArray)
.enter()
.append('g')
.attr("transform", function (d, i) {
// console.log(d);
// console.log(i);
return "translate(0," + ((i * 20) + 20) + ")";
})
I am appending it to the group created here.
var legendRect = legend.append('rect')
.attr("x", 0)
.attr("y", -10)
.attr("width", 10)
.attr("height", 10)
.style("fill", function (d, i) {
return d["colour"];
})
var nameText = legend.append('text')
.attr("x", function () {
return (legendRect.node().getBoundingClientRect().right);
})
.attr("y", 0)
.text(function (d) {
return d["name"];
})
.style("fill", function (d) {
return d["colour"];
})
declaration of nameText and legendRect. They are both elements I am using in my legend.
This is what is getting logged to console....
console log showing the getBoundingClientRect() output
Don't know how to proceed. Any help would be greatly appreciated. Thank you