Public
Edited
Mar 30, 2022
Insert cell
Insert cell
numbers = d3
.range(Math.round(Math.random() * 20))
.map((d) => Math.round(Math.random() * 100))
Insert cell
svg`<svg id='viz' width='600' height='400'></svg>`
Insert cell
{
const scaleLength = d3.scaleLinear().domain([0, 40]).range([0, 200]);
const scaleY = d3
.scaleBand()
.domain(d3.range(numbers.length))
.range([0, 300])
.paddingInner(0.05);

const gs = d3
.select("#viz")
.selectAll("g")
.data(numbers)
.join("g")
.attr("transform", (d, i) => `translate(${10},${scaleY(i)})`);

// .selectAll("rect")
// .data(numbers)
// .join("rect")
gs.selectAll("rect")
.data((d) => [d])
.join("rect")
.attr("height", scaleY.bandwidth())
.attr("width", scaleLength)
.attr("fill", "orange");

// d3.select("#viz")
// .selectAll("text")
// .data(numbers)
// .join("text")
gs.selectAll("text")
.data((d) => [d])
.join("text")
.attr("dx", 5)
.attr("y", scaleY.bandwidth())
.attr("dy", -3)
.text((d) => d);
}
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