I'm struggling to label the nodes in my force-directed graph created with d3.js. Despite trying various solutions from StackOverflow and online tutorials, I believe my issues stem from a lack of fundamental understanding of JavaScript. https://i.sstatic.net/1jSxJ.png
I've experimented with different combinations of .attr/.append/.text functions to display the source and target text on the nodes, but nothing seems to work.
Here's the part causing trouble:
node.append("title")
.text(function (d) {return d.target});
node.append("text")
.attr("dy", -3)
.text(function (d) {return d.source})
.attr("class", "font");
Below is a simplified snippet of the styles:
<style>
.node {
fill: #ccc; /* Circle fill color */
stroke: #ffffff;
stroke-width: 2px;
}
.font {
font: 10px;
font-family: sans-serif;
}
.link {
stroke: #777; /* Line color */
stroke-width: 2px;
}
</style>
Next is an excerpt from the script:
var width = 640,
height = 480;
var links = [
// Array of links
{source: "Germany", target: "name1"},
{source: "Germany", target: "name2"},
...
// Node setup
var svg = d3.select("body").append("svg")
...
var node = svg.selectAll(".node")
...
// Adding text elements to nodes
node.append("title")
...
node.append("text")
...
function tick(e) {
...
}
</script>
I'm encountering difficulties without any error messages for guidance. Any assistance would be greatly appreciated. Thank you!