Published
Edited
Apr 18, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = (dataset) => {
const data = dataset.data.filter(({ date }) => date.getUTCFullYear() >= filterYear);
return Plot.plot({
width,
marginBottom: 50,
color: {
legend: true,
type: dataset.colorType,
scheme: dataset.colorScheme,
reverse: dataset.colorReverse,
label: () => legendLabel(dataset),
},
x: {
label: "",
axis: "top",
// label every 12th bar, i.e. the bar corresponding to January for each year
ticks: data.map((d) => d.date).filter((d, i) => i % 12 === 0),
tickFormat: "%b\n%Y",
grid: true,
},
y: {
label: "",
grid: true,
},
marks: [
// 2012 -2016 drought
Plot.barX([0], {
// Droughts years are measured using water years (Oct-Sep)
x1: new Date(2011, 9, 1),
x2: new Date(2016, 8, 30),
fill: "#ddd",
fillOpacity: 0.4,
clip: true
}),
Plot.text(["2012-2016 Drought"], {
x: new Date(2014, 7, 1),
y: 4,
lineWidth: 6,
textAnchor: "middle",
clip: true,
fontSize: 12,
lineHeight: 1.5,
}),
// All data
Plot.barY(data, {
y: "anomaly",
x1: "date",
fill: "anomaly",
title: (d) => `${d3.timeFormat("%b %Y")(d.date)}: ${d.anomaly}`,
}),
Plot.ruleY([0]),
]
})
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more