Currently, I am utilizing d3.symbolTriangle to create equilateral triangles in my project. I am interested in learning how to access and modify the attribute values.
This is a snippet of my code:
var body = d3.select("body");
var triangle = d3.symbol()
.type(d3.symbolTriangle);
Group = [];
var svg = body.append("svg")
.attr("width", width)
.attr("height", height)
.style("border", "1px solid black")
.on("mousemove", function() {
mouse = d3.mouse(this);
isMouseMoving=true;
});
function drawTriangles(number) {
for (var i = 0; i < number; i++) {
Group[i] = svg.append("g")
.attr("id",i);
var dim = Math.floor(Math.random() * 800 + 50);
var r = Math.random()*360;
Group[i].append("path")
.attr("d", triangle.size(dim))
.attr("transform", function (d) {
var boundingBox = this.getBBox();
var elementWidth = Math.ceil(boundingBox.width);
var randomXOffset = Math.random() * (width - elementWidth - 2 * borderSize) + elementWidth/2 + borderSize;
var randomYOffset = Math.random() * (height - elementWidth - 2 * borderSize) + elementWidth/2 + borderSize;
return "translate(" + randomXOffset + "," + randomYOffset + ")rotate("+r+")";
})
.attr("fill", "rgb(" + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + ")")
.attr("opacity", 2)
.attr("stroke", "black")
.attr("proof", "Hello")
.attr("radius", function() {
return Math.sqrt(dim/(Math.sqrt(3)*3));
});
}
}
If I wish to retrieve the values of translate (the position of the triangle) or radius, and then set new values, for instance during a transition, how can I accomplish this? I attempted the following approach:
Group[i]._groups[0][0].childNodes[0].attributes["radius"].nodeValue
However, I am doubtful if this is the correct method to achieve my goal.