Skip to content

Commit 6b6c813

Browse files
Filmbostock
andauthored
Fix the parcoords chart. (#1581)
* Fix the parcoords chart. We must apply the normalization over the _whole extent of the data_ in both marks (line and "axis"); the previous version was normalizing the axes on the ticks only, resulting in misalignment between the values and the ticks. * avoid the normalize transform * minimize diff * fix test snapshot --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 5850da0 commit 6b6c813

File tree

2 files changed

+69
-50
lines changed

2 files changed

+69
-50
lines changed

test/output/carsParcoords.svg

Lines changed: 40 additions & 40 deletions
Loading

test/plots/cars-parcoords.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,27 @@ export async function carsParcoords() {
66
const dimensions = cars.columns.slice(1);
77

88
// Reshape wide data to make it tidy.
9-
const data = dimensions.flatMap((dimension: string) => {
9+
const points = dimensions.flatMap((dimension) => {
1010
return cars.map(({name, year, [dimension]: value}) => {
11-
return {name: `${name}-${year}`, dimension, value};
11+
return {name, year, dimension, value};
1212
});
1313
});
1414

15+
// Compute normalization scales for each dimension.
16+
const scales = new Map(
17+
dimensions.map((dimension) => {
18+
return [dimension, d3.scaleLinear().domain(d3.extent(cars, (d) => d[dimension]))];
19+
})
20+
);
21+
1522
// Compute ticks for each dimension.
1623
const ticks = dimensions.flatMap((dimension) => {
17-
return d3.ticks(...(d3.extent(cars, (d) => d[dimension]) as [number, number]), 7).map((value) => {
18-
return {dimension, value};
19-
});
24+
return scales
25+
.get(dimension)
26+
.ticks(7)
27+
.map((value) => ({dimension, value}));
2028
});
2129

22-
// Normalize the x-position based on the extent for each dimension.
23-
const xy = Plot.normalizeX("extent", {x: "value", y: "dimension", z: "dimension"});
24-
2530
return Plot.plot({
2631
marginLeft: 104,
2732
marginRight: 20,
@@ -36,8 +41,22 @@ export async function carsParcoords() {
3641
},
3742
marks: [
3843
Plot.ruleY(dimensions),
39-
Plot.line(data, {...xy, z: "name", stroke: "#444", strokeWidth: 0.5, strokeOpacity: 0.5}),
40-
Plot.text(ticks, {...xy, text: "value", fill: "black", stroke: "white"})
44+
Plot.lineX(points, {
45+
x: (d) => scales.get(d.dimension)(d.value),
46+
y: "dimension",
47+
z: (d) => `${d.name}-${d.year}`,
48+
stroke: "#444",
49+
strokeWidth: 0.5,
50+
strokeOpacity: 0.5
51+
}),
52+
Plot.text(ticks, {
53+
x: (d) => scales.get(d.dimension)(d.value),
54+
y: "dimension",
55+
text: "value",
56+
fill: "black",
57+
stroke: "white",
58+
strokeWidth: 3
59+
})
4160
]
4261
});
4362
}

0 commit comments

Comments
 (0)