Reputation: 105
I'm new to D3 and I cannot seem to find a way to implement node labels plus zoom/pan at the same time on my D3.js v4 forced-directed graph. My code is as below.
Any help with this would be greatly appreciated
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: black ;
stroke-width: 0px;
}
.svg-container {
display: inline-block;
position: relative;
width: 100%;
padding-bottom: 100%;
vertical-align: top;
overflow: hidden;
}
.svg-content {
display: inline-block;
position: absolute;
top: 0;
left: 0;
}
</style>
<div id="container" class="svg-container">
</div>
<!--<svg viewBox="0 0 300 300"></svg>-->
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var width = 3000;
var height = 3000;
//create somewhere to put the force directed graph
var svg = d3.select("div#container")
.append("svg")
.attr("preserveAspectRatio", "xMinYMin meet")
.attr("viewBox", "0 0 3000 3000")
.classed("svg-content", true);
var color = d3.scaleOrdinal(d3.schemeCategory20);
var radius = 5;
var nodes_data; // global
var links_data; // global
d3.json("features_map_export.json", function(error, data) {
nodes_data = data.nodes
links_data = data.links
var simulation = d3.forceSimulation()
.nodes(nodes_data);
var link_force = d3.forceLink(links_data)
.id(function(d) { return d.name; });
var charge_force = d3.forceManyBody()
.strength(-100);
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 for the nodes
var node = g.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(nodes_data)
.enter()
.append("circle")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); });
node.append("title")
.text(function(d) { return d.name; });
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.category });
//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 to choose what color circle we have
//Let's return blue for males and red for females
function circleColour(d){
return "pink";
}
//Function to choose the line colour and thickness
//If the link type is "A" return green
//If the link type is "E" return red
function linkColour(d){
return "green";
}
//Drag functions
//d is the node
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 circle positions each tick of the simulation
node
<!--.attr("cx", function(d) { return d.x; })-->
<!--.attr("cy", function(d) { return d.y; })-->
.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>
Upvotes: 1
Views: 1078
Reputation: 152
I have been doing quite a bit of work in this area recently, have a look at this complete solution in plunker...
http://plnkr.co/edit/ZSmvH05nnAD6cYZb0EM4?p=preview
The key bit here is:
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended))
Then just include the boiler-plate drag functions:
//Used to drag the graph round the screen
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
As for the zoom, this is just a case of attaching the zoom handler to the top level svg container... see the Plunker for details.
svg.call(zoom_handler)
.call(zoom_handler.transform, d3.zoomIdentity.scale(1,1))
;
The initial zoom can be set by changing the scale values:
.call(zoom_handler.transform, d3.zoomIdentity.scale(0.5,0.5))
will be half the size.
As for attaching your labels, this should hopefully be fairly obvious from the Plunker.
Hope this helps
Upvotes: 4