D3HClustStar
Visualises changes in clustering solutions
by Simon Raper
HTML
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script type="application/json" id="cl1">
{ "nodes":[{"name":"Alabama","group":1},{"name":"Alaska","group":1},{"name":"Arizona","group":1},{"name":"Arkansas","group":1},{"name":"California","group":1},{"name":"Colorado","group":1},{"name":"Connecticut","group":1},{"name":"Delaware","group":1},{"name":"Florida","group":1},{"name":"Georgia","group":1},{"name":"Hawaii","group":1},{"name":"Idaho","group":1},{"name":"Illinois","group":1},{"name":"Indiana","group":1},{"name":"Iowa","group":1},{"name":"Kansas","group":1},{"name":"Kentucky","group":1},{"name":"Louisiana","group":1},{"name":"Maine","group":1},{"name":"Maryland","group":1},{"name":"Massachusetts","group":1},{"name":"Michigan","group":1},{"name":"Minnesota","group":1},{"name":"Mississippi","group":1},{"name":"Missouri","group":1},{"name":"Montana","group":1},{"name":"Nebraska","group":1},{"name":"Nevada","group":1},{"name":"New Hampshire","group":1},{"name":"New Jersey","group":1},{"name":"New Mexico","group":1},{"name":"New York","group":1},{"name":"North Carolina","group":1},{"name":"North Dakota","group":1},{"name":"Ohio","group":1},{"name":"node 36","group":2},{"name":"node 37","group":2},{"name":"node 38","group":2},{"name":"node 39","group":2},{"name":"node 40","group":2}], "links":...
CSS
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
.d3-tip {
line-height: 1;
color: #999;
}
.cursor {
fill: none;
stroke: brown;
pointer-events: none;
}
JavaScript
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-100)
.linkDistance(30)
.gravity(0.1)
.friction(0.3)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.on('mousedown', relink);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function (d) {
return "<span style='color: #606060; font: ariel '>" + d.name + "</span>";
});
var cursor = svg.append("circle")
.attr("r", 30)
.attr("transform", "translate(-100,-100)")
.attr("class", "cursor");
var cl1 = document.getElementById('cl1').innerHTML;
graph = JSON.parse(cl1);
var newNodes = document.getElementById('newNodes').innerHTML;
newNodesJ = JSON.parse(newNodes);
var newLinks = document.getElementById('newLinks').innerHTML;
newLinksJ = JSON.parse(newLinks);
force.nodes(graph.nodes) //Creates the graph data structure out of the json data
.links(graph.links)
.start();
//Create all the line svgs but without locations yet <line class="link" style="stroke-width: 1px;"></line>
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function (d) {
return Math.sqrt(d.value);
});
//Do the same with the circles for the nodes - no
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function (d) {
return color(d.group);
})
.call(force.drag)
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
//Turn on the tip tool
svg.call(tip);
//Now we are giving the SVGs co-ordinates - looks like the force layout is generating the co-ordinates which this code is using to up date the attributes of the SVG elements
force.on("tick", function () {
link.attr("x1", function (d) {
return d.source.x;
})
...