Connect SVG circles using Javascript - javascript

I am trying to connect SVG circles and rectangles by drawing lines between source circles and target rectangles. Here is the format of my json file:
[{"sourceNode":"1","type":"sourceNode"},
{"sourceNode":"3","type":"sourceNode"},
{"sourceNode":"8","type":"sourceNode"},
.....
{"targetNode":"1","type":"targetNode"},
{"targetNode":"7","type":"targetNode"},
{"targetNode":"1","type":"targetNode"},
.....
{"type":"link","source":"1","target":"2"},
{"type":"link","source":"3","target":"4"},
{"type":"link","source":"3","target":"5"}]
I am using a tick function to give attributes to circles and the line. The circles work just fine, but I don't get lines with no attributes when I inspect my SVG in html.
Here is the code :
var nodeSource = g.selectAll("circle")
.data(data.filter(function (d){ return d.type == "sourceNode"; }))
.enter().append("circle")
.attr("r", 5)
.style("fill", "blue")
.call(force.drag);
var nodeTarget = g.selectAll("rect")
.data(data.filter(function (d){ return d.type == "targetNode"; }))
.enter().append("rect")
.attr("width", 10)
.attr("height", 10)
.style("fill", "green")
.call(force.drag);
var link = g.selectAll("line")
.data(data.filter(function (d){ return d.type == "link"; }))
.enter().append("line")
.style("stroke-width", "2")
.style("stroke", "grey")
.call(force.drag);
function tick(e) {
nodeSource
.attr("cx", function(d) { return d.x = Math.max(radius(), Math.min(width() - radius(), d.x)); })
.attr("cy", function(d) { return d.y = Math.max(radius(), Math.min(height() - radius(), d.y)); });
nodeTarget
.attr("x", function(d) { return d.x = Math.max(radius(), Math.min(width() - radius(), d.x)); })
.attr("y", function(d) { return d.y = Math.max(radius(), Math.min(height() - radius(), d.y)); });
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; });
chart.draw()
}

When you're assigning x1, x2, etc. coordinates to your line, you're not actually giving them a value. In your json file, the data for links has:
"source": "1"
for example. Using d.source.anything won't return a value because "1" doesn't have any properties attached to it. If you want to get a reference to the node which has this number, you have to use d3 to find it:
line.attr('x1', function (d) {
return d3.selectAll('circle').filter(function (k) {
return d.source === k.sourceNode;
}).attr('cx');
})
Then, when you want to do the target nodes:
line.attr('x2', function(d) {
return d3.selectAll('rect').filter(function (k) {
return d.target === k.targetNode;
}).attr('x');
})

Related

How to wrap text in force layout d3 with long labels?

