Skip to content

Commit 913629f

Browse files
mbostockFil
andauthored
default diverging based on scheme (#845)
* default diverging based on scheme * document * typo * Update README Co-authored-by: Philippe Rivière <[email protected]>
1 parent 9c6197e commit 913629f

14 files changed

+31
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ The following diverging scale schemes are supported:
371371
* <sub><img src="./img/burd.png" width="32" height="16" alt="burd"></sub> *burd*
372372
* <sub><img src="./img/buylrd.png" width="32" height="16" alt="buylrd"></sub> *buylrd*
373373

374-
Diverging color scales support a *scale*.**pivot** option, which defaults to zero. Values below the pivot will use the lower half of the color scheme (*e.g.*, reds for the *rdgy* scheme), while values above the pivot will use the upper half (grays for *rdgy*).
374+
Picking a diverging color scheme name defaults the scale type to *diverging*; set the scale type to *linear* to treat the color scheme as sequential instead. Diverging color scales support a *scale*.**pivot** option, which defaults to zero. Values below the pivot will use the lower half of the color scheme (*e.g.*, reds for the *rdgy* scheme), while values above the pivot will use the upper half (grays for *rdgy*).
375375

376376
The following cylical color schemes are supported:
377377

src/scales.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {isColor, isEvery, isOrdinal, isFirst, isTemporal, isTemporalString, isNu
33
import {registry, color, position, radius, opacity, symbol, length} from "./scales/index.js";
44
import {ScaleLinear, ScaleSqrt, ScalePow, ScaleLog, ScaleSymlog, ScaleQuantile, ScaleQuantize, ScaleThreshold, ScaleIdentity} from "./scales/quantitative.js";
55
import {ScaleDiverging, ScaleDivergingSqrt, ScaleDivergingPow, ScaleDivergingLog, ScaleDivergingSymlog} from "./scales/diverging.js";
6+
import {isDivergingScheme} from "./scales/schemes.js";
67
import {ScaleTime, ScaleUtc} from "./scales/temporal.js";
78
import {ScaleOrdinal, ScalePoint, ScaleBand, ordinalImplicit} from "./scales/ordinal.js";
89
import {isSymbol, maybeSymbol} from "./symbols.js";
@@ -212,7 +213,7 @@ function formatScaleType(type) {
212213
return typeof type === "symbol" ? type.description : type;
213214
}
214215

215-
function inferScaleType(key, channels, {type, domain, range, scheme}) {
216+
function inferScaleType(key, channels, {type, domain, range, scheme, pivot}) {
216217
// The facet scales are always band scales; this cannot be changed.
217218
if (key === "fx" || key === "fy") return "band";
218219

@@ -261,18 +262,20 @@ function inferScaleType(key, channels, {type, domain, range, scheme}) {
261262
// Otherwise, infer the scale type from the data! Prefer the domain, if
262263
// present, over channels. (The domain and channels should be consistently
263264
// typed, and the domain is more explicit and typically much smaller.) We only
264-
// check the first defined value for expedience and simplicitly; we expect
265+
// check the first defined value for expedience and simplicity; we expect
265266
// that the types are consistent.
266267
if (domain !== undefined) {
267268
if (isOrdinal(domain)) return asOrdinalType(kind);
268269
if (isTemporal(domain)) return "utc";
270+
if (kind === color && (pivot != null || isDivergingScheme(scheme))) return "diverging";
269271
return "linear";
270272
}
271273

272274
// If any channel is ordinal or temporal, it takes priority.
273275
const values = channels.map(({value}) => value).filter(value => value !== undefined);
274276
if (values.some(isOrdinal)) return asOrdinalType(kind);
275277
if (values.some(isTemporal)) return "utc";
278+
if (kind === color && (pivot != null || isDivergingScheme(scheme))) return "diverging";
276279
return "linear";
277280
}
278281

src/scales/schemes.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,21 @@ export function quantitativeScheme(scheme) {
258258
if (!quantitativeSchemes.has(s)) throw new Error(`unknown scheme: ${s}`);
259259
return quantitativeSchemes.get(s);
260260
}
261+
262+
const divergingSchemes = new Set([
263+
"brbg",
264+
"prgn",
265+
"piyg",
266+
"puor",
267+
"rdbu",
268+
"rdgy",
269+
"rdylbu",
270+
"rdylgn",
271+
"spectral",
272+
"burd",
273+
"buylrd"
274+
]);
275+
276+
export function isDivergingScheme(scheme) {
277+
return scheme != null && divergingSchemes.has(`${scheme}`.toLowerCase());
278+
}

test/plots/gistemp-anomaly-moving.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export default async function() {
1010
grid: true
1111
},
1212
color: {
13-
type: "diverging",
1413
scheme: "BuRd",
1514
symmetric: false
1615
},

test/plots/gistemp-anomaly-transform.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default async function() {
1212
grid: true
1313
},
1414
color: {
15-
type: "diverging",
1615
scheme: "BuRd",
1716
domain: [-1, 1],
1817
transform

test/plots/gistemp-anomaly.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ export default async function() {
1010
grid: true
1111
},
1212
color: {
13-
type: "diverging",
14-
reverse: true
13+
scheme: "BuRd"
1514
},
1615
marks: [
1716
Plot.ruleY([0]),

test/plots/hadcrut-warming-stripes.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export default async function() {
1515
round: true
1616
},
1717
color: {
18-
type: "diverging",
1918
scheme: "BuRd",
2019
symmetric: false
2120
},

test/plots/legend-color.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ export function colorLegendImplicitLabel() {
273273
export function colorLegendDiverging() {
274274
return Plot.legend({
275275
color: {
276-
type: "diverging",
277276
domain: [-0.1, 0.1],
278277
scheme: "PiYG",
279278
label: "Daily change"
@@ -285,7 +284,6 @@ export function colorLegendDiverging() {
285284
export function colorLegendDivergingPivot() {
286285
return Plot.legend({
287286
color: {
288-
type: "diverging",
289287
domain: [1, 4],
290288
pivot: 3,
291289
scheme: "PiYG"
@@ -296,7 +294,6 @@ export function colorLegendDivergingPivot() {
296294
export function colorLegendDivergingPivotAsymmetric() {
297295
return Plot.legend({
298296
color: {
299-
type: "diverging",
300297
symmetric: false,
301298
domain: [1, 4],
302299
pivot: 3,

test/plots/metro-inequality-change.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ export default async function() {
1414
label: "↑ Inequality"
1515
},
1616
color: {
17-
type: "diverging",
18-
reverse: true,
17+
scheme: "BuRd",
1918
symmetric: false
2019
},
2120
marks: [

test/plots/metro-unemployment-slope.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export default async function() {
88
grid: true
99
},
1010
color: {
11-
type: "diverging",
1211
scheme: "buylrd",
1312
domain: [-0.5, 0.5]
1413
},

0 commit comments

Comments
 (0)