Published
Edited
Sep 6, 2022
1 star
Insert cell
Insert cell
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());
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const options = {
type: "view",
children: [
{
type: "cell",
width,
data: tabluarData,
transform: [{ type: "sortX" }, { type: "sortY" }],
scale: {
color: {
interpolator: d3.interpolatePuRd,
relations: [
[isNaN, "#eee"],
[0, "#fff"]
]
},
y: { range: [0, 1] },
x: { position: "top", formatter: (d) => (+d % 10 === 0 ? d : "") }
},
encode: {
x: "year",
y: "name",
color: "value"
}
},
{ type: "lineX", data: [1663] },
{
type: "text",
encode: {
x: 1963,
text: 1963,
y: { value: 1, visual: true },
translateX: false
},
style: { dy: "0.5em" }
},
{
type: "text",
encode: {
x: 1963,
text: "Measles vaccine introduced",
y: { value: 1, visual: true },
translateX: false
},
style: { dy: "1.2em" }
}
]
};
return md`A better spec...`;
}
Insert cell
Insert cell
d3Chart
Insert cell
Insert cell
import { data, chart as d3Chart } from "@mbostock/the-impact-of-vaccines"
Insert cell
G2 = require("@antv/g2@5.0.0-alpha.0")
Insert cell
import { node } from "@pearmini/g2-utils"
Insert cell
vaccines = FileAttachment("vaccines.json").json()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more