Published
Edited
Aug 26, 2021
1 fork
20 stars
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

svg.append("g")
.call(grid);

const trail = svg.append("path")
.attr("fill", "none")
.attr("stroke", "gray")
.attr("stroke-width", 0.5)
.attr("stroke-miterlimit", 1);

const path = svg.append("path")
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-miterlimit", 1)
.attr("stroke-width", 2);

const dot = svg.append("circle")
.attr("fill", "red")
.attr("r", 4);

return Object.assign(svg.node(), {
update(date) {
const i = bisectDate(data, d3.utcMonth.offset(date, -3), 0);
const j = bisectDate(data, date, 0);
trail.attr("d", line(data.slice(0, i)));
path.attr("d", line(data.slice(Math.max(0, i - 1), j)));
dot.attr("cx", x(data[j - 1].volume)).attr("cy", y(data[j - 1].extent));
}
});
}
Insert cell
chart.update(date)
Insert cell
data = Array.from(d3.rollup([].concat(
extent.map(d => ({...d, type: "extent"})),
volume.map(d => ({...d, type: "volume"})),
), v => v.pop(), d => +d.date, d => d.type))
.filter(([, group]) => group.has("extent") && group.has("volume"))
.map(([date, group]) => ({
date: new Date(date),
extent: group.get("extent").value,
volume: group.get("volume").value
}))
Insert cell
extent = {
const parseDate = d3.utcParse("%B %d, %Y");
let month;
let date;
return d3.csvParseRows(await FileAttachment("Sea_Ice_Index_Daily_Extent_G02135_v3.0.csv").text(), (row, i) => {
if (i === 0) return;
if (row[0] !== "") month = row[0];
if (row[1] !== "") date = row[1];
return row
.slice(2)
.map((value, i) => ({
date: parseDate(`${month} ${date}, ${i + 1978}`),
value: value === "" ? null : +value
}))
.filter(({value}) => value !== null);
})
.flat(1)
.sort((a, b) => d3.ascending(a.date, b.date));
}
Insert cell
volume = d3.csvParse((await FileAttachment("PIOMAS.vol.daily.1979.2020.Current.v2.1.dat").text()).replace(/[ ]+/g, ","), ({Year: year, ["#day"]: day, Vol: volume}) => ({date: new Date(Date.UTC(year, 0, day)), value: +volume}))
Insert cell
bisectDate = d3.bisector(d => d.date).left
Insert cell
line = d3.line()
.x(d => x(d.volume))
.y(d => y(d.extent))
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data, d => d.volume)]).nice()
.rangeRound([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.extent)]).nice()
.rangeRound([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks())
.call(g => g.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text("Volume (thousands of km³) →"))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text("↑ Extent (millions of km²)"))
Insert cell
grid = g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));
Insert cell
height = 640
Insert cell
margin = ({top: 20, right: 20, bottom: 36, left: 30})
Insert cell
import {Scrubber} from "@mbostock/scrubber"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more