Published
Edited
May 20, 2022
1 star
Insert cell
Insert cell
untidy = d3.zip(
d3.cumsum({ length: 130 }, d3.randomNormal()),
d3.cumsum({ length: 130 }, d3.randomNormal())
)
Insert cell
Insert cell
timeof = {
const startDate = new Date(2010, 0, 1);
return (index) => d3.utcDay.offset(startDate, index);
}
Insert cell
Plot.plot({
marks: [
Plot.lineY(untidy, { x: (d, i) => timeof(i), y: "0", stroke: "green" }),
Plot.lineY(untidy, { x: (d, i) => timeof(i), y: "1", stroke: "red" }),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Insert cell
tidy = untidy.flatMap(([v1, v2], index) => [
{ series: "a", value: v1, time: timeof(index) },
{ series: "b", value: v2, time: timeof(index) }
])
Insert cell
Insert cell
Plot.plot({
facet: { data: tidy, x: "series" },
marks: [
Plot.frame(),
Plot.lineY(tidy, { x: "time", y: "value", stroke: "series" }),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
facet: { data: tidy, x: "series" },
marks: [
Plot.frame(),
Plot.lineY(tidy, {
x: "time",
y: "value",
z: "series",
facet: "exclude",
stroke: "lightgray",
strokeWidth: 0.5
}),
Plot.lineY(tidy, { x: "time", y: "value", stroke: "series" }),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
{
const shared = Plot.plot({
width: width / 2,
marks: [
Plot.lineY(untidy, { x: (d, i) => timeof(i), y: "0" }),
Plot.lineY(untidy, { x: (d, i) => timeof(i), y: "1" })
]
});
const x = shared.scale("x");
const y = shared.scale("y");

return html`<div style="display: flex">
${[
{ y: "0", stroke: "green" },
{ y: "1", stroke: "red" }
].map((opts) =>
Plot.plot({
width: width / 2,
marks: [
Plot.frame(),
Plot.ruleY([0]),
Plot.lineY(untidy, { x: (d, i) => timeof(i), ...opts })
],
x,
y
})
)}
</div>`;
}
Insert cell
Insert cell
Plot.plot({
width,
facet: { data: ["a", "b"], x: (d) => d },
marks: [
Plot.frame(),
Plot.lineY(untidy, {
transform: (data, facets) => ({
data: data.flat(), // shortcut for, more generally, data.flatMap(d => [first, second…])
facets: [
d3.range(0, 2 * data.length, 2), // evens: column "0"
d3.range(1, 2 * data.length, 2) // odds: column "1"
]
}),
x: (d, i) => timeof(Math.floor(i / 2)),
y: (d) => d,
facet: true,
stroke: (d, i) => (i % 2 === 0 ? "a" : "b"),
strokeWidth: 0.5
}),
Plot.ruleY([0])
]
})
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