D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
pstuffa
Full window
Github gist
Barley Network
<!DOCTYPE html> <meta charset="utf-8"> <style> .nodes circle { fill: #ccc; stroke: #000; } .links { stroke: #999; stroke-width: 1.5px; } .text { pointer-events: none; text-anchor: middle; font: 10px sans-serif; } </style> <svg width="960" height="600"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), width = +svg.attr("width"), height = +svg.attr("height"); var simulation = d3.forceSimulation() .force("charge", d3.forceManyBody().strength(-50)) .force("link", d3.forceLink().id(function(d) { return d.id; })) .force("x", d3.forceX(width / 2)) .force("y", d3.forceY(height / 2)); var color = d3.scaleOrdinal(d3.schemeCategory20c); d3.tsv("barley.tsv", function(error, data) { var yieldLookup = {} data.forEach(function(d) { d.site = d.site.replace(" ","").replace(". ", "") d.variety = d.variety.replace(" ","").replace(". ", "") yieldLookup[d.site + '-' + d.variety + "-" + d.year] = +d.yield; }) var sites = d3.set(data.map(function(d) { return d.site; })).values(); var siteVarieties = d3.set(data.map(function(d) { return d.site + '-' + d.variety; })).values(); var siteVarietiesYears = d3.set(data.map(function(d) { return d.site + '-' + d.variety + "-" + d.year; })).values(); var nodes = [], links = []; sites.forEach(function(site){ nodes.push({"id" : site, "shortName": site, "radius": 10, "color": "#fff", "classValue": "site" }) }); siteVarieties.forEach(function(siteVariety) { nodes.push({"id" : siteVariety, "shortName": siteVariety.split("-")[1], "radius": 10, "color" : color(siteVariety.split("-")[1]), "classValue": siteVariety.split("-")[1].replace(".","") }); links.push({ "source": siteVariety.split("-")[0], "target": siteVariety }) }); siteVarietiesYears.forEach(function(svy) { nodes.push({"id" : svy, "shortName": svy.split("-")[2], "radius": Math.sqrt(yieldLookup[svy])/Math.PI*10, "color": color(svy.split("-")[1]), "classValue": svy.split("-")[1].replace(".","") }); links.push({ "source": svy.split("-")[0] + '-' + svy.split("-")[1], "target": svy }) }); var graph = { "nodes": nodes, "links": links } var link = svg.append("g") .attr("class", "links") .selectAll("line") .data(graph.links) .enter().append("line"); var node = svg.append("g") .attr("class", "nodes") .selectAll("circle") .data(graph.nodes) .enter().append("circle") .attr("class", function(d) { return d.classValue; }) .attr("r", function(d) { return d.radius; }) .style("fill", function(d) { return d.color; }) .call(d3.drag() .on("start", dragstarted) .on("drag", dragged) .on("end", dragended)); var text = svg.append("g") .attr("class", "text") .selectAll("text") .data(graph.nodes) .enter().append("text") .text(function(d, i) { return d.shortName; }) 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; }) .attr("y", function(d) { return d.y; }); } }); function dragstarted(d) { if (!d3.event.active) simulation.alphaTarget(0.3).restart(); d.fx = d.x; d.fy = d.y; } function dragged(d) { d.fx = d3.event.x; d.fy = d3.event.y; } function dragended(d) { if (!d3.event.active) simulation.alphaTarget(0); d.fx = null; d.fy = null; } </script> </script>
https://d3js.org/d3.v4.min.js