Skip to content

Commit 7289b58

Browse files
Filmbostock
andauthored
test plots (#546)
* replace proportion plot by "who owns Britain" * tweak example Co-authored-by: Mike Bostock <[email protected]>
1 parent bc303d5 commit 7289b58

10 files changed

+129
-75
lines changed

test/data/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,9 @@ https://www.census.gov/retail/mrts/historic_releases.html
101101
## us-state-population-2010-2019.csv
102102
U.S. Census Bureau (?)
103103
https://observablehq.com/@d3/diverging-bar-chart
104+
105+
## wealth-britain.csv
106+
U.K. Office for National Statistics
107+
A recreation of “Who owns Britain?” by Richard Speigal; proportion plot chart type by Stephanie Evergreen
108+
https://www.linkedin.com/feed/update/urn:li:activity:6758306750174138368/
109+
https://stephanieevergreen.com/proportion-plots/

test/data/police-deaths.csv

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/data/wealth-britain.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
age,population,wealth
2+
16-34’s,30,3
3+
35-54’s,33,32
4+
55-74’s,28,52
5+
Over 75’s,9,13

test/output/policeDeathsBar.svg renamed to test/output/wealthBritainBar.svg

Lines changed: 6 additions & 6 deletions
Loading
Lines changed: 32 additions & 0 deletions
Loading

test/plots/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ export {default as penguinSpeciesIsland} from "./penguin-species-island.js";
9090
export {default as penguinSpeciesIslandRelative} from "./penguin-species-island-relative.js";
9191
export {default as penguinSpeciesIslandSex} from "./penguin-species-island-sex.js";
9292
export {default as polylinear} from "./polylinear.js";
93-
export {default as policeDeaths} from "./police-deaths.js";
94-
export {default as policeDeathsBar} from "./police-deaths-bar.js";
9593
export {default as randomBins} from "./random-bins.js";
9694
export {default as randomBinsXY} from "./random-bins-xy.js";
9795
export {default as randomQuantile} from "./random-quantile.js";
@@ -126,6 +124,8 @@ export {default as usPresidentialElection2020} from "./us-presidential-election-
126124
export {default as usPresidentialForecast2016} from "./us-presidential-forecast-2016.js";
127125
export {default as usRetailSales} from "./us-retail-sales.js";
128126
export {default as usStatePopulationChange} from "./us-state-population-change.js";
127+
export {default as wealthBritainBar} from "./wealth-britain-bar.js";
128+
export {default as wealthBritainProportionPlot} from "./wealth-britain-proportion-plot.js";
129129
export {default as wordCloud} from "./word-cloud.js";
130130
export {default as wordLengthMobyDick} from "./word-length-moby-dick.js";
131131

test/plots/police-deaths-bar.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/plots/police-deaths.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

test/plots/wealth-britain-bar.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
export default async function() {
5+
const data = await d3.csv("data/wealth-britain.csv", d3.autoType);
6+
return Plot.plot({
7+
marks: [
8+
Plot.barX(data, Plot.stackX({x: "wealth", fill: "age"})),
9+
Plot.textX(data, Plot.stackX({x: "wealth", text: "age"})),
10+
Plot.ruleX([0, 100])
11+
]
12+
});
13+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
export default async function() {
5+
const wide = await d3.csv("data/wealth-britain.csv", d3.autoType);
6+
const columns = wide.columns.slice(1);
7+
const data = columns.flatMap(type => wide.map(d => ({age: d.age, type, value: d[type]})));
8+
const stack = options => Plot.stackY({}, {x: "type", y: "value", z: "age", ...options});
9+
return Plot.plot({
10+
x: {
11+
domain: columns,
12+
axis: "top",
13+
label: null,
14+
tickFormat: d => `Share of ${d}`,
15+
tickSize: 0,
16+
padding: 0 // see margins
17+
},
18+
y: {
19+
axis: null,
20+
reverse: true
21+
},
22+
color: {
23+
scheme: "prgn",
24+
reverse: true
25+
},
26+
marginLeft: 50,
27+
marginRight: 60,
28+
marks: [
29+
Plot.areaY(data, stack({
30+
curve: "bump-x",
31+
fill: "age",
32+
stroke: "white"
33+
})),
34+
Plot.text(
35+
data,
36+
stack({
37+
filter: d => d.type === "population",
38+
text: d => `${d.value}%`,
39+
textAnchor: "end",
40+
dx: -6
41+
})
42+
),
43+
Plot.text(
44+
data,
45+
stack({
46+
filter: d => d.type === "wealth",
47+
text: d => `${d.value}%`,
48+
textAnchor: "start",
49+
dx: +6
50+
})
51+
),
52+
Plot.text(
53+
data,
54+
stack({
55+
filter: d => d.type === "population",
56+
text: "age",
57+
textAnchor: "start",
58+
fill: "white",
59+
fontWeight: "bold",
60+
dx: +8
61+
})
62+
)
63+
]
64+
});
65+
}

0 commit comments

Comments
 (0)