Public
Edited
Apr 17, 2023
Insert cell
Insert cell
Insert cell
chart = {
// 创建svg标签对象
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

// 建立x坐标轴
// xAxis是一个函数
svg.append("g")
.call(xAxis);

// 建立y坐标轴
// 与x同理
svg.append("g")
.call(yAxis);

// 建立g节点,作为rect的容器
const group = svg.append("g");

// 选中rect们
let rect = group.selectAll("rect");

// 返回 Object.assign(svg.node(), /* 函数 */)
return Object.assign(svg.node(), {
draw() {
/* TODO */

// transition函数
// 本次作业不涉及动态变换,可以先不看
const t = svg.transition()
.ease(d3.easeLinear)
.duration(delay);

// data-join结构,细理解
// TODO
rect = rect
.data(data.filter(d => d.year === 2005))
.join(
enter => enter.append("rect")
.style("mix-blend-mode", "darken")
.attr("fill", d => color(d.sex))
.attr("x", d => x(d.age))
.attr("y", d => y(d.value))
.attr("width", x.bandwidth() + 1)
.attr("height", d => y(0) - y(d.value))
// ,
// update => update,
// exit => exit.call(rect => rect.transition(t).remove()
// .attr("y", y(0))
// .attr("height", 0))
);

// y方向的transition
// rect.transition(t)
// .attr("y", d => y(d.value))
// .attr("height", d => y(0) - y(d.value));

// x方向transition
// group.transition(t)
// .attr("transform", `translate(${-dx},0)`);
}
});
}
Insert cell
// 调用update函数
chart.draw()
Insert cell
// 设置动画变换间隔 0.25s
delay = 250
Insert cell
// data赋值
// 奇怪的结构:Object.assign(d3.csvParse(...), {x: "...", y: "..."})
// 如何调用后方的x, y???
data = Object.assign(d3.csvParse(await FileAttachment("icelandic-population.csv").text(), d3.autoType), {x: "← Age", y: "Population ↑"})
Insert cell
data2 = FileAttachment("icelandic-population.csv").csv({typed: true})
Insert cell
// x轴(年份)下界
yearMin = d3.min(data, d => d.year)
Insert cell
// literally, yearStep
yearStep = 1
Insert cell
// 定义x的scale
x = d3.scaleBand()
.domain(Array.from(d3.group(data, d => d.age).keys()).sort(d3.ascending))
.range([width - margin.right, margin.left])
Insert cell
// 定义y的scale
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([height - margin.bottom, margin.top])
Insert cell
// color scale
color = d3.scaleOrdinal(["M", "F"], ["#4e79a7", "#e15759"])
Insert cell
// 定义xAxis函数
// TODO
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x)
.tickValues(d3.ticks(...d3.extent(data, d => d.age), width / 40))
.tickSizeOuter(0))
.call(g => g.append("text")
.attr("x", margin.right)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(data.x))
Insert cell
// TODO
yAxis = g => g
.attr("transform", `translate(${width - margin.right},0)`)
.call(d3.axisRight(y).ticks(null, "s"))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", margin.right)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(data.y))
Insert cell
// 初始化height值
height = 500
Insert cell
// margin
// 哪儿用到了?(
// 两个Axis函数
// 两个scale函数
margin = ({top: 20, right: 30, bottom: 34, left: 0})
Insert cell
// 制定d3版本
d3 = require("d3@6")
Insert cell
import {Scrubber} from "@mbostock/scrubber"
Insert cell
import {swatches} from "@d3/color-legend"
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