Skip to content

Commit d4a5f0b

Browse files
authored
null-safe sort (#1348)
1 parent 9cafef3 commit d4a5f0b

File tree

3 files changed

+188
-3
lines changed

3 files changed

+188
-3
lines changed

src/channel.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {ascending, descending, InternSet, rollup, sort} from "d3";
1+
import {InternSet, rollup, sort} from "d3";
2+
import {ascendingDefined, descendingDefined} from "./defined.js";
23
import {first, isColor, isEvery, isIterable, isOpacity, labelof, map, maybeValue, range, valueof} from "./options.js";
34
import {registry} from "./scales/index.js";
45
import {isSymbol, maybeSymbol} from "./symbol.js";
@@ -153,9 +154,9 @@ function values(channels, name, alias) {
153154
}
154155

155156
function ascendingGroup([ak, av], [bk, bv]) {
156-
return ascending(av, bv) || ascending(ak, bk);
157+
return ascendingDefined(av, bv) || ascendingDefined(ak, bk);
157158
}
158159

159160
function descendingGroup([ak, av], [bk, bv]) {
160-
return descending(av, bv) || ascending(ak, bk);
161+
return descendingDefined(av, bv) || ascendingDefined(ak, bk);
161162
}
Lines changed: 173 additions & 0 deletions
Loading

test/plots/athletes-sort.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,14 @@ export async function athletesSortNullLimit() {
3838
marks: [Plot.dot(athletes, {x: "height", y: "weight", stroke: "nationality", sort: {color: null, limit: 10}})]
3939
});
4040
}
41+
42+
export async function athletesSortWeightLimit() {
43+
const athletes = await d3.csv<any>("data/athletes.csv", d3.autoType);
44+
return Plot.plot({
45+
color: {legend: true},
46+
marks: [
47+
Plot.dot(athletes, {x: "weight", y: "nationality", sort: {y: "x", reduce: "median", limit: 10}}),
48+
Plot.tickX(athletes, Plot.groupY({x: "median"}, {x: "weight", y: "nationality", stroke: "red", strokeWidth: 2}))
49+
]
50+
});
51+
}

0 commit comments

Comments
 (0)