Published
Edited
Sep 23, 2022
2 forks
7 stars
Also listed in…
Retail
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { data, advancedDate, industryDateMap } from "@observablehq/monthly-retail-trade-report-data"
Insert cell
// we define the limits of our data to be 2019 to the date of the latest report
dateExtent = [d3.utcParse("%Y-%m-%d")("2019-09-01"), d3.utcMonth.offset(advancedDate, 2)]
Insert cell
Insert cell
dataIndustries = data.filter(d => d.naics.indexOf("*") < 0 && d.date >= dateExtent[0] && d.date <= dateExtent[1])
Insert cell
advancedIndustries = Array.from(industryDateMap.keys()).filter(d => d.indexOf("*") < 0 && industryDateMap.get(d).get(advancedDate))
Insert cell
dates = Array.from(new Set(dataIndustries.map(d => d.date)))
Insert cell
function pointIndex(index) {
return function (series) {
return series.map((d, i) => {
return (d / series[index] - 1);
});
};
}
Insert cell
percent = d3.format("+0.2%")
Insert cell
percentColor = (n) => textcolor(percent(n), { color: n > 0 ? 'darkgreen' : d3.schemeTableau10[2] })
Insert cell
dateFormat = d3.utcFormat("%b. %d, %Y")
Insert cell
monthFormat = d3.utcFormat("%B %Y")
Insert cell
recessionMarks = (max) => [
// recession annotations from https://www.nber.org/research/data/us-business-cycle-expansions-and-contractions
Plot.rect([{}], {
x1: d3.utcParse("%Y-%m-%d")("2020-02-01"),
x2: d3.utcParse("%Y-%m-%d")("2020-06-01"),
y1: 0,
y2: max,
fill: "lightgray",
fillOpacity: 0.5,
clip: true
}),
Plot.text([{}], { text: d => "COVID-19 Recession", x: d3.utcParse("%Y-%m-%d")("2020-02-01"), dy: -15, frameAnchor: "top", textAnchor: "start", clip: true}),
Plot.rect([{}], {
x1: d3.utcParse("%Y-%m-%d")("2007-12-01"),
x2: d3.utcParse("%Y-%m-%d")("2009-06-01"),
y1: 0,
y2: max,
fill: "lightgray",
fillOpacity: 0.5,
clip: true
}),
Plot.text([{}], { text: d => "2008 Recession", x: new Date("2007-12-01"), dy: -15, frameAnchor: "top", textAnchor: "start", clip: true}),
]
Insert cell
industryCategories = ({
"All other gen. merchandise stores": "General",
"Automobile and other motor vehicle dealers": "Auto",
"Automotive parts, acc., and tire stores": "Auto",
"Beer, wine and liquor stores": "Food",
"Building mat. and garden equip. and supplies dealers": "Home",
"Building mat. and supplies dealers": "Home",
"Clothing and clothing access. stores": "Fashion",
"Clothing stores": "Fashion",
"Department stores": "General",
"Electronic shopping and mail order houses": "Other",
"Electronics and appliance stores": "Home",
"Food and beverage stores": "Food",
"Food services and drinking places": "Food",
"Fuel dealers": "Auto",
"Furniture and home furnishings stores": "Home",
"Furniture, home furn, electronics, and appliance stores": "Home",
"Gasoline stations": "Auto",
"General merchandise stores": "General",
"Grocery stores": "Food",
"Health and personal care stores": "Other",
"Jewelry stores": "Fashion",
"Men's clothing stores": "Fashion",
"Miscellaneous stores retailers": "General",
"Motor vehicle and parts dealers": "Auto",
"Nonstore retailers": "Other",
"Other general merchandise stores": "General",
"Pharmacies and drug stores": "Other",
"Shoe stores": "Fashion",
"Sporting goods, hobby, musical instrument, and book stores": "Other",
"Warehouse clubs and superstores": "General",
"Women's clothing stores": "Fashion",
})
Insert cell
import { Plot } from "@mkfreeman/plot-tooltip"
Insert cell
import {textcolor} from "@observablehq/text-color-annotations-in-markdown"
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