Skip to content

Commit 68f2f86

Browse files
mbostockFil
andauthored
swatches legend requires ordinal scale (#971)
* swatches legend requires ordinal scale * Update test/legends/legends-test.ts Co-authored-by: Philippe Rivière <[email protected]> Co-authored-by: Philippe Rivière <[email protected]>
1 parent 3371256 commit 68f2f86

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/legends/swatches.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {create, path} from "d3";
22
import {inferFontVariant} from "../axes.js";
33
import {maybeAutoTickFormat} from "../axis.js";
44
import {isNoneish, maybeColorChannel, maybeNumberChannel} from "../options.js";
5+
import {isOrdinalScale} from "../scales.js";
56
import {applyInlineStyles, impliedString, maybeClassName} from "../style.js";
67

78
function maybeScale(scale, key) {
@@ -12,6 +13,7 @@ function maybeScale(scale, key) {
1213
}
1314

1415
export function legendSwatches(color, options) {
16+
if (!isOrdinalScale(color)) throw new Error(`swatches legend requires ordinal color scale (not ${color.type})`);
1517
return legendItems(
1618
color,
1719
options,

test/legends/legends-test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import * as Plot from "@observablehq/plot";
22
import * as assert from "assert";
33

4-
it("Plot.legend({color: {type:'identity'}}) returns undefined", () => {
5-
const l = Plot.legend({color: {type: "identity"}});
6-
assert.strictEqual(l, undefined);
4+
it(`Plot.legend({color: {type: "identity"}}) returns undefined`, () => {
5+
assert.strictEqual(Plot.legend({color: {type: "identity"}}), undefined);
6+
});
7+
8+
it(`Plot.legend({legend: "swatches", color: {type: "<not-ordinal>"}}) throws an error`, () => {
9+
assert.throws(() => Plot.legend({legend: "swatches", color: {type: "linear"}}), /swatches legend requires ordinal color scale \(not linear\)/);
10+
assert.throws(() => Plot.legend({legend: "swatches", color: {type: "diverging"}}), /swatches legend requires ordinal color scale \(not diverging\)/);
711
});
812

913
it("Plot.legend({}) throws an error", () => {

0 commit comments

Comments
 (0)