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

const gradient = DOM.uid();

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(area.defined()))
.attr("fill", "#eee")
.attr("opacity", 0.5)
.attr("d", area);

svg
.append("path")
.datum(blossoms)
.attr("fill", gradient)
.attr("opacity", 0.8)
.attr("d", area);

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

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

return svg.node();
}
Insert cell
d3.min(blossoms, (d) => d.date)
Insert cell
color.domain()
Insert cell
color.range()
Insert cell
color = d3
.scaleOrdinal()
.domain([d3.min(blossoms, (d) => d.date), d3.max(blossoms, (d) => d.date)])
.range(petalColors)
Insert cell
petalColors = [
"#edd1cb",
"#e6bdbc",
"#dda9af",
"#d194a4",
"#c3819c",
"#b37093",
"#9f5f8a",
"#8a5081",
"#744375",
"#5b3665",
"#432a53",
"#2d1e3e"
]
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
area(blossoms)
Insert cell
area = d3
.area()
.defined((d) => !isNaN(d.date))
.x((d) => x(d.year))
// .y0(y(0))
.y0(y(d3.min(blossoms, (d) => d.date)))
.y1((d) => y(d.date))
Insert cell
x = d3
.scaleLinear()
.domain(d3.extent(blossoms, (d) => d.year))
.range([margin.left, width - margin.right])
Insert cell
y(blossoms[14].date)
Insert cell
y(d3.min(blossoms, (d) => d.date))
Insert cell
y(0)
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
Insert cell
Insert cell
Insert cell
Insert cell
blossoms = Object.assign(
d3
.csvParse(await FileAttachment("peak-bloom.csv").text())
.map(({ year, date }) => ({
year: yearFormat(new Date(year)),
date: parseDate("0" + date) ? parseDate("0" + date) : undefined
})),
{ title: "Cherry Blossom Peak-bloom Date", y: " date" },
{ x: "Year →", y: "↑ Peak-bloom date" }
)
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