A reminder that force diagrams need not be limited to circles. Layouts don't dictate design.
Last active
August 29, 2015 14:03
-
-
Save danharr/79c21cb3557cf2a4656d to your computer and use it in GitHub Desktop.
Square Force
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.node { | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
.link { | |
stroke: black; | |
stroke-opacity: 0.5; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 800; | |
var color = d3.scale.category20(); | |
var force = d3.layout.force() | |
.charge(-4000) | |
.linkDistance(350) | |
.size([width, height]); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
d3.json("raw.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 10; }); | |
var circle = svg.append("g").selectAll(".node") | |
.data(graph.nodes) | |
.enter().append("rect") | |
.attr("class", "node") | |
.attr("width", function(d) {return d.size*75;}) | |
.attr("height", function(d) {return d.size*55;}) | |
.style("stroke","green") | |
.style("stroke-width", function(d) {return d.size*5;}) | |
.style("fill", function(d) { return "black"; }) | |
.call(force.drag); | |
var text = svg.append("g").selectAll(".text") | |
.data(graph.nodes) | |
.enter() | |
.append("text") | |
.attr("x",0) | |
.attr("y",".31em") | |
.text(function(d) {return d.name;}) | |
.style("fill","white") | |
.style("font-family","helvetica") | |
.style("font-size",function(d) {return d.size*15;}) | |
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; }); | |
circle.attr("x", function(d) { return d.x-(d.size*10); }) | |
.attr("y", function(d) { return d.y-(d.size*10); }); | |
text.attr("x", function(d) { return d.x; }) | |
.attr("y", function(d) { return d.y+(d.size*10); }); | |
}); | |
}); | |
</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
{ | |
"links":[ | |
{ | |
"source":0, | |
"target":1 | |
}, | |
{ | |
"source":0, | |
"target":2 | |
}, | |
{ | |
"source":0, | |
"target":3 | |
}, | |
{ | |
"source":0, | |
"target":4 | |
} | |
], | |
"nodes":[ | |
{ | |
"name":"Germany", | |
"size":4 | |
}, | |
{ | |
"name":"Brazil", | |
"size":1 | |
}, | |
{ | |
"name":"France", | |
"size":1.5 | |
} | |
, | |
{ | |
"name":"Spain", | |
"size":2 | |
} | |
, | |
{ | |
"name":"Italy", | |
"size":2.5 | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment