Public
Edited
Dec 6, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = import("d3")
Insert cell
<div id="chart"></div>
Insert cell
Insert cell
svg = d3.select("#chart")
.append("svg")
.attr("width", 500)
.attr("height", 500)
Insert cell
xScale = d3.scaleBand()
.domain(data.map(d => d.name))
.range([0, 400])
.padding(0.2)
Insert cell
data = [
{name: "A", value: 10},
{name: "B", value: 20},
{name: "C", value: 30},
{name: "D", value: 40},
{name: "E", value: 50}
]
Insert cell
yScale = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([400, 0])
Insert cell
xAxis = d3.axisBottom(xScale)
Insert cell
yAxis = d3.axisLeft(yScale)
Insert cell
svg.append("g")
.attr("transform", "translate(50, 0)")
.call(yAxis);
Insert cell
svg.append("g")
.attr("transform", "translate(50, 400)")
.call(xAxis)
Insert cell
bars = svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", d => xScale(d.name) + 50)
.attr("y", d => yScale(d.value))
.attr("width", xScale.bandwidth())
.attr("height", d => 400 - yScale(d.value))
.attr("fill", "steelblue")
Insert cell
Insert cell
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