Skip to content

swatches legend requires ordinal scale #971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 4, 2022
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
2 changes: 2 additions & 0 deletions src/legends/swatches.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {create, path} from "d3";
import {inferFontVariant} from "../axes.js";
import {maybeAutoTickFormat} from "../axis.js";
import {isNoneish, maybeColorChannel, maybeNumberChannel} from "../options.js";
import {isOrdinalScale} from "../scales.js";
import {applyInlineStyles, impliedString, maybeClassName} from "../style.js";

function maybeScale(scale, key) {
Expand All @@ -12,6 +13,7 @@ function maybeScale(scale, key) {
}

export function legendSwatches(color, options) {
if (!isOrdinalScale(color)) throw new Error(`swatches legend requires ordinal color scale (not ${color.type})`);
return legendItems(
color,
options,
Expand Down
10 changes: 7 additions & 3 deletions test/legends/legends-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import * as Plot from "@observablehq/plot";
import * as assert from "assert";

it("Plot.legend({color: {type:'identity'}}) returns undefined", () => {
const l = Plot.legend({color: {type: "identity"}});
assert.strictEqual(l, undefined);
it(`Plot.legend({color: {type: "identity"}}) returns undefined`, () => {
assert.strictEqual(Plot.legend({color: {type: "identity"}}), undefined);
});

it(`Plot.legend({legend: "swatches", color: {type: "<not-ordinal>"}}) throws an error`, () => {
assert.throws(() => Plot.legend({legend: "swatches", color: {type: "linear"}}), /swatches legend requires ordinal color scale \(not linear\)/);
assert.throws(() => Plot.legend({legend: "swatches", color: {type: "diverging"}}), /swatches legend requires ordinal color scale \(not diverging\)/);
});

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