Published
Edited
Mar 1, 2022
2 forks
19 stars
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
eras = [
{
start: "1782-01-01",
end: "1832-01-01",
name: "Portrait Era"
},
{
start: "1832-01-01",
end: "1931-01-01",
name: "Black and White Era"
},
{
start: "1931-01-01",
end: "2028-01-01",
name: "Color Era"
}
]
Insert cell
leastKnownImages = {
const svg = d3.create("svg").attr("viewBox", `0, 0, ${700}, ${300}`);
const g = svg.selectAll("g").data(leastKnown).join("g");

g.attr(
"transform",
(d, i) => `translate(${(i % 5) * 150}, ${Math.floor(i / 5) * 150})`
);

g.append("image")
.attr("href", (d) => d.image)
.attr("width", 100)
.attr("height", 120)
.attr("preserveAspectRatio", "xMidYMin slice");

if (showNames % 2) {
g.append("text")
.attr("font-size", 12)
.attr("text-anchor", "middle")
.attr("dy", 132)
.attr("dx", 50)
.text((d) => d.name);
}

return svg.node();
}
Insert cell
preserveAspectRatio = "xMidYMin slice"
Insert cell
stackedPresData = d3.merge(
[
'veryFav',
'someFav',
'dontKnow',
'haveNotHeard',
'someUnfav',
'veryUnfav'
].map(d =>
presData.map(({ name, initials, date, image, ...p }) => ({
name,
initials,
date,
image,
answer: d,
percent: p[d]
}))
)
)
Insert cell
leastKnown = d3
.reverse(d3.sort(presData, d => d.dontKnow + d.haveNotHeard))
.slice(0, 10)
Insert cell
presData = rawPresidentData.map(d => {
return {
name: d.Name,
initials: d.Name.split(' ')
.map(d => d[0])
.join(''),
veryFav: d['Very Favorable %'],
someFav: d['Somewhat Favorable %'],
someUnfav: d['Somewhat Unfavorable %'],
veryUnfav: d['Very Unfavorable %'],
dontKnow: d['Don’t know %'],
haveNotHeard: d['Have not heard of them %'],
date: d['First Inauguration Date'],
image: d['Portrait URL']
};
})
Insert cell
rawPresidentData = FileAttachment("us-president-favorability.csv").csv({
typed: true
})
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