Skip to content
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
28 changes: 18 additions & 10 deletions src/scales/ordinal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {ascendingDefined} from "../defined.js";
import {maybeSymbol} from "../options.js";
import {none} from "../style.js";
import {registry, color, symbol} from "./index.js";
import {ordinalScheme, quantitativeScheme} from "./schemes.js";
import {maybeBooleanRange, ordinalScheme, quantitativeScheme} from "./schemes.js";

export function ScaleO(scale, channels, {
type,
Expand All @@ -26,26 +26,34 @@ export function ScaleO(scale, channels, {

export function ScaleOrdinal(key, channels, {
type,
domain = inferDomain(channels),
range,
scheme = range === undefined ? type === "ordinal" ? "turbo" : "tableau10" : undefined,
scheme,
unknown,
...options
}) {
let hint;
if (registry.get(key) === symbol) {
hint = inferSymbolHint(channels);
range = range === undefined ? inferSymbolRange(hint) : Array.from(range, maybeSymbol);
} else if (registry.get(key) === color && scheme !== undefined) {
if (range !== undefined) {
const interpolate = quantitativeScheme(scheme);
const t0 = range[0], d = range[1] - range[0];
range = ({length: n}) => quantize(t => interpolate(t0 + d * t), n);
} else {
range = ordinalScheme(scheme);
} else if (registry.get(key) === color) {
if (scheme === undefined
&& range === undefined
&& (range = maybeBooleanRange(domain, "greys")) === undefined) {
scheme = type === "ordinal" ? "turbo" : "tableau10";
}
if (scheme !== undefined) {
if (range !== undefined) {
const interpolate = quantitativeScheme(scheme);
const t0 = range[0], d = range[1] - range[0];
range = ({length: n}) => quantize(t => interpolate(t0 + d * t), n);
} else {
range = ordinalScheme(scheme);
}
}
}
if (unknown === scaleImplicit) throw new Error("implicit unknown is not supported");
return ScaleO(scaleOrdinal().unknown(unknown), channels, {...options, type, range, hint});
return ScaleO(scaleOrdinal().unknown(unknown), channels, {...options, type, domain, range, hint});
}

export function ScalePoint(key, channels, {
Expand Down
17 changes: 17 additions & 0 deletions src/scales/schemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ const ordinalSchemes = new Map([

function scheme9(scheme, interpolate) {
return ({length: n}) => {
if (n === 1) return [scheme[3][1]]; // favor midpoint
if (n === 2) return [scheme[3][0], scheme[3][2]]; // favor extrema
n = Math.max(3, Math.floor(n));
return n > 9 ? quantize(interpolate, n) : scheme[n];
};
Expand Down Expand Up @@ -184,6 +186,21 @@ export function ordinalRange(scheme, length) {
return r.length !== length ? r.slice(0, length) : r;
}

// If the specified domain contains only booleans (ignoring null and undefined),
// returns a corresponding range where false is mapped to the low color and true
// is mapped to the high color of the specified scheme.
export function maybeBooleanRange(domain, scheme) {
const range = new Set();
const [f, t] = ordinalScheme(scheme)({length: 2});
for (const value of domain) {
if (value == null) continue;
if (value === true) range.add(t);
else if (value === false) range.add(f);
else return;
}
return [...range];
}

const quantitativeSchemes = new Map([
// diverging
["brbg", interpolateBrBG],
Expand Down
Loading