I am trying to wrap text which is parsed from a json response into my application.
I have two svg elements : rect and text. I want to find a way where I can wrap my svg text such that it fits in the rectangle.
( Picture Force layout with text labels and rectangles )
This is the link to the original project in which I have made modifications
Visualizing Reddit Discussions
setupGraph();
function setupGraph() {
$(".network").empty();
names = {};
nodecolor = {};
force = d3.layout.force()
.charge(-500)
.linkDistance(20)
.size([width, height]);
nodes = force.nodes(),
links = force.links();
force.on("tick", function() {
svg.selectAll("line.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;
});
svg.selectAll("rect.node")
.attr("x", function(d) {
return d.x - d.curWidth / 2;
})
.attr("y", function(d) {
return d.y - d.curHeight / 2;
});
svg.selectAll("text.node")
.attr("x", function(d) {
return d.x - d.curWidth / 2 + 8;
})
.attr("y", function(d) {
return d.y - d.curHeight / 2 + 20;
});
});
d3.select("svg").remove();
svg = d3.select("#chart").append("svg:svg")
.attr("width", width)
.attr("height", height)
.attr("class", "network");
}
function updateNetwork() {
var link = svg.selectAll("line.link")
.data(links, function(d) {
return d.source.id + "-" + d.target.id;
});
link.enter().insert("svg:line", "text.node", "rect.node")
.attr("class", "link")
.style("stroke-width", function(d) {
return 2;
})
.style("stroke", "gray")
.style("opacity", 0.1);
var node = svg.selectAll("rect.node")
.data(nodes, function(d) {
return d.id;
});
var node = svg.selectAll("text.node")
.data(nodes, function(d) {
return d.id;
});
var nodeEnter = node.enter().append("svg:rect")
.attr("class", "node")
.call(force.drag)
.attr("width", function(d) {
return d.curWidth;
})
.attr("height", function(d) {
return d.curHeight;
})
.style("opacity", 1.0)
.on("mouseover", displayTooltip)
.on("mousemove", moveTooltip)
.on("mouseout", removeTooltip)
.on("mouseover", function(d) {
d3.select(this).transition().attr("height", 100).attr("width", 100); //.style("fill", "red");
})
.call(force.drag)
var nodeEnterr = node.enter().append("svg:text")
.attr("class", "node")
.text(function(d) {
return d.name + ": " + d.body
})
.call(force.drag)
.style("opacity", 1.0)
.on("mouseover", displayTooltip)
.on("mousemove", moveTooltip)
.on("mouseout", removeTooltip)
.call(force.drag)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Grouping SVG circles using d3.js

I have this data :
[{"node":"A","group":"1","type":"node"},
{"node":"B","group":"2","type":"node"},
{"node":"D","group":"1","type":"node"},
{"node":"C","group":"2","type":"node"},
{"type":"link","interest":"1","source":"A","target":"B"},
{"type":"link","interest":"2","source":"A","target":"C"},
{"type":"link","interest":"10","source":"B","target":"C"},
{"type":"link","interest":"3","source":"D","target":"B"}]
Here is my code:
var force = d3.layout.force()
.nodes(data)
.size([+width(), +height()])
.gravity(.08)
.charge(-10)
.on("tick", tick)
.on("end", function(){
chart.dispatchEndDrawing()
})
.start();
var g = selection
.attr("width", width)
.attr("height", height);
var link = g.selectAll("line")
.data(data.filter(function (d){ return d.type == "link"; }))
.enter().append("line")
.style("stroke-width", function(d) { return d.interest; })
.style("stroke", "grey")
.call(force.drag);
var node = g.selectAll("circle")
.data(data.filter(function (d){ return d.type == "node"; }))
.enter().append("circle")
.attr("r", 5)
.style("fill", "blue")
.call(force.drag);
function tick(e) {
node
.attr("cx", function(d) { return d.x = Math.max(radius(), Math.min(width() - radius(), d.x)); })
.attr("cy", function(d) { return d.y = Math.max(radius(), Math.min(height()-10 - radius(), d.y)); });
link
.attr("x1", function(d) { return d3.selectAll('circle').filter(function (k) { return d.source === k.node; }).attr('cx');})
.attr("y1", function(d) { return d3.selectAll('circle').filter(function (k) { return d.source === k.node; }).attr('cy'); })
.attr("x2", function(d) { return d3.selectAll('circle').filter(function (k) { return d.target === k.node; }).attr('cx'); })
.attr("y2", function(d) { return d3.selectAll('circle').filter(function (k) { return d.target === k.node; }).attr('cy'); });
chart.dispatchStartDrawing()
}
})
I want to display 4 circles (A, B, C, D) in separate groups:
Group 1 - (A,D) | Group 2 - (B,C).
The grouping should be automatic by using the attribute "group".
The simulation is done by creating a d3 force layout object

Display text/label on Nodes, D3.js force directed graph

I don't understand how to display text on my nodes
I tried several times but didn't work, please help me to solve it
I'm not familiar with D3.js at all but I need to visualize a graph for my project, and I need this additional feature (display label) on the nodes
Here is the input file:
{
"nodes":[
{"name":"A","group":0},
{"name":"B","group":1},
{"name":"C","group":2},
{"name":"D","group":3},
{"name":"E","group":4}
],
"links":[
{"source":0,"target":1,"value":11},
{"source":0,"target":2,"value":11},
{"source":1,"target":2,"value":21},
{"source":1,"target":3,"value":21},
{"source":1,"target":4,"value":21},
{"source":2,"target":3,"value":21},
{"source":2,"target":4,"value":21},
{"source":3,"target":4,"value":11}
]
}
D3.js Code:
<script type="text/javascript">
// use print_graph() method to output graph vertices
main_graph();
var width = 700,
height = 300;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(150)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(0,0)");
d3.json("input.json", function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) {
return Math.sqrt(d.value);
});
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
// size of nodes
.attr("r", 20)
.style("fill", function(d) {
return color(d.group);
})
.call(force.drag);
//I'm stuck here
var texts = svg.selectAll("text.label")
.data(graph.nodes)
.enter().append("text")
.attr("class", "label")
.attr("fill", "white")
.text(function(d) {
return d.name;
});
/*
// 1st trying
var text = svg.selectAll("text")
.data(graph.node)
.enter().append("text")
.attr("fill","white")
.text(function (d) {return d.name; });
*/
// another trying
//node.append("text")
//.text(function(d) { return d.name; });
node.append("title")
.text(function(d) {
return d.name;
});
force.on("tick", function() {
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;
});
node.attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
});
});
});
</script>
How you are adding the text is fine but in your force.on("tick", function() {}) handler you don't position the them:
force.on("tick", function() {
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;
});
node.attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
});
texts.attr("x", function(d) { //<-- NEED TO POSITION THESE TOO
return d.x;
})
.attr("y", function(d) {
return d.y;
});
});
Here's your code fixed.

