Instantly share code, notes, and snippets.

@ccmcc ccmcc/index.html
Last active Dec 15, 2015

What would you like to do?
D3 Force Diagram Template with CSV files

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.

// Green is group1
// Red is group2
// Blue is group3
// Black is group4
// Black is type1
// Yellow is type2
<!DOCTYPE html>
<meta charset="utf-8">
.link {
stroke: #999;
stroke-opacity: .6;
.node {
stroke: #fff;
stroke-width: .5px;
.node text {
pointer-events: none;
font: 15px sans-serif;
font-weight: bolder;
fill: green;
stroke-opacity: .3;
stroke-width: 4;
fill: red;
stroke-opacity: .3;
stroke-width: 4;
fill: blue;
stroke-opacity: .3;
stroke-width: 4;
fill: black;
stroke-opacity: .3;
stroke-width: 4;
stroke: black;
stroke-opacity: .3;
stroke-width: 4;
stroke: red;
stroke-opacity: .3;
stroke-width: 4;
<script type="text/javascript" src=""></script>
var linkpath = ("links.csv");
var nodepath = ("nodes.csv");
var width = 1000,
height = 600;
var color = d3.scale.category20();
var svg ="body").append("svg")
.attr("width", width)
.attr("height", height);
//Want to have different labels
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
// changes how close nodes will get to each other. Neg is farther apart.
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:};
//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;
linkchecker.forEach(function(link) {
linkcollector[count] = {source: nodelookup[link.source], target: nodelookup[], type: link.type };
var nodes = d3.values(nodecollector);
var links = d3.values(linkcollector);
// Create the link lines.
var link = svg.selectAll(".link")
.attr("class", function(d) { return "link " + d.type; })
// Create the node circles.
var node = svg.selectAll(".node")
.attr("class", "node")
//put in little circles to drag
.attr("r", 4.5)
.attr("class", function(d) { return "node " +; })
//add the words
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return });
//get it going!
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; })
.attr("y2", function(d) { return; });
//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 + ")"; });
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