{var links = [
{source: "Person", target: "Country", type: "normal", label:"isFrom"},
{source: "Person", target: "Player", type: "normal", label:"wasPlayer"},
{source: "Player", target: "Person", type: "normal", label:"represents"},
{source: "Player", target: "Appearance", type: "normal", label:"appearsIn"},
{source: "Player", target: "Club", type: "normal", label:"playedFor"},
{source: "Appearance", target: "Game", type: "normal", label:"refersTo"},
{source: "Game", target: "Club", type: "normal", label:"hasHomeClub"},
{source: "Game", target: "Club", type: "normal", label:"hasAwayClub"},
{source: "Club", target: "Arena", type: "normal", label:"hasStadium"}];
links.sort(function(a,b) {
if (a.source > b.source) {return 1;}
else if (a.source < b.source) {return -1;}
else {
if (a.target > b.target) {return 1;}
if (a.target < b.target) {return -1;}
else {return 0;}
}
});
for (var i=0; i<links.length; i++) {
if (i != 0 &&
links[i].source == links[i-1].source &&
links[i].target == links[i-1].target) {
links[i].linknum = links[i-1].linknum + 1;
}
else {links[i].linknum = 1;};
};
for (var i=0; i<links.length; i++) {
links[i].id=i;
}
var nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});
var w = window.screen.width,
h = 600;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([w, h])
.linkDistance(200)
.charge(-2000)
.on("tick", tick)
.start();
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h);
// Per-type markers, as they don't inherit styles.
svg.append("svg:defs").selectAll("marker")
.data(["normal"])
.enter().append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -22 40 20")
.attr("refX", 25)
.attr("refY", -2)
.attr("markerWidth", 50)
.attr("markerHeight", 500)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
var path = svg.append("svg:g").selectAll("path")
.data(force.links())
.enter().append("svg:path").attr("id", function(d){return d.id;})
.attr("class", function(d) {
svg.append("svg:text")
.append("textPath") //append a textPath to the text element
.attr("xlink:href", "#"+d.id) //place the ID of the path here
.style("text-anchor","middle") //place the text halfway on the arc
.attr("startOffset", "50%")
.attr("x", 8)
.attr("y", ".31em")
.attr("class", "shadow")
.text(d.label);
svg.append("svg:text")
.append("text") //append a textPath to the text element
.attr("xlink:href", "#"+d.id) //place the ID of the path here
.style("text-anchor","middle") //place the text halfway on the arc
.attr("startOffset", "50%")
.attr("x", 8)
.attr("y", ".31em")
.text(d.label);
return "link " + d.type; })
.attr("marker-end", function(d) { return "url(#" + d.type + ")"; })
var circle = svg.append("svg:g").selectAll("circle")
.data(force.nodes())
.enter().append("svg:circle").style("fill", function (d) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
return '#'+randomColor; })
.attr("r", 30)
.call(force.drag);
var text = svg.append("svg:g").selectAll("g")
.data(force.nodes())
.enter().append("svg:g");
// A copy of the text with a thick white stroke for legibility.
text.append("svg:text")
.attr("x", 8)
.attr("y", ".31em")
.attr("class", "shadow")
.style("text-anchor","middle")
.text(function(d) { return d.name; });
text.append("svg:text")
.attr("x", 8)
.attr("y", ".31em")
.style("text-anchor","middle")
.text(function(d) { return d.name; });
// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
path.attr("d", function(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = 100/d.linknum; //linknum is defined above
return "M" + d.source.x + "," + d.source.y + "A" + dr/0.5 + "," + dr/0.5 + " 0 0,1 " + d.target.x + "," + d.target.y;
});
circle.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
text.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
return Object.assign(svg.node());
}