|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
.links line { |
|
stroke: #999; |
|
stroke-width: 1px; |
|
stroke-opacity: 0.6; |
|
} |
|
|
|
.nodes circle { |
|
stroke: #fff; |
|
stroke-width: 1px; |
|
} |
|
|
|
.labels text { |
|
pointer-events: none; |
|
} |
|
|
|
width = window.innerWidth || +svg.attr("width"), |
|
height = window.innerHeight || +svg.attr("height"); |
|
</style> |
|
<svg width="960" height="600"></svg> |
|
<script src="https://d3js.org/d3.v4.min.js"></script> |
|
<script> |
|
|
|
var svg = d3.select("svg"), |
|
width = window.innerWidth || +svg.attr("width"), |
|
height = window.innerHeight || +svg.attr("height"); |
|
|
|
// Set width and height |
|
svg.attr("width", width).attr("height", height); |
|
|
|
var color = d3.scaleOrdinal(d3.schemeCategory20); |
|
|
|
// Zoom |
|
svg.call(d3.zoom().on("zoom", function () { |
|
svg.selectAll("g").attr("transform", d3.event.transform); // Austin wrote this |
|
})); |
|
|
|
// Tooltip |
|
var tooltip = d3.select("body") |
|
.append("div") |
|
.attr("class", "d3-tip") |
|
.style("position", "absolute") |
|
.style("z-index", "10") |
|
.style("visibility", "hidden"); |
|
|
|
var simulation = d3.forceSimulation() |
|
.force("link", |
|
d3.forceLink() |
|
.id((d) => d.abbrev) |
|
.distance((d) => d.dist * 10) |
|
) |
|
.force("charge", d3.forceManyBody().strength(-1)) // d.mass? |
|
.force("collide", d3.forceCollide((d) => d.radius)) |
|
.force("center", d3.forceCenter(width / 2, height / 2)); |
|
|
|
d3.json("languages.json", function(error, graph) { |
|
if (error) throw error; |
|
|
|
window.graph = graph; |
|
|
|
// Links |
|
var link = svg.append("g") |
|
.attr("class", "links") |
|
.selectAll("line") |
|
.data(graph.links) |
|
.enter().append("line") |
|
.attr("stroke-width", function(d) { return Math.sqrt(d.dist); }); |
|
|
|
// Nodes |
|
var node = svg.append("g") |
|
.attr("class", "nodes") |
|
.selectAll("circle") |
|
.data(graph.nodes) |
|
.enter().append("circle") |
|
.each(function (d, i, nodes) { |
|
// Set custom properties |
|
d.color = color(d.family); |
|
d.radius = (d.diameter < 0.3 ? 0.3 : d.diameter) * 10; // TODO |
|
}) |
|
.attr("r", (d) => d.radius ) |
|
.attr("fill", (d) => d.color) |
|
// Mouse events |
|
.call(d3.drag() |
|
.on("start", ondragstart) |
|
.on("drag", ondrag) |
|
.on("end", ondragend)) |
|
.on("mouseover", onmouseover) |
|
.on("mouseout", onmouseout) |
|
.on("mousemove", onmousemove); |
|
|
|
node.append("title") |
|
.text(function(d) { return d.abbrev; }); |
|
|
|
// Add label text |
|
var text = svg.append("g") |
|
.attr("class", "labels") |
|
.selectAll("text") |
|
.data(graph.nodes) |
|
.enter() |
|
.append("text"); |
|
|
|
simulation |
|
.nodes(graph.nodes) |
|
.on("tick", ticked); |
|
|
|
simulation.force("link") |
|
.links(graph.links); |
|
|
|
function ticked() { |
|
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; }); |
|
|
|
text |
|
.attr("x", function(d) { return d.x - this.getBBox().width/2; }) |
|
.attr("y", function(d) { return d.y; }) |
|
.text((d) => d.abbrev) |
|
.attr("font-family", "Helvetica") |
|
// Set size according to zoom |
|
.attr("font-size", (90 / svg.select("g").each(function() { |
|
if(this.transform.baseVal[0]) { |
|
return this.transform.baseVal[0].matrix.e; |
|
} else { return 1; } |
|
})[0]) + "px") |
|
.attr("fill", "#000000"); |
|
} |
|
}); |
|
|
|
function ondragstart(d) { |
|
if (!d3.event.active) simulation.alphaTarget(0.3).restart(); |
|
d.fx = d.x; |
|
d.fy = d.y; |
|
} |
|
|
|
function ondrag(d) { |
|
d.fx = d3.event.x; |
|
d.fy = d3.event.y; |
|
moveTooltip(d); |
|
} |
|
|
|
function ondragend(d) { |
|
if (!d3.event.active) simulation.alphaTarget(0); |
|
d.fx = null; |
|
d.fy = null; |
|
} |
|
|
|
function onmouseover(d) { |
|
d3.select(this).transition() |
|
.attr("style", (d) => "stroke: black; stroke-width: " + 1); |
|
showTooltip(d); |
|
} |
|
|
|
function onmouseout(d) { |
|
d3.select(this).transition() |
|
// .attr("style", (d) => "stroke: " + d.color + "; stroke-width: 0"); |
|
.attr("style", (d) => "stroke: black; stroke-width: 0"); |
|
hideTooltip(); |
|
} |
|
|
|
function onmousemove(d) { |
|
moveTooltip(d); |
|
} |
|
|
|
function showTooltip(d) { |
|
var html = "Language: " + d.lang + "<br>"; |
|
html += "Family: " + d.family + "</span><br>"; |
|
html += "Speakers: " + d.speakers + "</span><br>"; |
|
tooltip.style("visibility", "visible").html(html); |
|
} |
|
|
|
function moveTooltip(d) { |
|
// TODO put directly above node |
|
tooltip.style("top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px"); |
|
} |
|
|
|
function hideTooltip() { |
|
tooltip.style("visibility", "hidden"); |
|
} |
|
|
|
</script> |