score:55
Accepted answer
you are adding a text element inside a circle, that won't work. you can add groups under the svg element and then append the circle and a text in each group:
// create the groups under svg
var gnodes = svg.selectall('g.gnode')
.data(graph.nodes)
.enter()
.append('g')
.classed('gnode', true);
// add one circle in each group
var node = gnodes.append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
// append the labels to each group
var labels = gnodes.append("text")
.text(function(d) { return d.name; });
force.on("tick", function() {
// update the links
link.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; });
// translate the groups
gnodes.attr("transform", function(d) {
return 'translate(' + [d.x, d.y] + ')';
});
});
a fork of your fiddle with this strategy is here
Source: stackoverflow.com
Related Query
- Add text label to d3 node in Force directed Graph and resize on hover
- How to use hover and add text over node in d3 force simulation
- unable to add label to node on force directed graph
- D3 force directed graph with drag and drop support to make selected node position fixed when dropped
- Add text label to d3 node in Force layout
- How to add text in the center of node in force directed graph?
- Both single and double click on a node in d3 force directed graph
- D3 force directed graph node - text is being duplicated when expading
- Highlight a node and its neighbour node in force directed graph
- D3.js Donut Chart change node colors and add text label to middle
- How can I append text to and render that text from a line in a force directed graph in D3.js?
- styling node label not working in d3 force directed graph
- force directed graph - define the color of a node, if the node is source and target at the same time
- Vertically center a label consisting of multiple lines over a node in D3 force directed graph
- how to display the node attributes like person's name photo and address in a force directed graph
- How can I add text to edges in a force directed graph d3?
- How to add text to a force directed graph in D3.js
- Highlight selected node, its links, and its children in a D3 force directed graph
- Add text/label onto links in D3 force directed graph
- Is there a tap and double tap event in d3.js force directed graph
- D3 highlight selected node, and its links to parent and ancestors in a force directed graph
- Labels / text on the nodes of a D3 force directed graph
- d3js force directed - On hover to node, highlight/colourup linked nodes and links?
- How to have a text label on links in d3 force directed graphs
- Prevent node overlap in force directed graph
- Zooming and brushing in d3 force directed graph
- Adding label to the links in D3 force directed graph
- How to calculate in-degree, out-degree and weigted degree for force directed graph (d3.js)?
- How to add a dynamic legend to a D3 force directed graph in Apex?
- Freezing force directed network graph and make draggable in D3
More Query from same tag
- Plot rolling/moving average in d3.js
- Focused part shows wrong chart (NVD3)
- Wordcloud update data/ working example of adding words to cloud?
- D3.js - layouts - value accessor
- Correctly Overlay DC.JS (D3) canvas elements
- d3.js Uncaught (in promise) TypeError: d3.forceSimulation is not a function
- How to group by multiple keys at the same time using D3?
- d3 - Create new element from the dataset for each node in the selection
- AJAX is not a function error in d3/flask interface
- Having issue with translate function
- Can d3.js graph put inside a table?
- How to change negative Y axis domain to be positive
- labels on x axis in scale time d3.js
- D3: update path link position
- Zooming and brushing in d3 force directed graph
- How to use d3.js in Phoenix?
- Can't access array in Javascript/d3
- Error: <rect> attribute y: Expected length, "NaN"
- javascript - Compare and match lengths of two array having with similiar key/value pairs
- built-in for generating array of plots?
- How to skip x axis labels in d3.js chart dynamically to have always a pretty chart?
- Return multiple values from a crossfilter dimension for a pie chart
- Migrate code from a tree layout chart in version 3 to version 5
- How to reach a URL based on a node click?
- d3 stacked bar chart values not showing up on chart
- How to solve duplicate label in zoom
- I am trying to reproduce an example with NVD3, but not working
- add sparkline to d3.js table generated from nested JSON data
- d3 update old scale when pan and zoom
- dynamically update d3.js treemap