A static beeswarm plot implemented using d3-force’s collision constraint. A Voronoi overlay improves hover interaction.
forked from mbostock's block: Beeswarm
forked from BBischof's block: Beeswarm (Broken)
forked from BBischof's block: Beeswarm (Less Broken)
xxxxxxxxxx
<meta charset="utf-8">
<style>
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
font: 10px sans-serif;
}
.cells path {
fill: none;
pointer-events: all;
}
.cells :hover circle {
fill: red;
}
</style>
<body>
<!-- <svg width="960" height="200"></svg> -->
<script src="https://d3js.org/d3.v4.min.js"></script>
<!-- <script src="d3moji.js"></script> -->
<script>
var margin = {top: 40, right: 40, bottom: 40, left: 40},
width = 960 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
// var svg = d3.select("svg"),
// margin = {top: 40, right: 40, bottom: 40, left: 40},
// width = svg.attr("width") - margin.left - margin.right,
// height = svg.attr("height") - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
// console.log(svg.attr("height"))
var formatValue = d3.format(",d");
var x = d3.scaleTime()
.rangeRound([0, width-50]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var parseDate = d3.timeParse("%m/%d/%y");
console.log(parseDate('7/22/16'))
d3.csv("bees.csv", function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) {console.log(String(d.date_spotted)); return parseDate(d.date_spotted) }));
var simulation = d3.forceSimulation(data)
.force("x", d3.forceX(function(d) { return x(parseDate(d.date_spotted)); }).strength(1))
.force("y", d3.forceY(height / 2))
.force("collide", d3.forceCollide(4))
.stop();
// console.log("here")
for (var i = 0; i < 120; ++i) simulation.tick();
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(20, ".0s"));
// console.log(height)
var cell = g.append("g")
.attr("class", "cells")
.selectAll("g").data(d3.voronoi()
.extent([[-margin.left, -margin.top], [width + margin.right, height + margin.top]])
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.polygons(data)).enter().append("g");
cell.append('image')
.attr('xlink:href',
'https://twemoji.maxcdn.com/svg/1f41d.svg')
.attr('height', '12')
.attr('width', '12')
.attr("x", function(d) { return d.data.x; })
.attr("y", function(d) { return d.data.y; });
cell.append("title")
.text(function(d) { return d.data.id + "\n" + formatValue(d.data.value); });
});
function type(d) {
if (!d.date_spotted) return;
d.date_spotted = +d.date_spotted;
return d;
}
</script>
https://d3js.org/d3.v4.min.js