canvas = {
const perRow = 5;
const presidents = presData.length;
const imageWidth = width / perRow;
const imageHeight = imageWidth;
const svg = d3.create("svg").attr("viewBox", `0, 0, ${width}, ${presidents / perRow * imageHeight}`);
const g = svg.selectAll("g").data(presData).join("g");
g.attr(
"transform",
(d, i) => `translate(${(i % perRow) * imageWidth}, ${Math.floor(i / perRow) * imageWidth})`
);
if (!showColor) {
svg
.attr('filter', 'grayscale(100%)');
}
g.append("image")
.attr("href", (d) => d.image)
.attr("width", imageWidth)
.attr("height", imageHeight)
.attr("opacity", (d) => (100 - (d.dontKnow + d.haveNotHeard)) / 100)
.attr("preserveAspectRatio", "xMidYMin slice");
let textPadding = width > 768 ? 6 : 3;
if (showLabels) {
g.append("text")
.attr("x", imageWidth - textPadding)
.attr("y", textPadding)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'end')
.text((d, i) => i == 0 && width > 968 ?
`${100 - (d.dontKnow + d.haveNotHeard)}% know + have opinion` :
`${100 - (d.dontKnow + d.haveNotHeard)}%`)
.attr('fill', 'none')
.attr('stroke', 'black')
.attr('stroke-width', '2px')
.attr('stroke-linecap', 'round')
.attr('stroke-linejoin', 'round')
.attr('font-weight', '400')
g.append("text")
.attr("x", imageWidth - textPadding)
.attr("y", textPadding)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'end')
.text((d, i) => i == 0 && width > 968 ?
`${100 - (d.dontKnow + d.haveNotHeard)}% know + have opinion` :
`${100 - (d.dontKnow + d.haveNotHeard)}%`)
.attr('font-weight', '400')
.attr('fill', 'white')
g.append("text")
.attr("x", textPadding)
.attr("y", imageHeight - textPadding)
.attr('text-anchor', 'start')
.text(d => d.initials)
.attr('fill', 'none')
.attr('stroke', 'black')
.attr('stroke-width', '2px')
.attr('stroke-linecap', 'round')
.attr('stroke-linejoin', 'round')
.attr('font-weight', '400')
.attr('font-size', '1.5rem');
g.append("text")
.attr("x", textPadding)
.attr("y", imageHeight - textPadding)
.attr('text-anchor', 'start')
.text(d => d.initials)
.attr('font-weight', '400')
.attr('fill', 'white')
.attr('font-size', '1.5rem');
}
return svg.node();
}