Public
Edited
May 31, 2024
1 star
Insert cell
Insert cell
{
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const iwidth = width - margin.right - margin.left;
const iheight = height - margin.top - margin.bottom;

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

const x = d3
.scaleLinear()
.domain(d3.extent(data, (d) => d[quantAttrib]))
.range([0, iwidth])
.nice();

const y = d3
.scaleLinear()
.domain([0, iwidth])
.range(d3.extent(data, (d) => d[quantAttrib]))
.nice();

const color = d3
.scaleOrdinal(d3.schemePastel1)
.domain(new Set(data.map((d) => d[catAttrib])).values());

g.append("g").call(d3.axisTop(x));

g.selectAll(".row")
.data(data)
.join("circle")
.attr("r", 5)
.attr("cx", (d) => x(d[quantAttrib]))
.attr("cy", (d) => y(d[quantAttrib]))
.style("fill", (d) => color(d[catAttrib]));

return svg.node();
}
Insert cell
quantAttrib = "Beak Length (mm)"
Insert cell
catAttrib = "Species"
Insert cell
data = await vegaDatasets["penguins.json"]()
Insert cell
Insert cell
height = 100
Insert cell
margin = ({ left: 20, right: 20, top: 20, bottom: 20 })
Insert cell
vegaDatasets = require("vega-datasets@2")
Insert cell
d3 = require("d3@6")
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