This is a D3 force diagram template that brings in separate csv files of the links and nodes. Here the nodes are foods that can be in either "group1" (healthy) or "group2" (unhealthy"). The links are things that taste good together. Link "type1" is anything involving peanut butter. Link "type2" is any other kind of link. Separating out the links and nodes like this might make it easier to organize your information.
Last active
December 15, 2015 01:49
-
-
Save ccmcc/5182685 to your computer and use it in GitHub Desktop.
D3 Force Diagram Template with CSV files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NODE KEY | |
// Green is group1 | |
// Red is group2 | |
// Blue is group3 | |
// Black is group4 | |
<br> | |
// LINK KEY | |
// Black is type1 | |
// Yellow is type2 | |
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.link { | |
stroke: #999; | |
stroke-opacity: .6; | |
} | |
.node { | |
stroke: #fff; | |
stroke-width: .5px; | |
} | |
.node text { | |
pointer-events: none; | |
font: 15px sans-serif; | |
font-weight: bolder; | |
} | |
.node.group1{ | |
fill: green; | |
stroke-opacity: .3; | |
stroke-width: 4; | |
} | |
.node.group2{ | |
fill: red; | |
stroke-opacity: .3; | |
stroke-width: 4; | |
} | |
.node.group3{ | |
fill: blue; | |
stroke-opacity: .3; | |
stroke-width: 4; | |
} | |
.node.group4{ | |
fill: black; | |
stroke-opacity: .3; | |
stroke-width: 4; | |
} | |
.link.type1{ | |
stroke: black; | |
stroke-opacity: .3; | |
stroke-width: 4; | |
} | |
.link.type2{ | |
stroke: red; | |
stroke-opacity: .3; | |
stroke-width: 4; | |
} | |
</style> | |
</style> | |
<body> | |
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var linkpath = ("links.csv"); | |
var nodepath = ("nodes.csv"); | |
var width = 1000, | |
height = 600; | |
var color = d3.scale.category20(); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
//Want to have different labels | |
// SETTING UP THE FORCE LAYOUT | |
var force = d3.layout.force() | |
//using width/height from above, but size is mainly det'd by linkDistance and charge | |
.size([width, height]) | |
// how far between nodes | |
.linkDistance(80) | |
// changes how close nodes will get to each other. Neg is farther apart. | |
.charge(-300); | |
d3.csv(nodepath, function(nodes) { | |
var nodelookup = {}; | |
var nodecollector = {}; | |
count = 0; | |
// we want to create a lookup table that will relate the links file and the nodes file | |
nodes.forEach(function(row) { | |
nodelookup[row.node] = count; | |
nodecollector[row.node] = {name: row.node, group: row.group}; | |
//console.log(nodecollector) | |
//console.log(row.node) | |
//console.log(nodelookup) | |
count++; | |
}); | |
//Get all the links out of of the csv in a way that will match up with the nodes | |
d3.csv(linkpath, function(linkchecker) { | |
var linkcollector = {}; | |
indexsource = 0; | |
indextarget = 0; | |
count= 0; | |
//console.log(nodelookup['celery']) | |
linkchecker.forEach(function(link) { | |
linkcollector[count] = {source: nodelookup[link.source], target: nodelookup[link.target], type: link.type }; | |
//console.log(linkcollector[count]) | |
count++ | |
}); | |
//console.log(linkcollector) | |
var nodes = d3.values(nodecollector); | |
var links = d3.values(linkcollector); | |
//console.log(nodes) | |
//console.log(links) | |
// Create the link lines. | |
var link = svg.selectAll(".link") | |
.data(links) | |
.enter().append("line") | |
.attr("class", function(d) { return "link " + d.type; }) | |
// Create the node circles. | |
var node = svg.selectAll(".node") | |
.data(nodes) | |
.enter().append("g") | |
.attr("class", "node") | |
.call(force.drag); | |
//put in little circles to drag | |
node.append("circle") | |
.attr("r", 4.5) | |
.attr("class", function(d) { return "node " + d.group; }) | |
.call(force.drag); | |
//add the words | |
node.append("text") | |
.attr("dx", 12) | |
.attr("dy", ".35em") | |
.text(function(d) { return d.name }); | |
//get it going! | |
force | |
.nodes(nodes) | |
.links(links) | |
.start(); | |
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; }); | |
//I think that translate changes all of the x and ys at once instead of one by one? | |
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); | |
}) | |
}); | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source | target | type | |
---|---|---|---|
banana | peanut butter | type1 | |
celery | peanut butter | type1 | |
celery | ranch dressing | type2 | |
banana | honey | type2 | |
hummus | celery | type2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node | group | |
---|---|---|
celery | group1 | |
peanut butter | group2 | |
hummus | group1 | |
banana | group1 | |
honey | group2 | |
ranch dressing | group2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment