Skip to content

Commit f1cee3d

Browse files
committed
adopt Plot.find
1 parent 0c58cf0 commit f1cee3d

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

test/output/youngAdults.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ <h3>…by age and sex. Data: Eurostat</h3>
165165
<text y="0.32em" transform="translate(40,30)">100</text>
166166
</g>
167167
</g>
168+
<g aria-label="y-axis label" text-anchor="start" transform="translate(-36.5,-26.5)">
169+
<text y="0.71em" transform="translate(40,30)">↑ OBS_VALUE</text>
170+
</g>
168171
<g aria-label="x-axis tick" transform="translate(0.5,0)">
169172
<g fill="none" stroke="currentColor" transform="translate(0,0)">
170173
<path transform="translate(48.833333333333336,400)" d="M0,0L0,6"></path>
@@ -230,7 +233,7 @@ <h3>…by age and sex. Data: Eurostat</h3>
230233
</g>
231234
</g>
232235
<g aria-label="x-axis label" text-anchor="end" transform="translate(17.5,27.5)">
233-
<text transform="translate(907,400)">year</text>
236+
<text transform="translate(907,400)">TIME_PERIOD</text>
234237
</g>
235238
<g aria-label="frame" transform="translate(0.5,0.5)">
236239
<rect fill="none" stroke="currentColor" x="40" y="30" width="159" height="370" transform="translate(0,0)"></rect>

test/plots/young-adults.ts

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,11 @@ import * as Plot from "@observablehq/plot";
22
import * as d3 from "d3";
33

44
export async function youngAdults() {
5-
const ilc_lvps08 = await d3.csv<any>("data/ilc_lvps08.csv", d3.autoType);
65
const ages = ["Y16-19", "Y20-24", "Y25-29"];
76
const geos = ["SE", "FR", "DE", "TR", "IT"];
8-
const wider = d3
9-
.flatRollup(
10-
ilc_lvps08.filter(({age, geo}) => ages.includes(age) && geos.includes(geo)),
11-
(v) =>
12-
Object.fromEntries(
13-
d3
14-
.index(
15-
v.map(({sex, OBS_VALUE}) => [sex, OBS_VALUE]),
16-
(d) => d[0]
17-
)
18-
.values()
19-
),
20-
(d) => d.geo,
21-
(d) => d.age,
22-
(d) => d.TIME_PERIOD
23-
)
24-
.map(([geo, age, year, d]) => ({geo, age, year, ...d}));
7+
const ilc_lvps08 = await d3.csv<any>("data/ilc_lvps08.csv", (d) =>
8+
ages.includes(d.age) && geos.includes(d.geo) ? d3.autoType(d) : null
9+
);
2510
return Plot.plot({
2611
title: "Share of young adults living with their parents (%)",
2712
subtitle: "…by age and sex. Data: Eurostat",
@@ -32,20 +17,28 @@ export async function youngAdults() {
3217
y: {grid: true, nice: true},
3318
marks: [
3419
Plot.frame(),
35-
Plot.differenceY(wider, {
36-
x: "year",
37-
y1: "F",
38-
y2: "M",
39-
sort: {fx: {value: "y", reduce: "mean"}},
40-
fillOpacity: 0.5,
41-
negativeFill: "grey",
42-
positiveFill: "age",
43-
fx: "geo",
44-
curve: "basis",
45-
z: (d) => `${d.age}, ${d.geo}`,
46-
channels: {M: "M", F: "F"},
47-
tip: true
48-
})
20+
Plot.differenceY(
21+
ilc_lvps08,
22+
Plot.groupX(
23+
{
24+
y1: Plot.find((d) => d.sex === "F"),
25+
y2: Plot.find((d) => d.sex === "M"),
26+
positiveFill: "first"
27+
},
28+
{
29+
x: "TIME_PERIOD",
30+
y: "OBS_VALUE",
31+
negativeFill: "grey",
32+
positiveFill: "age",
33+
z: "age",
34+
sort: {fx: {value: "y", reduce: "mean"}},
35+
fillOpacity: 0.5,
36+
fx: "geo",
37+
curve: "basis",
38+
tip: true
39+
}
40+
)
41+
)
4942
]
5043
});
5144
}

0 commit comments

Comments
 (0)