Skip to content

Commit 168e8bf

Browse files
mbostockFil
andauthored
implicit categorical color scale (#1567)
* implicit categorical color scale * implicit categorical color scale (#1569) * add missing test result * test the scheme after the domain and channels have been checked * scheme-less asOrdinalType --------- Co-authored-by: Mike Bostock <[email protected]> --------- Co-authored-by: Philippe Rivière <[email protected]>
1 parent e9e08c2 commit 168e8bf

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed

src/scales.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
createScaleDivergingLog,
2929
createScaleDivergingSymlog
3030
} from "./scales/diverging.js";
31-
import {isDivergingScheme} from "./scales/schemes.js";
31+
import {isCategoricalScheme, isDivergingScheme} from "./scales/schemes.js";
3232
import {createScaleTime, createScaleUtc} from "./scales/temporal.js";
3333
import {createScaleOrdinal, createScalePoint, createScaleBand, ordinalImplicit} from "./scales/ordinal.js";
3434
import {maybeSymbol} from "./symbol.js";
@@ -421,15 +421,18 @@ function inferScaleType(key, channels, {type, domain, range, scheme, pivot, proj
421421
if (domain !== undefined) {
422422
if (isOrdinal(domain)) return asOrdinalType(kind);
423423
if (isTemporal(domain)) return "utc";
424-
if (kind === color && (pivot != null || isDivergingScheme(scheme))) return "diverging";
425-
return "linear";
424+
} else {
425+
const values = channels.map(({value}) => value).filter((value) => value !== undefined);
426+
if (values.some(isOrdinal)) return asOrdinalType(kind);
427+
if (values.some(isTemporal)) return "utc";
428+
}
429+
430+
// For color scales, take a hint from the color scheme and pivot option.
431+
if (kind === color) {
432+
if (pivot != null || isDivergingScheme(scheme)) return "diverging";
433+
if (isCategoricalScheme(scheme)) return "categorical";
426434
}
427435

428-
// If any channel is ordinal or temporal, it takes priority.
429-
const values = channels.map(({value}) => value).filter((value) => value !== undefined);
430-
if (values.some(isOrdinal)) return asOrdinalType(kind);
431-
if (values.some(isTemporal)) return "utc";
432-
if (kind === color && (pivot != null || isDivergingScheme(scheme))) return "diverging";
433436
return "linear";
434437
}
435438

src/scales/schemes.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ import {
7777
schemeYlOrRd
7878
} from "d3";
7979

80-
const ordinalSchemes = new Map([
81-
// categorical
80+
const categoricalSchemes = new Map([
8281
["accent", schemeAccent],
8382
["category10", schemeCategory10],
8483
["dark2", schemeDark2],
@@ -88,7 +87,15 @@ const ordinalSchemes = new Map([
8887
["set1", schemeSet1],
8988
["set2", schemeSet2],
9089
["set3", schemeSet3],
91-
["tableau10", schemeTableau10],
90+
["tableau10", schemeTableau10]
91+
]);
92+
93+
export function isCategoricalScheme(scheme) {
94+
return scheme != null && categoricalSchemes.has(`${scheme}`.toLowerCase());
95+
}
96+
97+
const ordinalSchemes = new Map([
98+
...categoricalSchemes,
9299

93100
// diverging
94101
["brbg", scheme11(schemeBrBG, interpolateBrBG)],
Lines changed: 52 additions & 0 deletions
Loading

test/plots/shorthand-cell.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
23

34
export async function shorthandCell() {
45
const matrix = [
@@ -18,3 +19,7 @@ export async function shorthandCell() {
1819
];
1920
return Plot.cell(matrix).plot();
2021
}
22+
23+
export async function shorthandCellCategorical() {
24+
return Plot.cellX(d3.range(10)).plot({color: {scheme: "Tableau10"}});
25+
}

0 commit comments

Comments
 (0)