File tree Expand file tree Collapse file tree 2 files changed +9
-3
lines changed Expand file tree Collapse file tree 2 files changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import {create, path} from "d3";
2
2
import { inferFontVariant } from "../axes.js" ;
3
3
import { maybeAutoTickFormat } from "../axis.js" ;
4
4
import { isNoneish , maybeColorChannel , maybeNumberChannel } from "../options.js" ;
5
+ import { isOrdinalScale } from "../scales.js" ;
5
6
import { applyInlineStyles , impliedString , maybeClassName } from "../style.js" ;
6
7
7
8
function maybeScale ( scale , key ) {
@@ -12,6 +13,7 @@ function maybeScale(scale, key) {
12
13
}
13
14
14
15
export function legendSwatches ( color , options ) {
16
+ if ( ! isOrdinalScale ( color ) ) throw new Error ( `swatches legend requires ordinal color scale (not ${ color . type } )` ) ;
15
17
return legendItems (
16
18
color ,
17
19
options ,
Original file line number Diff line number Diff line change 1
1
import * as Plot from "@observablehq/plot" ;
2
2
import * as assert from "assert" ;
3
3
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" } } ) , / s w a t c h e s l e g e n d r e q u i r e s o r d i n a l c o l o r s c a l e \( n o t l i n e a r \) / ) ;
10
+ assert . throws ( ( ) => Plot . legend ( { legend : "swatches" , color : { type : "diverging" } } ) , / s w a t c h e s l e g e n d r e q u i r e s o r d i n a l c o l o r s c a l e \( n o t d i v e r g i n g \) / ) ;
7
11
} ) ;
8
12
9
13
it ( "Plot.legend({}) throws an error" , ( ) => {
You can’t perform that action at this time.
0 commit comments