making d3 force layout work with labeled data

I've been trying to glue ideas from various d3 examples into what i need, starting with the basic example using miserable.json data and then adding:
use of a key function for the data joins
modifying the underlying graph ala this example
making the links' linkDistance() function depend on an attribute of the graph's links'
adding labels to the nodes ala this example
So far i'm only 3 out of 4: something about using the g elements -- using code taken directly from the Mike's "Labeled Force Layout" example -- breaks things and the nodes aren't drawn. I can make it work if i join circle elements directly, but not if I interpose g elements with attached circles and text elements.
The code below is my best effort at a minimal example. This example works, but does not if I replace the .enter().append("circle") line with the .enter().append("g") lines.
Does anyone know why?
var Width = 200;
var Height = 200;
var Pix2Len = 10;
var color = d3.scale.category10();
var svg = d3.select("body").append("svg")
.attr("width", Width)
.attr("height", Height);
var force = d3.layout.force()
.size([Width, Height])
var graph = {
"nodes":[
{"name":"Myriel","idx":0},
{"name":"Napoleon","idx":1},
{"name":"Mlle.Baptistine","idx":2},
{"name":"Mme.Magloire","idx":3}
],
"links":[
{"source":1,"target":0,"len":1,"idx":"1-0"},
{"source":2,"target":1,"len":4,"idx":"2-1"},
{"source":2,"target":0,"len":8,"idx":"2-0"},
{"source":3,"target":0,"len":10,"idx":"3-0"},
{"source":3,"target":1,"len":4,"idx":"3-1"},
{"source":3,"target":2,"len":6,"idx":"3-2"}
]
}
console.log("data loaded. nnode="+graph.nodes.length+" nlinks="+graph.links.length);
force
.nodes(graph.nodes)
.links(graph.links)
.size([Width, Height])
.linkDistance(function(link) {
// console.log("link: "+link.source.name+' '+link.target.name+' '+link.idx+' '+link.len)
return link.len * Pix2Len})
.on("tick", tick);
var link = svg.selectAll(".link")
.data(graph.links, function(d) {return d.idx; })
.enter().append("line")
.attr("class", "link");
var node = svg.selectAll(".node")
.data(graph.nodes, function(d) {return d.idx; })
// THIS WORKS
.enter().append("circle").attr("r", 8).style("fill", function(d) { return color(0); });
// BUT THIS DOES NOT
// modeled after http://bl.ocks.org/mbostock/950642
//
//.enter().append("g")
//.attr("class", "node")
//.attr("cx", function(d) { return d.x; })
//.attr("cy", function(d) { return d.y; });
//
//node.append("circle")
//.attr("r", 10)
//.style("fill", function(d) { return color(0); });
//
//node.append("text")
//.attr("dx", 12)
//.attr("dy", ".35em")
//.text(function(d) { return d.name });
// 1. Begin with graph from JSON data
setTimeout(function() {
start();
}, 0);
// 2. Change graph topology
setTimeout(function() {
var new4 = {"name":"CountessdeLo","idx":4}
var new5 = {"name":"Geborand","idx":5}
graph.nodes.push(new4,new5);
var link40 = {"source":4,"target":0,"len":1,"idx":"4-0"};
var link43 = {"source":4,"target":3,"len":4,"idx":"4-3"};
var link50 = {"source":5,"target":0,"len":1,"idx":"5-0"};
var link52 = {"source":5,"target":2,"len":4,"idx":"5-2"};
graph.links.push(link40,link43,link50,link52);
start();
}, 3000);
//3. Change some link lengths
setTimeout(function() {
// force.links().forEach(function(link) {
graph.links.forEach(function(link) {
if (link.idx == '1-0')
{link.len=10; }
else if (link.idx == '3-0')
{link.len=2; }
else if (link.idx == '5-0')
{link.len=10; };
}); // eo-forEach
start();
}, 6000);
function start() {
link = link.data(force.links(), function(d) { return d.idx; });
link.enter().insert("line", ".node").attr("class", "link");
link.exit().remove();
node = node.data(force.nodes(), function(d) { return d.idx;});
node.enter().append("circle").attr("class", function(d) {
// tried with the <g> version above
// node.enter().append("g").attr("class", function(d) {
console.log('start:'+' '+d.name);
return d.idx; }).attr("r", 5).style("fill", function(d) { return color(1); });
node.exit().remove();
force.start();
}
function tick() {
node.attr("cx", function(d) {
// console.log('tick:'+' '+d.name);
return d.x; })
.attr("cy", function(d) { return d.y; })
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; });
}
//} // eo-ready()
In your code you are setting cx and cy attributes to the g element. g elements does not support any position attributes like x, y or cx, cy. To move the contents of a g element you will have to use the transform attribute.
Your code
var node = svg.selectAll(".node")
.data(graph.nodes, function(d) {return d.idx; })
.enter().append("g")
.attr("class", "node")
.attr("cx", function(d) { return d.x; }) //will not work
.attr("cy", function(d) { return d.y; }); //will not work
Solution
var node = svg.selectAll(".node")
.data(graph.nodes, function(d) {return d.idx; })
.enter().append("g")
.attr("class", "node");
node.append("circle")
.attr("r", 10)
.style("fill", function(d) { return color(0); });
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });
Use translate function as below to move group elements.
function tick() {
//Moving <g> elements using transform attribute
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
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; });
}
JSFiddle

