{
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 } }
});
}