Published
Edited
Aug 17, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("fill", "none")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round");

const gradient = DOM.uid();

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

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

svg
.append("linearGradient")
.attr("id", gradient.id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", 0)
.attr("y1", height - margin.bottom)
.attr("x2", 0)
.attr("y2", margin.top)
.selectAll("stop")
.data(d3.ticks(0, 1, 10))
.join("stop")
.attr("offset", (d) => d)
.attr("stop-color", color);

svg
.append("path")
.datum(blossoms.filter(line.defined()))
// .attr("fill", "none")
.attr("stroke", "#ccc")
.attr("d", line);

svg
.append("path")
.datum(blossoms)
// .attr("fill", "none")
.attr("stroke", gradient)
.attr("stroke-width", 1.5)
.attr("d", line);

return svg.node();
}
Insert cell
// legend({
// color: d3.scaleSequential([0, 100], ["#edd1cb", "#6e4071", "#2d1e3e"]),
// title: blossoms.title
// })
Insert cell
color = d3
.scaleOrdinal()
.domain([d3.min(blossoms, (d) => d.date), d3.max(blossoms, (d) => d.date)])
.range(petalColors)
Insert cell
Insert cell
line(blossoms)
Insert cell
line = d3
.line()
.defined((d) => !isNaN(d.date))
.x((d) => x(d.year))
.y((d) => y(d.date))
Insert cell
xAxis = (g) =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(
d3
.axisBottom(x)
.ticks(width / 80)
.tickSizeOuter(0)
)
.call((g) =>
g
.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "#2d1e3e")
.attr("text-anchor", "end")
.text(blossoms.x)
)
.call((g) => g.selectAll(".tick line").attr("stroke", "#2d1e3e"))
.call((g) => g.selectAll(".tick text").attr("fill", "#2d1e3e"))
Insert cell
yAxis = (g) =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(width / 120))
.call((g) => g.select(".domain").remove())

.call((g) =>
g
.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "#2d1e3e")
.attr("text-anchor", "start")
.text(blossoms.y)
)
.call((g) => g.selectAll(".tick line").attr("stroke", "#2d1e3e"))
.call((g) => g.selectAll(".tick text").attr("fill", "#2d1e3e"))
Insert cell
d3.extent(blossoms, (d) => d.year)
Insert cell
x = d3
.scaleLinear()
.domain(d3.extent(blossoms, (d) => d.year))
// .nice()
.range([margin.left, width - margin.right])
Insert cell
d3.min(blossoms, (d) => d.date)
Insert cell
d3.max(blossoms, (d) => d.date)
Insert cell
d3.timeFormat("%m%d")(d3.median(blossoms, (d) => d.date))
Insert cell
d3.timeFormat("%m%d")(d3.mean(blossoms, (d) => d.date))
Insert cell
y = d3
.scaleTime()
.domain([d3.min(blossoms, (d) => d.date), d3.max(blossoms, (d) => d.date)])
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
blossoms = Object.assign(
d3
.csvParse(await FileAttachment("peak-bloom.csv").text())
.map(({ year, date }) => ({
year: d3.timeFormat("%Y")(new Date(year)),
date: d3.utcParse("%m%d")("0" + date)
? d3.utcParse("%m%d")("0" + date)
: undefined
})),
{ title: "Cherry Blossom Peak-bloom Date", y: " date" },
{ x: "Year →", y: "↑ Peak-bloom date" }
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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