score:5
You have three problems in your code:
There is no SVG element called
<node>
. Thus, instead of....append("node");
... it should be:
.append("g");
Text elements have no
cx
orcy
attribute.Instead of changing the
cx
orcy
ofnode
, which has none (since it is a<g>
element), you have to translate it:node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")" });
All together, this is your updated code:
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: black;
stroke-width: 0px;
}
<svg width="800" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// Create somewhere to put the force directed graph
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var radius = 20;
// DATA
var nodes_data = [{
"id": "Lillian",
"sex": "F"
}, {
"id": "Gordon",
"sex": "M"
}, {
"id": "Sylvester",
"sex": "M"
}, {
"id": "Mary",
"sex": "F"
}, {
"id": "Helen",
"sex": "F"
}, {
"id": "Jamie",
"sex": "M"
}, {
"id": "Jessie",
"sex": "F"
}, {
"id": "Ashton",
"sex": "M"
}, {
"id": "Duncan",
"sex": "M"
}, {
"id": "Evette",
"sex": "F"
}, {
"id": "Mauer",
"sex": "M"
}, {
"id": "Fray",
"sex": "F"
}, {
"id": "Duke",
"sex": "M"
}, {
"id": "Baron",
"sex": "M"
}, {
"id": "Infante",
"sex": "M"
}, {
"id": "Percy",
"sex": "M"
}, {
"id": "Cynthia",
"sex": "F"
}, {
"id": "Feyton",
"sex": "M"
}, {
"id": "Lesley",
"sex": "F"
}, {
"id": "Yvette",
"sex": "F"
}, {
"id": "Maria",
"sex": "F"
}, {
"id": "Lexy",
"sex": "F"
}, {
"id": "Peter",
"sex": "M"
}, {
"id": "Ashley",
"sex": "F"
}, {
"id": "Finkler",
"sex": "M"
}, {
"id": "Damo",
"sex": "M"
}, {
"id": "Imogen",
"sex": "F"
}];
var links_data = [{
"source": "Sylvester",
"target": "Gordon",
"type": "A"
}, {
"source": "Sylvester",
"target": "Lillian",
"type": "A"
}, {
"source": "Sylvester",
"target": "Mary",
"type": "A"
}, {
"source": "Sylvester",
"target": "Jamie",
"type": "A"
}, {
"source": "Sylvester",
"target": "Jessie",
"type": "A"
}, {
"source": "Sylvester",
"target": "Helen",
"type": "A"
}, {
"source": "Helen",
"target": "Gordon",
"type": "A"
}, {
"source": "Mary",
"target": "Lillian",
"type": "A"
}, {
"source": "Ashton",
"target": "Mary",
"type": "A"
}, {
"source": "Duncan",
"target": "Jamie",
"type": "A"
}, {
"source": "Gordon",
"target": "Jessie",
"type": "A"
}, {
"source": "Sylvester",
"target": "Fray",
"type": "E"
}, {
"source": "Fray",
"target": "Mauer",
"type": "A"
}, {
"source": "Fray",
"target": "Cynthia",
"type": "A"
}, {
"source": "Fray",
"target": "Percy",
"type": "A"
}, {
"source": "Percy",
"target": "Cynthia",
"type": "A"
}, {
"source": "Infante",
"target": "Duke",
"type": "A"
}, {
"source": "Duke",
"target": "Gordon",
"type": "A"
}, {
"source": "Duke",
"target": "Sylvester",
"type": "A"
}, {
"source": "Baron",
"target": "Duke",
"type": "A"
}, {
"source": "Baron",
"target": "Sylvester",
"type": "E"
}, {
"source": "Evette",
"target": "Sylvester",
"type": "E"
}, {
"source": "Cynthia",
"target": "Sylvester",
"type": "E"
}, {
"source": "Cynthia",
"target": "Jamie",
"type": "E"
}, {
"source": "Mauer",
"target": "Jessie",
"type": "E"
}, {
"source": "Duke",
"target": "Lexy",
"type": "A"
}, {
"source": "Feyton",
"target": "Lexy",
"type": "A"
}, {
"source": "Maria",
"target": "Feyton",
"type": "A"
}, {
"source": "Baron",
"target": "Yvette",
"type": "E"
}, {
"source": "Evette",
"target": "Maria",
"type": "E"
}, {
"source": "Cynthia",
"target": "Yvette",
"type": "E"
}, {
"source": "Maria",
"target": "Jamie",
"type": "E"
}, {
"source": "Maria",
"target": "Lesley",
"type": "E"
}, {
"source": "Ashley",
"target": "Damo",
"type": "A"
}, {
"source": "Damo",
"target": "Lexy",
"type": "A"
}, {
"source": "Maria",
"target": "Feyton",
"type": "A"
}, {
"source": "Finkler",
"target": "Ashley",
"type": "E"
}, {
"source": "Sylvester",
"target": "Maria",
"type": "E"
}, {
"source": "Peter",
"target": "Finkler",
"type": "E"
}, {
"source": "Ashley",
"target": "Gordon",
"type": "E"
}, {
"source": "Maria",
"target": "Imogen",
"type": "E"
}];
// Set up the simulation and add forces
var simulation = d3.forceSimulation()
.nodes(nodes_data);
var link_force = d3.forceLink(links_data)
.id(function(d) {
return d.id;
});
var charge_force = d3.forceManyBody()
.strength(-200);
var center_force = d3.forceCenter(width / 2, height / 2);
simulation
.force("charge_force", charge_force)
.force("center_force", center_force)
.force("links", link_force);
// Add tick instructions:
simulation.on("tick", tickActions);
// Add encompassing group for the zoom
var g = svg.append("g")
.attr("class", "everything");
// Draw lines for the links
var link = g.append("g")
.attr("class", "links")
.selectAll("line")
.data(links_data)
.enter()
.append("line")
.attr("stroke-width", 2)
.style("stroke", linkColour);
// Draw circles and texts for the nodes
var nodes = g.append("g")
.attr("class", "nodes");
var node = nodes.selectAll("node")
.data(nodes_data)
.enter()
.append("g");
var circle = node.append("circle")
.attr("r", radius)
.attr("fill", circleColour);
var text = node.append("text")
.text("asdasdasdas")
.attr("y", -22)
.attr("text-anchor", "middle");
// Add drag capabilities
var drag_handler = d3.drag()
.on("start", drag_start)
.on("drag", drag_drag)
.on("end", drag_end);
drag_handler(node);
// Add zoom capabilities
var zoom_handler = d3.zoom()
.on("zoom", zoom_actions);
zoom_handler(svg);
/** Functions **/
function circleColour(d) {
if (d.sex == "M") {
return "blue";
} else {
return "pink";
}
}
// Function to choose the line colour and thickness
function linkColour(d) {
return "black";
}
// Drag functions
function drag_start(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
// Make sure you can't drag the circle outside the box
function drag_drag(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function drag_end(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
// Zoom functions
function zoom_actions() {
g.attr("transform", d3.event.transform)
}
function tickActions() {
// update node positions each tick of the simulation
node.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"
});
// update link positions
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;
});
}
</script>
score:1
Here is what I suppose you want : https://jsfiddle.net/n504g8vw/1/
How did I do it ? First of all I removed the node element and changed it for a g element with the class node.
var node = nodes.selectAll("g")
.data(nodes_data)
.enter()
.append("g")
.attr('class', 'node');
Okay now the circles are displaying but not moving with the graph. I modified the tick function to translate the g element containing the circle and text.
node.attr('transform', function (d) {
return 'translate(' + d.x + ', ' + d.y + ')';
});
The nodes are now moving with the graph but the center of circle is not matching the center of the nodes. You have to set the center of each circle to 0,0.
var circle = node.append("circle")
.attr("r", radius)
.attr("fill", circleColour)
.attr("cx",0)
.attr("cy", 0);
Finally you want the text to be centered on the node so you put some css on the text, you can do it with D3 like this (or directly on your css sheet)
var text = node.append("text")
.text("asdasdasdas")
.style("text-anchor", "middle");
Source: stackoverflow.com
Related Query
- D3 v4 moving both circle and text at the same time
- d3: hovering over words in text at the same time highlighting the corresponding bars and make word in text clickable
- How can I make both HTTP and Websocket requests at the same time on the same server?
- d3 zoom and brush working at the same time
- D3.js nesting and rollup at the same time in v4
- What is the proper way to both rotate and translate text in d3?
- How to have the same scale in both x and y
- How to automatically move nodes and links at the same time in d3.js
- Moving all nodes the same time including links?
- set both title and text of circle on d3 not working
- animate grow and radius at the same time
- force directed graph - define the color of a node, if the node is source and target at the same time
- D3 - How can I show/hide a text element when hovering a circle element created from different attributes of the same data entry?
- Can't get 4 circle to appear at the same time
- Transitioning xAxis and data at the same time - D3.js
- text and input elements appended and given attributes the same way. Text shows but not the input. Why?
- Add text on hover, image on the svg circle and make the nodes less sticky in a d3 graph
- How can I change the radius and opacity of a circle in d3?
- D3: How do I set "click" event and "dbclick" event at the same time?
- AngularJS, D3 and JQuery in the same page. D3 can't read the correct DOM dimensions
- D3 v4 Brush and Zoom on the same element (without mouse events conflicting)
- D3, SVG and textPath: How to move the text down?
- How to group by multiple keys at the same time using D3?
- How to place text on the circle when using D3.js force layout?
- d3.js: run the same code in Angular app and on node.js
- Wrap text in a circle with D3 v4 and D3plus
- Mouseover event on two charts at the same time d3.js
- Why are both of these lines coming up the same color?
- How to get the boundaries of currently visible time scale (after panning and zooming)?
- Arrow head and link of the same color in D3 js graph
More Query from same tag
- Update the heatmap based on the selected data
- d3.v3 scatterplot with all circles the same radius
- Colour of points repeat after 10 data sets on D3.js scatter plot
- d3js not having the same behaviour within and outside a function
- Circle in D3.js only displaying text without circle
- Using D3 javascript diagram without sending id of the element
- how to create separate bar chart with JSON array set?
- Coloring svg elements depending on data value with d3.js
- how to fix weird drag behavior?
- Implement discontinuous bar chart with D3.js
- Tooltip in the network graph is flashing on and off
- Nest multiple rects inside multiple svg containers with d3?
- Map zooming out of svg in IE9
- Same X-axis Multiple Line Charts using d3
- how to include use of node_modules in rollup compile
- How can I dynamically change the position and the size of direction arrows on a directed d3.js force layout?
- Deep clone a function in javascript
- D3 Transition Path Left And Shift Up/Down Simultaneously
- Update words(size change or remove words) in D3 word cloud without changing their position?
- how to draw a d3 pie chart from a json file
- How can I change the radius and opacity of a circle in d3?
- Proper Convention for Injecting D3 into AngualrJS
- D3 using classed() to add and remove class with checkbox
- How can I open and parse a local csv file into an array?
- D3js collision not updating
- Why does d3.max() output second largest number?
- D3 Choropleth Map: Colors are not appearing
- D3 unable to select two elements in mouseover
- given 1 chart in d3 fed by 1 file + want to draw X charts given an array of X files + d3
- D3 inserting a chart within the tooltip