Public
Edited
Aug 1, 2023
7 forks
3 stars
Insert cell
Insert cell
chart = {

// Specify the chart’s dimensions.
const width = 928;
const height = 600;
const marginTop = 20;
const marginRight = 20;
const marginBottom = 30;
const marginLeft = 30;

// Create the positional and color scales.
const x = d3.scaleTime()
.domain(d3.extent(data, d => d.date))
.range([marginLeft, width - marginRight]);

const y = d3.scaleLinear()
.domain([
d3.min(data, d => Math.min(d.value0, d.value1)),
d3.max(data, d => Math.max(d.value0, d.value1))
])
.range([height - marginBottom, marginTop]);

const colors = [d3.schemeRdYlBu[3][2], d3.schemeRdYlBu[3][0]];

// Create the SVG container.
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;")
.datum(data);

// Create the axes.
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(x)
.ticks(width / 80)
.tickSizeOuter(0))
.call(g => g.select(".domain").remove());

svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("x2", width - marginLeft - marginRight)
.attr("stroke-opacity", 0.1))
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", -marginLeft)
.attr("y", -30)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text("↑ Temperature (°F)"));

// Create the clipPaths.
svg.append("clipPath")
.attr("id", "above")
.append("path")
.attr("d", d3.area()
.curve(d3.curveStep)
.x(d => x(d.date))
.y0(0)
.y1(d => y(d.value1)));

svg.append("clipPath")
.attr("id","below")
.append("path")
.attr("d", d3.area()
.curve(d3.curveStep)
.x(d => x(d.date))
.y0(height)
.y1(d => y(d.value1)));

// Create the color areas.
svg.append("path")
.attr("clip-path", "url(#above)")
.attr("fill", colors[1])
.attr("d", d3.area()
.curve(d3.curveStep)
.x(d => x(d.date))
.y0(height)
.y1(d => y(d.value0)));

svg.append("path")
.attr("clip-path", "url(#below)")
.attr("fill", colors[0])
.attr("d", d3.area()
.curve(d3.curveStep)
.x(d => x(d.date))
.y0(0)
.y1(d => y(d.value0)));

// Create the black line.
svg.append("path")
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", d3.line()
.curve(d3.curveStep)
.x(d => x(d.date))
.y(d => y(d.value0)));
return svg.node();
}
Insert cell
data = {
const parseDate = d3.timeParse("%Y%m%d");
return d3.tsvParse(await FileAttachment("weather.tsv").text(), d => ({
date: parseDate(d.date),
value0: +d["San Francisco"], // The primary value.
value1: +d["New York"] // The secondary comparison value.
}));
}
Insert cell
Insert cell
Plot.plot({
y: { grid: true },
color: {scheme: "RdYlBu"},
marks: [
Plot.areaY(data, {
x: "date",
y1: "value0",
y2: "value1",
curve: "step-after",
fill: (d) => (d["value0"] < d["value1"] ? "SF" : "NY"),
z: null
}),
Plot.lineY(data, {
x: "date",
y: "value1",
curve: "step-after"
})
]
})
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