{
const formatter = metric === "absolute" ? "+,d" : "+.1%";
const xy = { x: "State", y: "Change" };
const options = {
width,
coordinates: [{ type: "transpose" }],
data: {
value: states,
transform: [
{
type: "derive",
Change: (d) =>
metric === "absolute" ? d[2019] - d[2010] : d[2019] / d[2010] - 1
}
]
},
children: [
{
type: "interval",
scale: {
color: {
type: "divergingOrdinal",
range: d3.schemeRdBu[3],
display: false
},
y: {
tickCount: 15,
title: false,
nice: true,
position: "top",
formatter
},
x: { display: false }
},
transform: [{ type: "sortX", channel: "y" }],
encode: { ...xy, color: "Change" }
},
{ type: "lineY", data: [0] },
{
type: "text",
encode: {
...xy,
text: { value: "Change", formatter },
textAnchor: (d) => (d.Change < 0 ? "end" : "start"),
dx: (d) => (d.Change < 0 ? 5 : -5)
}
},
{
type: "text",
encode: {
...xy,
y: 0,
text: "State",
textAnchor: (d) => (d.Change < 0 ? "start" : "end"),
dx: (d) => (d.Change < 0 ? -5 : 5)
}
},
{
type: "text",
data: [0, 0, "← decrease · Change in population · increase →"],
scale: { x: { type: "identity" } },
style: { textAnchor: "middle", dy: -30 }
}
]
};
return md`A better description...`;
}