D3HClust

Converting hierarchi

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":"Oklahoma","group":1},{"name":"Oregon","group":1},{"name":"Pennsylvania","group":1},{"name":"Rhode Island","group":1},{"name":"South Carolina","group":1},{"name":"node  41","group":2},{"name":"node  42","group":2},{"name":"node  43","group":2},{"name":"node  44","group":2},{"name":"node  45","group":2},{"name":"node  46","group":2},{"name":"node  47","group":2},{"name":"node  48","group":2},{"name":"node  49","group":2},{"name":"node  50","group":2},{"name":"node  51","group":2},{"name":"node  52","group":2},{"name":"node  53","group":2},{"name":"node  54","group":2},{"name":"node  55","group":2},{"name":"node  56","group":2},{"name":"node  57","group":2},{"name":"node  58","group":2},{"name":"node  59","group":2},{"name":"node  60","group":2},{"name":"node ...

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(10)
    .gravity(0.1)
    .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 cl2 = document.getElementById('cl2').innerHTML;
graphClus2 = JSON.parse(cl2);

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;
    })
        .attr("y1", function (d) {
        return d.source.y;
    })
        .attr("x2", function (d) {
        return d.target.x;
   ...