Published
Edited
Nov 11, 2020
Insert cell
Insert cell
Insert cell
b = a + 10
Insert cell
message = "Hello World!"
Insert cell
html`<p>${message}</>`
Insert cell
a = 5 + 7
Insert cell
Insert cell
daysInAYear = 365.24
Insert cell
daysLeftThisYear = daysInAYear -
(now - new Date(new Date(now).getFullYear(), 0, 0)) / (1000.0 * 60 * 60 * 24)
Insert cell
{
let margin = 30;
let size = 300;
const ctx = DOM.context2d(size, size);
let startAngle = 1.5 * Math.PI;

function drawSlice(angle, color) {
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(size / 2, size / 2);
ctx.arc(
size / 2,
size / 2,
size / 2 - 2 * margin,
startAngle,
startAngle + angle
);
ctx.closePath();
ctx.fill();
}

drawSlice(2 * Math.PI, "blue");
drawSlice((daysLeftThisYear / daysInAYear) * 2 * Math.PI, "red");

return ctx.canvas;
}
Insert cell
Insert cell
attributes = [
"mean_temperature",
"min_temperature_200cm",
"max_temperature_200cm"
]
Insert cell
Insert cell
md`Winter is coming ${sparkline(
data.slice(-14).map(item => item.min_temperature_200cm)
)}.`
Insert cell
Insert cell
meanTemps = data.map(item => item.mean_temperature)
Insert cell
import { chart as histogram } with { meanTemps as data } from "@d3/histogram"
Insert cell
histogram
Insert cell
Insert cell
viewof heatmap = vl
.markRect({ tooltip: { content: "data" }, clip: true })
.data(data)
.encode(
vl
.y()
.fieldO("date")
.timeUnit("month"),
vl
.x()
.fieldO("date")
.timeUnit("date"),
vl
.color()
.average("mean_temperature")
.scale({ scheme: "redyellowblue", reverse: true })
)
.render()
Insert cell
Insert cell
dateExtent = d3.extent(data, d => d.date)
Insert cell
margin = ({ top: 10, right: 70, bottom: 50, left: 10 })
Insert cell
x(new Date("2020-07-01"))
Insert cell
x = d3
.scaleUtc()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3
.scaleLinear()
.domain([
d3.min(data, d => d.min_temperature_200cm),
d3.max(data, d => d.max_temperature_200cm)
])
.range([height - margin.bottom, margin.top])
Insert cell
height = 500
Insert cell
xAxis = d3.axisBottom(x).tickSizeOuter(0)
Insert cell
yAxis = d3.axisRight(y)
Insert cell
tempChart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

svg
.append("path")
.datum(data)
.attr("fill", "steelblue")
.attr("opacity", 0.2)
.attr(
"d",
d3
.area()
.x(d => x(d.date))
.y0(d => y(d.min_temperature_200cm))
.y1(d => y(d.max_temperature_200cm))
);

svg
.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr(
"d",
d3
.line()
.x(d => x(d.date))
.y(d => y(d.mean_temperature))
);

const grid = svg
.append("g")
.attr("stroke", "black")
.attr("stroke-opacity", 0.1);
grid
.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom);
grid
.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right);

svg
.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(xAxis)
.call(g => g.select(".domain").remove());

svg
.append("g")
.attr("transform", `translate(${width - margin.right},0)`)
.call(yAxis)
.call(g => g.select(".domain").remove())
.call(g =>
g
.select(".tick:last-of-type text")
.clone()
.attr("x", -3)
.attr("text-anchor", "end")
.attr("font-weight", "bold")
.text("temperature (°C)")
);

const marker = svg.append("g").style("display", "none");
const markerLine = marker
.append("line")
.attr("stroke", "black")
.attr("stroke-opacity", 0.8)
.attr("y2", -(height - margin.top - margin.bottom));
const markerDateLabel = marker
.append("text")
.attr("y", 30)
.attr("text-anchor", "middle")
.attr("font-size", 12);
const markerValue = marker
.append("circle")
.attr("r", 3)
.attr("fill", "red");

svg.on("touchmove mousemove", function(event) {
const item = bisect(d3.pointer(event, this)[0]);
marker
.style("display", null)
.attr(
"transform",
`translate(${x(item.date)},${height - margin.bottom})`
);
markerDateLabel.text(formatDate(item.date));
markerValue.attr("cy", y(item.mean_temperature));
});

svg.on("touchend mouseleave", () => {
marker.style("display", "none");
});

return svg.node();
}
Insert cell
Insert cell
function formatDate(date) {
return date.toLocaleString("en", {
month: "short",
day: "numeric",
year: "numeric",
timeZone: "UTC"
});
}
Insert cell
bisect = {
const bisect = d3.bisector(d => d.date).left;
return mx => {
const date = x.invert(mx);
const index = bisect(data, date, 1);
const a = data[index - 1];
const b = data[index];
return b && date - a.date > b.date - date ? b : a;
};
}
Insert cell
Insert cell
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