Published
Edited
Mar 4, 2020
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csvParse(await FileAttachment("cmpco2019@2.csv").text(), parseData)
Insert cell
parseData = d => ({date: parseDate(`${d["DateTime"]}`), usage: +d["Value"]})
Insert cell
parseDate = d3.timeParse("%_m/%_d/%Y %H:%M")
Insert cell
dateExtent = d3.extent(data, d => d.date)
Insert cell
x = d3.scaleBand(d3.range(24), [margin.left, width - margin.right]).round(true)
Insert cell
y = d3.scaleBand(d3.timeDays(...dateExtent), [margin.top, height - margin.bottom]).round(true)
Insert cell
color = {
let [min, max] = d3.extent(data, d => d.usage);
if (min < 0) {
max = Math.max(-min, max);
return d3.scaleDiverging([-max, 0, max], t => d3.interpolateRdBu(1 - t));
}
return d3.scaleSequential([0, max], d3.interpolateReds);
}
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(x).tickFormat(formatHour))
.call(g => g.select(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickFormat(formatDay))
.call(g => g.select(".domain").remove())
Insert cell
formatUsage = d3.format(".2f")
Insert cell
formatDate = d3.timeFormat("%B %-d, %-I %p")
Insert cell
formatDay = {
const formatMonth = d3.timeFormat("%b %-d");
const formatDate = d3.timeFormat("%-d");
return d => (d.getDate() === 1 ? formatMonth : formatDate)(d);
}
Insert cell
formatHour = {
return d => d === 0 ? "12 AM" : d === 12 ? "12 PM" : (d % 12) + "";
}
Insert cell
width = 954
Insert cell
height = margin.top + margin.bottom + (d3.timeDay.count(...dateExtent) + 1) * 10
Insert cell
margin = ({top: 70, right: 0, bottom: 0, left: 40})
Insert cell
d3 = require("d3@5")
Insert cell
import {legend} from "@d3/color-legend"
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