Public
Edited
Apr 15, 2023
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("font", "12px sans-serif");

const color = d3.scaleSequential(y.domain(), d3.interpolateTurbo);
const gradient = DOM.uid();

const year = svg
.selectAll("g")
.data(dataByYears)
.join("g")
.attr("class", (d) => "y" + d.year);

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, 50))
.join("stop")
.attr("offset", (d) => d)
.attr("stop-color", color.interpolator());

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

year
.append("rect")
.attr("x", (d) => x(d.year))
.attr("y", (d) => y(d.max))
.attr("width", barWidth)
.attr("height", (d) => y(d.min) - y(d.max))
.attr("class", (d) => "bar " + "bar" + d.year)
.attr("fill", gradient)
.attr("stroke", "#fff")
.attr("stroke-width", 0.5)
.on("mouseover", function (i, d) {
d3.select(this.parentNode).raise();
d3.selectAll(".x-axis, .tickmarks").raise();

svg
.append("text")
.attr("class", "value")
.attr("x", x(d.year) + 10)
.attr("y", y(5))
.text(d.max + "°C");

svg
.append("text")
.attr("class", "value")
.attr("x", x(d.year) + 10)
.attr("y", y(-5))
.text(d.min + "°C");
})
.on("mouseout", (d) => {
d3.selectAll(".value").remove();
});

svg
.append("line")
.attr("x1", x(d3.min(dataByYears, (d) => d.year)))
.attr("x2", x(d3.max(dataByYears, (d) => d.year)) + barWidth)
.attr("y1", y(0))
.attr("y2", y(0))
.attr("class", "x-axis")
.attr("stroke", "#333")
.attr("stroke-width", 1);

const ticks = svg
.append("g")
.attr("class", "tickmarks")
.selectAll("g")
.data(dataByYears.filter((d, i) => i % 10 === 0))
.join("g");

ticks
.append("line")
.attr("x1", (d) => x(d.year) + barWidth / 2)
.attr("x2", (d) => x(d.year) + barWidth / 2)
.attr("y1", y(0.5))
.attr("y2", y(-0.5))
.attr("stroke", "#333");

ticks
.append("text")
.attr("x", (d) => x(d.year) + barWidth / 2)
.attr("y", y(1))
.attr("text-anchor", "middle")
.text((d) => d.year);

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
parseDate = d3.utcParse("%Y-%m-%d")
Insert cell
csv = FileAttachment("weather_kyiv.csv").csv()
Insert cell
Insert cell
Insert cell
d3 = require("d3@7.8.0")
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