Public
Edited
May 26, 2023
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
populationByPrefecture
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
addTooltips(
Plot.plot({
y: {
grid: true,
tickFormat: "s"
},
color: {scheme: "spectral", domain: genders, legend: true},
marks: [
Plot.barY(populationByPrefectureGender, {
x: "district",
y: "population",
fill: "gender",
title: "population",
sort: {x: "y", reverse: true}
}),
Plot.ruleY([0])
]
})
)
Insert cell
chart = PieChart(populationByPrefecture, {
name: d => d["地区"],
value: d => d["合计"],
})
Insert cell
addTooltips(
Plot.plot({
projection: {
type: "albers",
rotate: [-110, 0],
domain: prefectures
},
marks: [
Plot.geo(prefectures, {
fill: p => prefectureToPopulation.get(p.properties.fullname),
title: p => `${p.properties.name} \n ${prefectureToPopulation.get(p.properties.fullname).toLocaleString()}`
})
],
margin: 20,
color: {
scheme: "reds", // Change color scheme
unknown: "#ddd", // Polygons with unknown values are gray
type: "linear", // Linear scale for color progression
legend: true, // Add the legend
tickFormat: "s",
label: "Population", // Update legend label
}
})
)
Insert cell
addTooltips(
Plot.plot({
y: {
grid: true,
tickFormat: d => d + 1,
label: "↑ Male/Female Ratio"
},
color: {scheme: "PiYg"},
marks: [
Plot.barY(populationByPrefecture, {
x: "地区",
y: d => d["性别比"] - 1,
fill: d => Math.sign(d["性别比"] - 1),
title: "性别比",
sort: {x: "y", reverse: true},
}),
Plot.ruleY([0])
]
})
)
Insert cell
Insert cell
Insert cell
import {addTooltips} from "@mkfreeman/plot-tooltip"
Insert cell
import {PieChart} from "@d3/pie-chart"
Insert cell
workbook = FileAttachment("人口普查.xlsx").xlsx()
Insert cell
populationByPrefecture = workbook.sheet("地级", { headers: true })
Insert cell
populationByPrefecture[14].地区 = "沈抚新区"
Insert cell
genders = ["男", "女"]
Insert cell
populationByPrefectureGender = genders.flatMap(gender => populationByPrefecture.map(d => ({district: d.地区, gender, population: d[gender]})))
Insert cell
prefectureToPopulation = new Map(populationByPrefecture.map(({地区, 合计}) => [地区, 合计]))
Insert cell
liaoning = d3.json("https://geojson.cn/api/data/210000.topo.json")
Insert cell
prefectures = topojson.feature(liaoning, liaoning.objects.default)
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