chart = {
const chart = new G2.Chart({
width,
height: 800,
paddingLeft: 60
});
const color = d3.scaleSequentialSqrt(
[0, d3.max(tabluarData, (d) => d.value)],
d3.interpolatePuRd
);
const annotateText = (node) =>
node
.data([0])
.encode("x", 1963)
.encode("y", 1)
.scale("y", { type: "identity", independent: true, guide: null })
.scale("x", { type: "band" })
.style("fill", "black")
.style("textAnchor", "center")
.style("fontSize", 10);
chart
.grid()
.data(tabluarData)
.encode("x", "year")
.encode("y", "name")
.encode("tooltip", { type: "field", value: "value" })
.encode("color", (d) =>
isNaN(d.value) ? "#eee" : d.value === 0 ? "#fff" : color(d.value)
)
.scale("y", { range: [0, 1], domain: data.names })
.scale("x", {
domain: data.years,
guide: {
position: "top",
formatter: (d) => (+d % 10 === 0 ? d : "") + ""
}
})
.scale("color", { guide: null, type: "identity" })
.animate("enter", { type: null });
chart
.annotationLineX()
.data([0])
.encode("x", 1963)
.scale("x", { type: "band" })
.style("stroke", "black");
chart.text().call(annotateText).encode("text", 1963).style("dy", "0.5em");
chart
.text()
.call(annotateText)
.encode("text", "Measles vaccine introduced")
.style("dy", "1.2em")
.style("fontWeight", "bold");
return node(chart.render());
}