Last active
June 28, 2018 14:30
-
-
Save jhubley/7994726c6de8b1afcf017165ec6280e2 to your computer and use it in GitHub Desktop.
the simplest beeswarm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
country | amount | |
---|---|---|
Algeria | 22 | |
Argentina | 49 | |
Armenia | 3 | |
Australia | 9 | |
Austria | 1 | |
Bahamas | 5 | |
Bahrain | 22 | |
Belarus | 9 | |
Belgium | 46 | |
Brazil | 79 | |
Canada | 12 | |
China | 26 | |
Colombia | 9 | |
Croatia | 8 | |
Cuba | 14 | |
Czech Republic | 11 | |
Denmark | 125 | |
Dominican Republic | 124 | |
Egypt | 39 | |
Estonia | 141 | |
Ethiopia | 38 | |
France | 4 | |
Germany | 15 | |
Greece | 16 | |
Grenada | 241 | |
Hungary | 135 | |
India | 22 | |
Indonesia | 31 | |
Iran | 88 | |
Ireland | 12 | |
Italy | 128 | |
Jamaica | 1 | |
Japan | 41 | |
Jordan | 137 | |
Kenya | 13 | |
Malaysia | 25 | |
Mexico | 59 | |
Moldova | 71 | |
Mongolia | 22 | |
Morocco | 131 | |
Netherlands | 129 | |
New Zealand | 148 | |
Niger | 1 | |
Nigeria | 41 | |
Norway | 14 | |
Philippines | 15 | |
Poland | 12 | |
Portugal | 31 | |
Puerto Rico | 51 | |
Romania | 15 | |
Serbia | 18 | |
South Africa | 14 | |
Sweden | 11 | |
Switzerland | 7 | |
Thailand | 61 | |
Trinidad and Tobago | 12 | |
Tunisia | 34 | |
Turkey | 28 | |
Ukraine | 11 | |
Uzbekistan | 123 | |
Venezuela | 23 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>bswrm</title> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
</head> | |
<body> | |
<div id="chart"></div> | |
<script> | |
var width = 1000, height = 500; | |
var svg = d3.select("#chart") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var pointScale = d3.scalePoint() | |
.range([10, width - 10]) | |
.domain(['country']) | |
d3.csv("data.csv", function(data){ | |
data.forEach(d=>{ | |
d.amount = +d.amount; | |
}); | |
var sort = data.sort((a,b)=>d3.descending(a,b)); | |
var vertScale = d3.scaleLinear() | |
.range([height - 30, 80]) | |
.domain([d3.min(data, function(d){return d['amount'] }), | |
d3.max(data, function(d){return d['amount']})]); | |
var simulation = d3.forceSimulation(data) | |
.force("x", d3.forceX(pointScale('country'))) | |
.force("y", d3.forceY( | |
function(d){return vertScale(d['amount'])} | |
).strength(2) | |
) | |
.force("collide", d3.forceCollide(7)) | |
.stop(); | |
for (var i = 0; i < data.length*2; ++i) simulation.tick(); | |
var circles = svg.selectAll(".circles") | |
.data(data); | |
var circlesEnter = circles.enter() | |
.append("circle"); | |
circlesEnter.attr("r", 4) | |
.attr("cx", function(d){return d.x}) | |
.attr("cy", function(d){return d.y}) | |
.attr("fill", '#333') | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment