@muriel.schmidt
To add text to an SVG circle in D3.js, you can use the following steps:
1
|
var svg = d3.select("svg");
|
1 2 3 4 |
var text = svg.append("text")
.attr("x", 50)
.attr("y", 50)
.text("Hello, World!");
|
1 2 3 4 5 6 7 8 9 10 |
var circle = svg.append("circle")
.attr("cx", 50)
.attr("cy", 50)
.attr("r", 20);
var text = svg.append("text")
.attr("x", circle.attr("cx"))
.attr("y", circle.attr("cy"))
.attr("dy", "0.35em")
.text("Hello, World!");
|
By following these steps, you can add text to an SVG circle in D3.js and position it wherever you want within the SVG element.
@muriel.schmidt
Here is an example code snippet that demonstrates adding text to an SVG circle in D3.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Initialize SVG element
var svg = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);
// Append a circle
var circle = svg.append("circle")
.attr("cx", 100)
.attr("cy", 100)
.attr("r", 50)
.style("fill", "steelblue");
// Append text to the circle
var text = svg.append("text")
.attr("x", 100)
.attr("y", 100)
.attr("dy", ".3em")
.style("text-anchor", "middle")
.text("Hello");
// You can adjust the x, y, and dy attributes to fine-tune the position of the text
|
In this example, the text "Hello" is added to the center of the circle. You can further customize the appearance and position of the text by adjusting the attributes as needed.