Published
Edited
Sep 7, 2022
1 star
Insert cell
Insert cell
Insert cell
chart = {
const chart = new G2.Chart({
width,
height: 1000,
paddingLeft: 30
});

chart
.coordinate({ type: "transpose" })
.data(data)
.call((chart) =>
order === "time"
? chart
: chart.transform({
type: "connector",
callback: (data) =>
groupSort(
data,
([, values]) => d3.min(values, (d) => d.start),
(d) => d.region
)
})
);

chart
.interval()
.scale("x", {
guide: null
})
.scale("color", { range: d3.schemeSet2 })
.scale("y", {
guide: { formatter: (d) => Math.abs(d) + (d < 0 ? "BC" : "AC") }
})
.encode("x", "civilization")
.encode("y", ["start", "end"])
.encode("color", "region");

chart
.text()
.encode("x", "civilization")
.encode("y", (d) => (d.end > -1500 && d.start > -3000 ? d.start : d.end))
.encode("text", "civilization")
.style("textAnchor", "end")
.style("fontSize", 10)
.style("fill", "black")
.style("dy", 5)
.style("dx", -5);

return node(chart.render());
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const options = {
type: "view",
coordinates: [{ type: "transpose" }],
children: [
{
type: "interval",
transform: order === "region" && [
{ type: "sortX", group: "color", channel: "y" }
],
scale: {
color: { range: d3.schemeSet2 },
y: { formatter: (d) => Math.abs(d) + (d < 0 ? "BC" : "AC") },
x: { display: null }
},
encode: {
x: "civilization",
y: ["start", "end"],
color: "region"
},
labels: [
{
text: "civilization",
position: (d) =>
d.end > -1500 && d.start > -3000 ? "start" : "end",
textAnchor: (d) =>
d.end > -1500 && d.start > -3000 ? "start" : "end"
}
]
},
{
type: "axisY",
style: { position: "top", scales: ["y"] }
}
]
};
return md`A better spec...`;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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