Skip to content

Commit 404db45

Browse files
committed
descending order
1 parent 0490ba6 commit 404db45

File tree

7 files changed

+33
-12
lines changed

7 files changed

+33
-12
lines changed

src/channel.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {Interval} from "./interval.js";
22
import type {Reducer} from "./reducer.js";
33
import type {ScaleName, ScaleType} from "./scales.js";
4+
import type {CompareFunction} from "./transforms/basic.js";
45
import type {BinOptions} from "./transforms/bin.js";
56

67
/** Lazily-constructed channel values derived from data. */
@@ -192,7 +193,10 @@ export interface ChannelDomainOptions {
192193
*/
193194
reduce?: Reducer | boolean | null;
194195

195-
/** If true, use descending instead of ascending order. */
196+
/** How to order reduces values. */
197+
order?: CompareFunction | "ascending" | "descending" | null;
198+
199+
/** If true, reverse the order after sorting. */
196200
reverse?: boolean;
197201

198202
/**

src/channel.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {InternSet, rollup, sort} from "d3";
1+
import {InternSet, rollups} from "d3";
22
import {ascendingDefined, descendingDefined} from "./defined.js";
33
import {first, isColor, isEvery, isIterable, isOpacity, labelof, map, maybeValue, range, valueof} from "./options.js";
44
import {registry} from "./scales/index.js";
@@ -78,11 +78,16 @@ export function inferChannelScale(name, channel) {
7878
// computed; i.e., if the scale’s domain is set explicitly, that takes priority
7979
// over the sort option, and we don’t need to do additional work.
8080
export function channelDomain(data, facets, channels, facetChannels, options) {
81-
const {reverse: defaultReverse, reduce: defaultReduce = true, limit: defaultLimit} = options;
8281
for (const x in options) {
8382
if (!registry.has(x)) continue; // ignore unknown scale keys (including generic options)
84-
let {value: y, reverse = defaultReverse, reduce = defaultReduce, limit = defaultLimit} = maybeValue(options[x]);
85-
if (reverse === undefined) reverse = y === "width" || y === "height"; // default to descending for lengths
83+
let {
84+
value: y,
85+
order = options.order,
86+
reverse = options.reverse,
87+
reduce = options.reduce === undefined ? true : options.reduce,
88+
limit = options.limit
89+
} = maybeValue(options[x]);
90+
order = order === undefined ? y === "width" || y === "height" ? descendingGroup : ascendingGroup : maybeOrder(order); // prettier-ignore
8691
if (reduce == null || reduce === false) continue; // disabled reducer
8792
const X = x === "fx" || x === "fy" ? reindexFacetChannel(facets, facetChannels[x]) : findScaleChannel(channels, x);
8893
if (!X) throw new Error(`missing channel for scale: ${x}`);
@@ -106,12 +111,13 @@ export function channelDomain(data, facets, channels, facetChannels, options) {
106111
: values(channels, y, y === "y" ? "y2" : y === "x" ? "x2" : undefined);
107112
const reducer = maybeReduce(reduce === true ? "max" : reduce, YV);
108113
X.domain = () => {
109-
let domain = rollup(
114+
let domain = rollups(
110115
range(XV),
111116
(I) => reducer.reduceIndex(I, YV),
112117
(i) => XV[i]
113118
);
114-
domain = sort(domain, reverse ? descendingGroup : ascendingGroup);
119+
if (order) domain.sort(order);
120+
if (reverse) domain.reverse();
115121
if (lo !== 0 || hi !== Infinity) domain = domain.slice(lo, hi);
116122
return domain.map(first);
117123
};
@@ -154,6 +160,17 @@ function values(channels, name, alias) {
154160
throw new Error(`missing channel: ${name}`);
155161
}
156162

163+
function maybeOrder(order) {
164+
if (typeof order === "function") return order;
165+
switch (`${order}`.toLowerCase()) {
166+
case "ascending":
167+
return ascendingGroup;
168+
case "descending":
169+
return descendingGroup;
170+
}
171+
throw new Error(`invalid order: ${order}`);
172+
}
173+
157174
function ascendingGroup([ak, av], [bk, bv]) {
158175
return ascendingDefined(av, bv) || ascendingDefined(ak, bk);
159176
}

test/plots/athletes-nationality.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export async function athletesNationality() {
1111
label: null
1212
},
1313
marks: [
14-
Plot.barX(athletes, Plot.groupY({x: "count"}, {y: "nationality", sort: {y: "x", reverse: true, limit: 20}}))
14+
Plot.barX(athletes, Plot.groupY({x: "count"}, {y: "nationality", sort: {y: "x", order: "descending", limit: 20}}))
1515
]
1616
});
1717
}

test/plots/d3-survey-2015.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function bars(groups, title) {
4343
y: ([key]) => key,
4444
fill: "steelblue",
4545
insetTop: 1,
46-
sort: {y: "x", reverse: true}
46+
sort: {y: "x", order: "descending"}
4747
}),
4848
Plot.ruleX([0])
4949
]

test/plots/learning-poverty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function learningPoverty() {
3030
x: (d) => (d.type === "ok" ? -1 : 1) * d.share, // diverging bars
3131
y: "Country Name",
3232
fill: "type",
33-
sort: {y: "x", reverse: true}
33+
sort: {y: "x", order: "descending"}
3434
}),
3535
Plot.ruleX([0])
3636
]

test/plots/metro-unemployment-ridgeline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function metroUnemploymentRidgeline() {
2121
},
2222
marks: [
2323
Plot.areaY(data, {x: "date", y: "unemployment", fill: "#eee"}),
24-
Plot.line(data, {x: "date", y: "unemployment", sort: {fy: "y", reverse: true}}),
24+
Plot.line(data, {x: "date", y: "unemployment", sort: {fy: "y", order: "descending"}}),
2525
Plot.ruleY([0])
2626
]
2727
});

test/plots/movies-rating-by-genre.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function moviesRatingByGenre() {
3737
sort: {
3838
fy: "x",
3939
reduce: "median",
40-
reverse: true
40+
order: "descending"
4141
}
4242
}
4343
)

0 commit comments

Comments
 (0)