Published
Edited
Sep 6, 2022
Insert cell
Insert cell
{
let [min, max] = d3.extent(data, (d) => d.usage);
max = Math.max(-min, max);
const color = d3.scaleDiverging([-max, 0, max], (t) =>
d3.interpolateRdBu(1 - t)
);
const formatMonth = d3.timeFormat("%b %-d");
const formatDate = d3.timeFormat("%-d");
const formatDay = (d) => (d.getDate() === 1 ? formatMonth : formatDate)(d);
const formatHour = (d) =>
d === 0 ? "12 AM" : d === 12 ? "12 PM" : (d % 12) + "";

return G2.render({
type: "grid",
width,
height: 4000,
data,
scale: {
x: { guide: { formatter: formatHour, position: "top" } },
y: { guide: { formatter: (d) => formatDay(new Date(d)) } },
color: { type: "identity" }
},
encode: {
x: (d) => d.date.getHours(),
y: (d) => +d3.timeDay(d.date),
color: (d) => color(d.usage)
},
animate: { enter: { type: null } }
});
}
Insert cell
Insert cell
Insert cell
{
const formatMonth = d3.timeFormat("%b %-d");
const formatDate = d3.timeFormat("%-d");
const formatDay = (d) => (d.getDate() === 1 ? formatMonth : formatDate)(d);
const formatHour = (d) =>
d === 0 ? "12 AM" : d === 12 ? "12 PM" : (d % 12) + "";
const options = {
type: "cell",
width,
data,
scale: {
x: { formatter: formatHour, position: "top" },
y: { formatter: formatDay },
color: {
type: "diverging",
interpolator: (t) => d3.interpolateRdBu(1 - t)
}
},
encode: {
x: (d) => d.date.getHours(),
y: (d) => d3.timeDay(d.date),
color: "usage"
}
};
return md`A better spec...`;
}
Insert cell
Insert cell
chart
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