Public
Edited
Jan 10, 2023
Insert cell
Insert cell
Insert cell
import { beeswarmForce } from "@harrystevens/force-directed-beeswarm"
Insert cell
// import { general_data } from "@eesur/hr-dataset"
Insert cell
general_data.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
beeswarm(general_data, {
bind: d3.select(".vis-1")
})
Insert cell
function beeswarm(
data,
{
bind = null,
width = 1000,
height = 500,
r = d3.scaleSqrt([100, 1000], [1, Math.sqrt(width * height) / 30]),
margin = {
top: 100,
right: 0,
bottom: 10,
left: 100
}
}
) {
// remove the vis before renderging, can take out if exporting the code locally to JS.
console.log(data);
bind.selectAll("svg").remove();

const chartwidth = width - margin.left - margin.right;
const chartheight = height - margin.top - margin.bottom;

const beeswarm = beeswarmForce()
.x((d) => x(d.Age))
.y(chartheight / 2)
.r(1 + 2);
// .r((d) => 1 + r(d.size));

// const random = d3.randomNormal();
// const data = Array.from({ length: 100 }).map((d) => ({
// value: random(),
// size: d3.randomUniform(...r.domain())()
// }));

console.log(data);

const x = d3.scaleLinear(
d3.extent(data, (d) => d.Age),
[0, chartwidth]
);

// const r = d3.scaleSqrt([100, 1000], [1, Math.sqrt(width * height) / 30]);

// make the svg
const svg = bind
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);

const g = svg
.append("g")
.attr("transform", `translate(${[margin.left, margin.top]})`);

g.append("g")
.call(d3.axisBottom(x).tickSizeOuter(0))
.attr("transform", `translate(0, ${chartheight / 1.25})`);

g.selectAll("circle")
.data(beeswarm(data))
.join("circle")
.attr("stroke", "black")
.attr("fill-opacity", 0)
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
.attr("r", 2);
// .attr("r", (d) => r(d.data.size));

return svg.node();
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more