Skip to content

Commit ff386e0

Browse files
committed
opt-out of boolean range; custom scheme
1 parent 4f4c6b6 commit ff386e0

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/scales.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {registry, color, position, radius, opacity, symbol, length} from "./scal
44
import {ScaleLinear, ScaleSqrt, ScalePow, ScaleLog, ScaleSymlog, ScaleQuantile, ScaleThreshold, ScaleIdentity} from "./scales/quantitative.js";
55
import {ScaleDiverging, ScaleDivergingSqrt, ScaleDivergingPow, ScaleDivergingLog, ScaleDivergingSymlog} from "./scales/diverging.js";
66
import {ScaleTime, ScaleUtc} from "./scales/temporal.js";
7-
import {ScaleOrdinal, ScalePoint, ScaleBand} from "./scales/ordinal.js";
7+
import {ScaleOrdinal, ScalePoint, ScaleBand, ordinalImplicit} from "./scales/ordinal.js";
88

99
export function Scales(channels, {
1010
inset: globalInset = 0,
@@ -172,7 +172,7 @@ function Scale(key, channels = [], options = {}) {
172172
case "diverging-pow": return ScaleDivergingPow(key, channels, options);
173173
case "diverging-log": return ScaleDivergingLog(key, channels, options);
174174
case "diverging-symlog": return ScaleDivergingSymlog(key, channels, options);
175-
case "categorical": case "ordinal": return ScaleOrdinal(key, channels, options);
175+
case "categorical": case "ordinal": case ordinalImplicit: return ScaleOrdinal(key, channels, options);
176176
case "cyclical": case "sequential": case "linear": return ScaleLinear(key, channels, options);
177177
case "sqrt": return ScaleSqrt(key, channels, options);
178178
case "threshold": return ScaleThreshold(key, channels, options);
@@ -255,7 +255,7 @@ function inferScaleType(key, channels, {type, domain, range, scheme}) {
255255
function asOrdinalType(kind) {
256256
switch (kind) {
257257
case position: return "point";
258-
case color: return "categorical";
258+
case color: return ordinalImplicit;
259259
default: return "ordinal";
260260
}
261261
}

src/scales/ordinal.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ import {none} from "../style.js";
66
import {registry, color, symbol} from "./index.js";
77
import {maybeBooleanRange, ordinalScheme, quantitativeScheme} from "./schemes.js";
88

9+
// This denotes an implicitly ordinal color scale: the scale type was not set,
10+
// but the associated values are strings or booleans. If the associated defined
11+
// values are entirely boolean, the range will default to greys. You can opt out
12+
// of this by setting the type explicitly.
13+
export const ordinalImplicit = Symbol("ordinal");
14+
915
export function ScaleO(scale, channels, {
1016
type,
1117
domain = inferDomain(channels),
1218
range,
1319
reverse,
1420
hint
1521
}) {
16-
if (type === "categorical") type = "ordinal"; // shorthand for color schemes
22+
if (type === "categorical" || type === ordinalImplicit) type = "ordinal"; // shorthand for color schemes
1723
if (reverse) domain = reverseof(domain);
1824
scale.domain(domain);
1925
if (range !== undefined) {
@@ -37,9 +43,11 @@ export function ScaleOrdinal(key, channels, {
3743
hint = inferSymbolHint(channels);
3844
range = range === undefined ? inferSymbolRange(hint) : Array.from(range, maybeSymbol);
3945
} else if (registry.get(key) === color) {
40-
if (scheme === undefined
41-
&& range === undefined
42-
&& (range = maybeBooleanRange(domain, "greys")) === undefined) {
46+
if (range === undefined && (type === "ordinal" || type === ordinalImplicit)) {
47+
range = maybeBooleanRange(domain, scheme === undefined ? "greys" : scheme);
48+
if (range !== undefined) scheme = undefined; // Don’t re-apply scheme.
49+
}
50+
if (scheme === undefined && range === undefined) {
4351
scheme = type === "ordinal" ? "turbo" : "tableau10";
4452
}
4553
if (scheme !== undefined) {

0 commit comments

Comments
 (0)