D3NodeThreshold

Basic force directed layout

by ramnathv

HTML

<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script src="//bost.ocks.org/mike/fisheye/fisheye.js?0.0.3"></script>
<script type="application/json" id="mis">
    {
        "nodes": [{
            "name": "Myriel",
                "group": 1
        }, {
            "name": "Napoleon",
                "group": 1
        }, {
            "name": "Mlle.Baptistine",
                "group": 1
        }, {
            "name": "Mme.Magloire",
                "group": 1
        }, {
            "name": "CountessdeLo",
                "group": 1
        }, {
            "name": "Geborand",
                "group": 1
        }, {
            "name": "Champtercier",
                "group": 1
        }, {
            "name": "Cravatte",
                "group": 1
        }, {
            "name": "Count",
                "group": 1
        }, {
            "name": "OldMan",
                "group": 1
        }, {
            "name": "Labarre",
                "group": 2
        }, {
            "name": "Valjean",
                "group": 2
        }, {
            "name": "Marguerite",
                "group": 3
        }, {
            "name": "Mme.deR",
                "group": 2
        }, {
            "name": "Isabeau",
                "group": 2
        }, {
            "name": "Gervais",
                "group": 2
        }, {
            "name": "Tholomyes",
                "group": 3
        }, {
            "name": "Listolier",
                "group": 3
        }, {
            "name": "Fameuil",
                "group": 3
        }, {
            "name": "Blacheville",
                "group": 3
        }, {
            "name": "Favourite",
                "group": 3
        }, {
            "name": "Dahlia",
                "group": 3
        }, {
            "name": "Zephine",
                "group": 3
        }, {
            "name": "Fantine",
                "group": 3
        }, {
            "name":...

CSS

.node {
    stroke: #fff;
    stroke-width: 1.5px;
}
.link {
    stroke: #999;
    stroke-opacity: .6;
}

h3 {
    color: #1ABC9C;
    text-align:center;  
    font-style: italic;
    font-size: 14px;
    font-family: "Helvetica";
}

JavaScript

//Constants for the SVG
var width = 500,
    height = 500;

//Set up the colour scale
var color = d3.scale.category20();

//Set up the force layout
var force = d3.layout.force()
    .charge(-120)
    .linkDistance(30)
    .size([width, height]);

//Append a SVG to the body of the html page. Assign this SVG as an object to svg
var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
//.on("dblclick", threshold);

//Read the data from the mis element 
var mis = document.getElementById('mis').innerHTML;
graph = JSON.parse(mis);
graphRec=JSON.parse(JSON.stringify(graph)); //Add this line

//Creates the graph data structure out of the json data
force.nodes(graph.nodes)
    .links(graph.links)
    .start();

//Create all the line svgs but without locations yet
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", 8)
    .style("fill", function (d) {
    return color(d.group);
})
    .call(force.drag);

//Toggle stores whether the highlighting is on
var toggle = 0;
//Create an array logging what is connected to what
var linkedByIndex = {};
for (i = 0; i < graph.nodes.length; i++) {
    linkedByIndex[i + "," + i] = 1;
};
graph.links.forEach(function (d) {
    linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
//This function looks up whether a pair are neighbours
function neighboring(a, b) {
    return linkedByIndex[a.index + "," + b.index];
}
function connectedNodes() {
    if (toggle == 0) {
        //Reduce the opacity of all but the neighbouring nodes
        d = d3.select(this).node().__data__;
        node.style("opacity", function (o) {
            return neighboring(d, o) | neighboring(o, d) ? 1 : 0.1;
 ...