More experimenting with combining force-directed graphs and voronoi diagrams.
Also see:
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
margin:0;
position:fixed;
top:0;
right:0;
bottom:0;
left:0;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
</style>
<svg></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = innerWidth,
height = innerHeight;
svg.attr("width", width)
.attr("height", height)
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json("miserables.json", function(error, graph) {
if (error) throw error;
var voronoi = d3.voronoi().extent([[0,0], [width, height]]);
var polygons = svg.append("g")
.attr("class", "polygons")
.selectAll("polygon")
.data(graph.nodes)
.enter().append("polygon")
.style("fill", function(d) { return color(d.group); })
.style("fill-opacity", .2)
.style("stroke", "black")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
if (graph.nodes[0].x) {
var polygonShapes = voronoi(graph.nodes.map(function(d) {return [d.x, d.y]})).polygons()
polygons.attr("points", function(d, i) {
return polygonShapes[i]
})
}
}
});
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>
https://d3js.org/d3.v4.min.js