Public
Edited
Aug 23, 2021
1 star
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
Insert cell
Insert cell
Insert cell
Insert cell
data = {
const resolve = (url, acc = []) => {
const data = d3.json(url).then((response) => {
const next = acc.concat(
response.data.map((d) => ({
date: d3.utcParse("%Y-%m-%d")(d.record_date),
value: +d.tot_pub_debt_out_amt
})
));
if (response.links.next) {
return resolve(`${endpoint}?${response.links.next}`, next);
}

return next;
});
return data;
};
return resolve(`${endpoint}?page[number]=1&page[size]=1000`);
}
Insert cell
valueByDate = {
const acc = {};
for (const { date, value } of data) {
acc[date.toISOString()] = value;
}
return acc;
}
Insert cell
partitionDataByDateRange = (start, end) => {
const slice = data.slice(
data.findIndex(
d => d.date.toISOString() === parseDate(start).toISOString()
),
data.findIndex(d => d.date.toISOString() === parseDate(end).toISOString())
);

return slice;
}
Insert cell
cagr = (slice) => {
const { date: startDate, value: startValue } = slice[0];
const { date: endDate, value: endValue } = slice[slice.length - 1];
const diffMs = endDate.getTime() - startDate.getTime();
const totalYears = diffMs / (1000 * 60 * 60 * 24 * 365);
return (Math.pow(endValue / startValue, 1 / totalYears) - 1) * 100;
}
Insert cell
Insert cell
parseDate = d3.utcParse('%Y-%m-%d');
Insert cell
colors = ({
annotation: '#4B5563',
dem: '#2563EB',
rep: '#DC2626',
positive: '#D97706',
negative: '#7C3AED',
axis: '#D1D5DB'
})
Insert cell
height = 600;
Insert cell
margin = ({ top: 40, left: 40, right: 40, bottom: 40 });
Insert cell
Insert cell
d3 = require('d3@6');
Insert cell
_ = require('lodash');
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