Published
Edited
Feb 16, 2022
1 star
Insert cell
Insert cell
Insert cell
// replace with your data by uploading or importing a different CSV
import {unemployment as data} from "@d3/stacked-area-chart"
Insert cell
categoriesName = "industry"
Insert cell
metricName = "unemployed"
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// a few functions to choose from for sorting the categories
sortOptions = ({
'alphabetical': (a,b) => a[0] > b[0] ? 1 : -1,
'largest to smallest': (a,b) => b[1] - a[1],
'smallest to largest': (a,b) => a[1] - b[1],
})
Insert cell
// extracts and sums the categories from the data based on the metricName and categoriesName
categoriesRolledUp = d3.rollups(data, v => d3.sum(v, d => d[metricName]), d => d[categoriesName])

Insert cell
// extracts the names of the categories from the categoriesRollupUp array
categories = categoriesRolledUp.map(d => d[0])
Insert cell
// filter and sort the categorical strings based on the selections from the checkboxes and radio buttons
categoriesFilteredSorted =
categoriesRolledUp.
filter(d => categoriesToShow.includes(d[0]))
.sort(sortOptions[selectedSortOption])
.map(d => d[0])
Insert cell
dataFiltered = data.filter(d => categoriesFilteredSorted.includes(d[categoriesName]))
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