Published
Edited
Nov 21, 2021
1 fork
Insert cell
# 1.First blood
Insert cell
md`# 全球温度变化趋势, 时间跨度:1880-2021, 包括拟合曲线


下面的图表显示了温度与1951-1980年平均值的偏差。


数据来源: [NASA Goddard Institute for Space Studies](https://data.giss.nasa.gov/gistemp/)`
Insert cell
md`## 制作意义
目前,全球变暖已经是一种趋势,这次可视化能让我们更加直观的看出全球这几十年的温度变化情况,能够激起我们更加爱护地球的做法,平时更加注意到要节约用水、用电等小事,让我们的地球变得更加美好。`
Insert cell
下面是交互界面,我们可以通过控制bandwidth来控制拟合曲线实现交互效果
Insert cell
viewof bandwidth = Inputs.range([0, 1], {
value: .15,
label: "bandwidth",
step: .01
});
Insert cell
{
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

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

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

svg.append("g")
.attr("stroke", "none")
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => x(d.date))
.attr("cy", d => y(d.value))
.attr("fill", d => z(d.value))
.attr("r", 2.5);
svg.append("path")
.datum(regression(data))
.style("fill", "none")
.style("stroke", "gray")
.attr("d", line);

return svg.node();
}
Insert cell
md`### 来源和数据`
Insert cell
md`### 布局`
Insert cell
height = 600
Insert cell
margin = ({top: 5, right: 5, bottom: 20, left: 30})
Insert cell
md`### Data`
Insert cell
FileAttachment("temperatures-1.csv").csv()
Insert cell
data = {
const data = [];
// https://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.csv
await d3.csvParse(await FileAttachment("temperatures-1.csv").text(), (d, i, columns) => {
for (let i = 1; i < 13; ++i) {
const value = +d[columns[i]];
if (value){
data.push({
date: new Date(d.Year, i - 1, 1),
value
});
}
}
});
return data;
}
Insert cell
md`### 图形 & 功能`
Insert cell
regression = d3.regressionLoess()
.x(d => d.date)
.y(d => d.value)
.bandwidth(bandwidth);
Insert cell
line = d3.line()
.x(d => x(d[0]))
.y(d => y(d[1]));
Insert cell
x = d3.scaleTime()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data, d => d.value)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
z = {
const max = d3.max(data, d => Math.abs(d.value));
return d3.scaleSequential(d3.interpolateRdBu).domain([max, -max]);
}
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, "+"))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line")
.filter(d => d === 0)
.clone()
.attr("x2", width - margin.right - margin.left)
.attr("stroke", "#ccc"))
.call(g => g.append("text")
.attr("fill", "#000")
.attr("x", 5)
.attr("y", margin.top)
.attr("dy", "0.32em")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text("Anomaly (°C)"))
Insert cell
d3 = require("d3@7", "d3-regression@1")
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