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)
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.scaleLog()
.rangeRound([0, width]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.csv("flare.csv", type, function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.value; }));
offset = 100;
var simulation = d3.forceSimulation(data)
.force("x", d3.forceX(function(d) { return width - x(d.value); }).strength(1))
.force("y", d3.forceY(function(d) { return (height / 2) -80; }))
.force("collide", d3.forceCollide(4))
// .alpha(0.2)
.stop();
// d3.transition()
// .duration(1000)
// .tween((a))
// simulation.transition()
// .duration(2000)
// .alpha(1);
// var simulation = d3.forceSimulation(data)
// .force("x", d3.forceX(function(d) { return x(d.value); }).strength(1))
// .force("y", d3.forceY(height / 2))
// .force("collide", d3.forceCollide(4))
// .stop();
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"));
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.selectAll("image")
.transition()
.duration(750)
.delay(500)
.attr("x", function(d) { return width - d.data.x; })
.attr("y", function(d) { return d.data.y+80; });
cell.append("title")
.text(function(d) { return d.data.id + "\n" + formatValue(d.data.value); });
});
function type(d) {
if (!d.value) return;
d.value = +d.value;
return d;
}
</script>
https://d3js.org/d3.v4.min.js