Notebooks 2.0 is here.

Published
Edited
Nov 4, 2021
Insert cell
# bar-chart
Insert cell
chart = {
const svg = d3.select(DOM.svg(width,height));
svg.append("g")
.attr("fill", "#20C4DC")
.selectAll("rect").data(arr).enter().append("rect")
.attr("x", d => x(d.hour))
.attr("y", d => y(d.count))
.attr("height", d => y(0) - y(d.count))
.attr("width", x.bandwidth())

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

svg.append("g")
.call(yAxis)
return svg.node();
}
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
Insert cell
xAxis = g => g
.attr("transform", `rotate(45)`)
.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(d3.axisBottom(x)
.tickSize(0))
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(arr, d => d.count)]).nice()
.range([height - margin.bottom ,margin.top])
Insert cell

x = d3.scaleBand()
.domain(arr.map(d => d.hour))
.range([margin.left, width - margin.right])
.padding(0.1)

Insert cell
width = 450;
Insert cell
height = 450;
Insert cell
margin = ({top: 20, right:20, bottom:30, left: 35})
Insert cell
d3 = require("d3@5");
Insert cell
pie_funciton = d3.pie();
Insert cell
arr = [{count:100 ,hour :0},{ count: 200, hour: 1},{count:300, hour:2}]
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