Need help modifying d3.js Collapsible Force Layout

I am modifying the d3.js Collapsible Force Layout where the nodes are given as circles.
http://bl.ocks.org/mbostock/1062288
I changed it to a group g and attached the circle into the g. The problem is that in the tick function when I change from
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
to
node.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
the nodes are all displaced.
I have changed the reference to node so node refers to the group g in the second case.
I have created force layouts and have done this a lot of times but never faced this problem.
Is it because of the d3.tree.layout? I don't know. Please help.
I have tried a few more times with other svg elements like text and rect and I found out that the problem occurs only when there is circle involved and I need to give attributes like cx and cy to text and rect even though these elements don't have these attributes, to make them work.
So please help someone. Totally confused.
Code::
var link = svg.selectAll(".link"),
node = svg.selectAll(".node");
link = link.data(force.links());
// Exit any old links.
link.exit().remove();
// Enter any new links.
link.enter().insert("line",".node")
.attr("class", "link")
.attr("x1", function(d,i) { 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; });
// Update the nodes…
node = node.data(force.nodes(), function(d) { return d.id; }).style("fill", color);
// Exit any old nodes.
node.exit().remove();
// Enter any new nodes.
node.enter().append("g")
.attr("class", "node")
.attr("title",function(d) { return d.name || d.layout; })
.on("click", click)
.call(force.drag);
node.append("circle")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", function(d) { return 4.5; })
.style("fill", color);
node.append("text")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; })
.attr("fill","red")
.attr("stroke","red")
.style("font-size","9px")
.on("click",click)
.text(function(d){return d.name})
.call(force.drag);
function tick() {
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; });
// node.attr("cx", function(d) { return d.x; })
// .attr("cy", function(d) { return d.y; });
node.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
Here's the code that generates the faulty graph.
The problem is that you're setting the coordinates twice; on the actual elements and on the g containing them:
node.append("circle")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", function(d) { return 4.5; })
.style("fill", color);
(similarly for the text elements) and
node.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
This causes the offset. To fix, just set one of them (the translation on the g elements):
node.append("circle")
.attr("r", function(d) { return 4.5; })
.style("fill", color);
node.append("text")
.attr("fill","red")
.attr("stroke","red")
// etc

Categories