Published
Edited
Apr 8, 2021
1 fork
4 stars
Insert cell
Insert cell
chart = {
const r = radius + padding / 2;
const beeswarmData =
new AccurateBeeswarm.AccurateBeeswarm(data, r, d => x(d.value))
.withTiesBrokenRandomly()
.calculateYPositions()
.map(({datum, x, y}) => ({data: datum, x, y}));
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.call(xAxis);

svg.append("g")
.selectAll("circle")
.data(beeswarmData)
.join("circle")
.attr("cx", d => d.x)
.attr("cy", d => height / 2 + d.y)
.attr("r", radius)
.append("title")
.text(d => d.data.name);

return svg.node();
}
Insert cell
data = (await FileAttachment("cars.csv").csv({typed: true})).map(({Name: name, Weight_in_lbs: value}) => ({name, value}))
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(data, d => d.value))
.range([margin.left, width - margin.right])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
Insert cell
height = 240
Insert cell
radius = 3
Insert cell
padding = 1.5
Insert cell
margin = ({top: 20, right: 20, bottom: 30, left: 20})
Insert cell
d3 = require("d3@6")
Insert cell
AccurateBeeswarm = import("accurate-beeswarm-plot")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more