I am new to D3 and was playing with D3 network diagram. I can successfully create a network diagram and make it draggable but I was not able to add labels to nodes. I searched for answers and I think my code should work. Here is the code.
Note: The layout of the network did not properly render here (but the nodes can be dragged to see the proper layout )but it renders properly (without the label) when it ran on full web page.
Please point out where did I do wrong.
Thanks a lot,
Alex
var data = {
"name": "A1",
"children": [{
"name": "B1",
"children": [{
"name": "C1",
"value": 100
},
{
"name": "C2",
"value": 300
},
{
"name": "C3",
"value": 200
}
]
},
{
"name": "B2",
"value": 200
}
]
};
var root = d3.hierarchy(data);
function dragged(d) {
d.x = d3.event.x, d.y = d3.event.y;
d3.select(this).attr("cx", d.x).attr("cy", d.y);
link.filter(function(l) {
return l.source === d;
}).attr("x1", d.x).attr("y1", d.y);
link.filter(function(l) {
return l.target === d;
}).attr("x2", d.x).attr("y2", d.y);
}
// Nodes
var node = d3.select('#network g.nodes')
.selectAll('circle.node')
.data(root.descendants())
.enter()
.append('circle')
.classed('node', true)
.attr('cx', function(d) {
return d.x;
})
.attr('cy', function(d) {
return d.y;
})
.attr('r', 4)
.each(function(d) {
console.log(d);
d3.select(this)
.append('text')
.text(d.data.name);
})
.call(d3.drag().on("drag", dragged));
// Links
var link = d3.select('#network g.links')
.selectAll('line.link')
.data(root.links())
.enter()
.append('line')
.classed('link', true)
.attr('x1', function(d) {
return d.source.x;
})
.attr('y1', function(d) {
return d.source.y;
})
.attr('x2', function(d) {
return d.target.x;
})
.attr('y2', function(d) {
return d.target.y;
});
/*
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) {
console.log(d);
return d.data.name;
});
*/
circle {
cursor: pointer;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1px;
}
svg text {
color: #000;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.2/d3.min.js"></script>
<svg id="network" width="400" height="220">
<g transform="translate(20, 5)">
<g class="links"></g>
<g class="nodes"></g>
</g>
</svg>