Skip to content

Instantly share code, notes, and snippets.

@ggb
Last active August 29, 2015 14:22
Show Gist options
  • Save ggb/871edb94134fdead2a2d to your computer and use it in GitHub Desktop.
Save ggb/871edb94134fdead2a2d to your computer and use it in GitHub Desktop.
Visualize cooccurrance and centrality with d3.js (inspired by M. Bostocks http://bl.ocks.org/mbostock/4062045). The example shows BetweennessCentrality on the first two paragraphs of Wikipedias "Computer Science"-article. The nodes are CCS (Computer Classification System) entities. Demo (with more data): http://examples.ideen-und-soehne.de/coocc…
{
"links": [
{
"source": 0,
"target": 10,
"value": 1
},
{
"source": 0,
"target": 12,
"value": 1
},
{
"source": 1,
"target": 9,
"value": 1
},
{
"source": 1,
"target": 10,
"value": 1
},
{
"source": 1,
"target": 3,
"value": 1
},
{
"source": 1,
"target": 11,
"value": 1
},
{
"source": 2,
"target": 3,
"value": 1
},
{
"source": 2,
"target": 13,
"value": 1
},
{
"source": 3,
"target": 1,
"value": 1
},
{
"source": 3,
"target": 2,
"value": 1
},
{
"source": 4,
"target": 11,
"value": 1
},
{
"source": 4,
"target": 9,
"value": 1
},
{
"source": 5,
"target": 10,
"value": 1
},
{
"source": 6,
"target": 10,
"value": 1
},
{
"source": 6,
"target": 8,
"value": 1
},
{
"source": 7,
"target": 12,
"value": 1
},
{
"source": 7,
"target": 10,
"value": 1
},
{
"source": 8,
"target": 12,
"value": 1
},
{
"source": 8,
"target": 6,
"value": 1
},
{
"source": 9,
"target": 1,
"value": 1
},
{
"source": 9,
"target": 4,
"value": 1
},
{
"source": 10,
"target": 6,
"value": 1
},
{
"source": 10,
"target": 13,
"value": 1
},
{
"source": 10,
"target": 11,
"value": 1
},
{
"source": 10,
"target": 5,
"value": 1
},
{
"source": 10,
"target": 7,
"value": 1
},
{
"source": 10,
"target": 0,
"value": 1
},
{
"source": 10,
"target": 1,
"value": 1
},
{
"source": 11,
"target": 10,
"value": 1
},
{
"source": 11,
"target": 1,
"value": 1
},
{
"source": 11,
"target": 4,
"value": 1
},
{
"source": 12,
"target": 7,
"value": 1
},
{
"source": 12,
"target": 0,
"value": 1
},
{
"source": 12,
"target": 8,
"value": 1
},
{
"source": 13,
"target": 10,
"value": 1
},
{
"source": 13,
"target": 2,
"value": 1
}
],
"nodes": [
{
"descriptor": "10003569",
"name": "automating",
"value": 0.14790566291819257
},
{
"descriptor": "10003496",
"name": "systems",
"value": 0.3521563402814109
},
{
"descriptor": "10003752",
"name": "theory of computation",
"value": 0.09860377527879506
},
{
"descriptor": "10011673",
"name": "design",
"value": 0.10564690208442326
},
{
"descriptor": "10010371",
"name": "computer graphics",
"value": 0.007043126805628218
},
{
"descriptor": "10010854",
"name": "usable",
"value": 0
},
{
"descriptor": "10010607",
"name": "memory",
"value": 0.26059569180824405
},
{
"descriptor": "10011035",
"name": "procedures",
"value": 0
},
{
"descriptor": "10011458",
"name": "cell",
"value": 0.2042506773632183
},
{
"descriptor": "10003783",
"name": "computational complexity theory",
"value": 0.07747439486191039
},
{
"descriptor": "10003458",
"name": "computer",
"value": 0.7747439486191039
},
{
"descriptor": "10011006",
"name": "programming language",
"value": 0.21129380416884652
},
{
"descriptor": "10003521",
"name": "computer science",
"value": 0.22538005778010298
},
{
"descriptor": "10011242",
"name": "spec",
"value": 0.14790566291819257
}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// s. http://bl.ocks.org/mbostock/4062045
var width = 1600,
height = 1000;
var force = d3.layout.force()
.charge(-700)
.linkDistance(150)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("data.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 1; });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("text")
.text(function(d) { return d.name; })
.attr("text-anchor", "middle")
.style("fill", "grey")
.style("font-size", function(d) { return ( 10 + 20 * d.value ) + "px"; })
.style("font-family", "Arial")
.call(force.drag);
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; });
node.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; });
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment