Skip to content

descending order #1607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/channel.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {Interval} from "./interval.js";
import type {Reducer} from "./reducer.js";
import type {ScaleName, ScaleType} from "./scales.js";
import type {CompareFunction} from "./transforms/basic.js";
import type {BinOptions} from "./transforms/bin.js";

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

/** If true, use descending instead of ascending order. */
/** How to order reduced values. */
order?: CompareFunction | "ascending" | "descending" | null;

/** If true, reverse the order after sorting. */
reverse?: boolean;

/**
Expand Down
24 changes: 18 additions & 6 deletions src/channel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {InternSet, rollup, sort} from "d3";
import {InternSet, rollups} from "d3";
import {ascendingDefined, descendingDefined} from "./defined.js";
import {first, isColor, isEvery, isIterable, isOpacity, labelof, map, maybeValue, range, valueof} from "./options.js";
import {registry} from "./scales/index.js";
Expand Down Expand Up @@ -78,11 +78,11 @@ export function inferChannelScale(name, channel) {
// computed; i.e., if the scale’s domain is set explicitly, that takes priority
// over the sort option, and we don’t need to do additional work.
export function channelDomain(data, facets, channels, facetChannels, options) {
const {reverse: defaultReverse, reduce: defaultReduce = true, limit: defaultLimit} = options;
const {order: defaultOrder, reverse: defaultReverse, reduce: defaultReduce = true, limit: defaultLimit} = options;
for (const x in options) {
if (!registry.has(x)) continue; // ignore unknown scale keys (including generic options)
let {value: y, reverse = defaultReverse, reduce = defaultReduce, limit = defaultLimit} = maybeValue(options[x]);
if (reverse === undefined) reverse = y === "width" || y === "height"; // default to descending for lengths
let {value: y, order = defaultOrder, reverse = defaultReverse, reduce = defaultReduce, limit = defaultLimit} = maybeValue(options[x]); // prettier-ignore
order = order === undefined ? y === "width" || y === "height" ? descendingGroup : ascendingGroup : maybeOrder(order); // prettier-ignore
if (reduce == null || reduce === false) continue; // disabled reducer
const X = x === "fx" || x === "fy" ? reindexFacetChannel(facets, facetChannels[x]) : findScaleChannel(channels, x);
if (!X) throw new Error(`missing channel for scale: ${x}`);
Expand All @@ -106,12 +106,13 @@ export function channelDomain(data, facets, channels, facetChannels, options) {
: values(channels, y, y === "y" ? "y2" : y === "x" ? "x2" : undefined);
const reducer = maybeReduce(reduce === true ? "max" : reduce, YV);
X.domain = () => {
let domain = rollup(
let domain = rollups(
range(XV),
(I) => reducer.reduceIndex(I, YV),
(i) => XV[i]
);
domain = sort(domain, reverse ? descendingGroup : ascendingGroup);
if (order) domain.sort(order);
if (reverse) domain.reverse();
if (lo !== 0 || hi !== Infinity) domain = domain.slice(lo, hi);
return domain.map(first);
};
Expand Down Expand Up @@ -154,6 +155,17 @@ function values(channels, name, alias) {
throw new Error(`missing channel: ${name}`);
}

function maybeOrder(order) {
if (order == null || typeof order === "function") return order;
switch (`${order}`.toLowerCase()) {
case "ascending":
return ascendingGroup;
case "descending":
return descendingGroup;
}
throw new Error(`invalid order: ${order}`);
}

function ascendingGroup([ak, av], [bk, bv]) {
return ascendingDefined(av, bv) || ascendingDefined(ak, bk);
}
Expand Down
104 changes: 104 additions & 0 deletions test/output/channelDomainAscending.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 104 additions & 0 deletions test/output/channelDomainAscendingReverse.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading