Public
Edited
Oct 6, 2022
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
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
{
//create svg
const svg = d3.create("svg").attr("width", width).attr("height", height);
svg
.append("rect")
.attr("width", width)
.attr("height", height)
.attr("fill", backgroundColor);

//loop through architects - though it does not really matter which is chosen since they have the same length
for (let i = 0; i < leftData.length; i++) {
const leftState = leftData[i];
const rightState = rightData[i];

//right side bars
svg
.append("rect")
.attr("width", barWidthScale(rightState[dataKey]))
.attr("height", barHeight)
.attr("x", width / 2 + innerMargin / 2)
.attr("y", i * barHeight + margin)
.attr(
"fill",
d3.interpolateOranges(1 - rightColorScale(rightState[dataKey]))
)
.attr("stroke", "white")
.attr("stroke-width", 2);

//left side bars
svg
.append("rect")
.attr("width", barWidthScale(leftState[dataKey]))
.attr("height", barHeight)
.attr(
"x",
width / 2 - barWidthScale(leftState[dataKey]) - innerMargin / 2
)
.attr("y", i * barHeight + margin)
.attr(
"fill",
d3.interpolatePurples(1 - leftColorScale(leftState[dataKey]))
)
.attr("stroke", "white")
.attr("stroke-width", 2);

//middle text labels
svg
.append("text")
.text(leftData[i].abbreviation)
.attr("x", width / 2)
.attr("y", i * barHeight + margin + barHeight / 2)
.attr("text-anchor", "middle")
.attr("alignment-baseline", "middle")
.attr("fill", "white")
.attr("font-family", "courier");
}

//left label
svg
.append("text")
.text(leftCreatorType + " : " + leftRacialCategory)
.attr("x", margin / 2)
.attr("y", height / 2)
.attr("fill", "white")
.attr("font-family", "courier")
.attr("alignment-baseline", "middle")
.attr("text-anchor", "middle")
.attr("transform", `rotate(-90 ${margin / 2} ${height / 2} )`);

//right label
svg
.append("text")
.text(rightCreatorType + " : " + rightRacialCategory)
.attr("x", width - margin / 2)
.attr("y", height / 2)
.attr("fill", "white")
.attr("font-family", "courier")
.attr("text-anchor", "end")
.attr("alignment-baseline", "middle")
.attr("text-anchor", "middle")
.attr("transform", `rotate(90 ${width - margin / 2} ${height / 2} )`);

//title
svg
.append("text")
.attr("x", width / 2)
.attr("y", margin / 2)
.text(
`${leftCreatorType} (${leftRacialCategory}) Compared with ${rightCreatorType} (${rightRacialCategory}) | ${dataMode}`
)
.attr("text-anchor", "middle")
.attr("fill", "white")
.attr("font-family", "courier");

return svg.node();
}
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