Skip to content

Instantly share code, notes, and snippets.

@fabiovalse
Last active May 17, 2016 09:12
Show Gist options
  • Save fabiovalse/c2e578ce545305ebb95a to your computer and use it in GitHub Desktop.
Save fabiovalse/c2e578ce545305ebb95a to your computer and use it in GitHub Desktop.
DBpedia Classes Node Link Diagram

This experiment displays the graph of the classes of DBpedia. Each node is a class (e.g., Person, Organisation, Event, Place, Species) while each link represents the amount of triples between the entities of the classes it connects.

Given a node, for instance the class Person, it is important to specify that it represents all the entities having as class (rdf:type) Person without any of its subclasses. That is, each instance is represented only with the most specific class it is classified in the DBpedia hierarchy (ontology).

svg {
background: #FFF;
}
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #000;
stroke-opacity: .1;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DBpedia Classes Node Link Diagram</title>
<link type="text/css" href="index.css" rel="stylesheet"/>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<script src="index.js"></script>
</body>
</html>
//noprotect
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.linkDistance(1000)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
var zoomable_layer = svg.append('g');
zoom = d3.behavior.zoom()
.scaleExtent([0.5,4])
.on('zoom', function() {
zoomable_layer
.attr('transform', "translate(" + zoom.translate() + ")scale(" + zoom.scale() + ")");
});
svg.call(zoom);
d3.json("node_link_ontology.json", function(error, graph) {
var thickness = d3.scale.sqrt()
.domain([1, d3.max(graph.links, function(d) {return d.value;})])
.range([0.1, 10]);
nodeLabelID = [];
graph.nodes.forEach(function(d) {
nodeLabelID[d.id] = d.name;
});
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = zoomable_layer.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return thickness(d.value); });
link.append("title")
.text(function(d) {
return nodeLabelID[d.source.id] + " -> " + nodeLabelID[d.target.id];
});
var node = zoomable_layer.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 2.5)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
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("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment