@@ -15,22 +15,16 @@ function maybeScale(scale, key) {
15
15
export function legendSwatches ( color , { opacity, ...options } = { } ) {
16
16
if ( ! isOrdinalScale ( color ) && ! isThresholdScale ( color ) )
17
17
throw new Error ( `swatches legend requires ordinal or threshold color scale (not ${ color . type } )` ) ;
18
- return legendItems (
19
- color ,
20
- options ,
21
- ( selection , scale ) =>
22
- selection
23
- . append ( "svg" )
24
- . attr ( "fill" , scale . scale )
25
- . attr ( "fill-opacity" , maybeNumberChannel ( opacity ) [ 1 ] )
26
- . append ( "rect" )
27
- . attr ( "width" , "100%" )
28
- . attr ( "height" , "100%" ) ,
29
- ( className ) => `.${ className } -swatch svg {
30
- width: var(--swatchWidth);
31
- height: var(--swatchHeight);
32
- margin-right: 0.5em;
33
- }`
18
+ return legendItems ( color , options , ( selection , scale , width , height ) =>
19
+ selection
20
+ . append ( "svg" )
21
+ . attr ( "width" , width )
22
+ . attr ( "height" , height )
23
+ . attr ( "fill" , scale . scale )
24
+ . attr ( "fill-opacity" , maybeNumberChannel ( opacity ) [ 1 ] )
25
+ . append ( "rect" )
26
+ . attr ( "width" , "100%" )
27
+ . attr ( "height" , "100%" )
34
28
) ;
35
29
}
36
30
@@ -55,36 +49,27 @@ export function legendSymbols(
55
49
fillOpacity = maybeNumberChannel ( fillOpacity ) [ 1 ] ;
56
50
strokeOpacity = maybeNumberChannel ( strokeOpacity ) [ 1 ] ;
57
51
strokeWidth = maybeNumberChannel ( strokeWidth ) [ 1 ] ;
58
- return legendItems (
59
- symbol ,
60
- options ,
61
- ( selection ) =>
62
- selection
63
- . append ( "svg" )
64
- . attr ( "viewBox" , "-8 -8 16 16" )
65
- . attr ( "fill" , vf === "color" ? ( d ) => sf . scale ( d ) : null )
66
- . attr ( "stroke" , vs === "color" ? ( d ) => ss . scale ( d ) : null )
67
- . append ( "path" )
68
- . attr ( "d" , ( d ) => {
69
- const p = path ( ) ;
70
- symbol . scale ( d ) . draw ( p , size ) ;
71
- return p ;
72
- } ) ,
73
- ( className ) => `.${ className } -swatch > svg {
74
- width: var(--swatchWidth);
75
- height: var(--swatchHeight);
76
- margin-right: 0.5em;
77
- overflow: visible;
78
- fill: ${ cf } ;
79
- fill-opacity: ${ fillOpacity } ;
80
- stroke: ${ cs } ;
81
- stroke-width: ${ strokeWidth } px;
82
- stroke-opacity: ${ strokeOpacity } ;
83
- }`
52
+ return legendItems ( symbol , options , ( selection , scale , width , height ) =>
53
+ selection
54
+ . append ( "svg" )
55
+ . attr ( "viewBox" , "-8 -8 16 16" )
56
+ . attr ( "width" , width )
57
+ . attr ( "height" , height )
58
+ . attr ( "fill" , vf === "color" ? ( d ) => sf . scale ( d ) : cf )
59
+ . attr ( "fill-opacity" , fillOpacity )
60
+ . attr ( "stroke" , vs === "color" ? ( d ) => ss . scale ( d ) : cs )
61
+ . attr ( "stroke-opacity" , strokeOpacity )
62
+ . attr ( "stroke-width" , strokeWidth )
63
+ . append ( "path" )
64
+ . attr ( "d" , ( d ) => {
65
+ const p = path ( ) ;
66
+ symbol . scale ( d ) . draw ( p , size ) ;
67
+ return p ;
68
+ } )
84
69
) ;
85
70
}
86
71
87
- function legendItems ( scale , options = { } , swatch , swatchStyle ) {
72
+ function legendItems ( scale , options = { } , swatch ) {
88
73
let {
89
74
columns,
90
75
tickFormat,
@@ -102,35 +87,28 @@ function legendItems(scale, options = {}, swatch, swatchStyle) {
102
87
className = maybeClassName ( className ) ;
103
88
tickFormat = maybeAutoTickFormat ( tickFormat , scale . domain ) ;
104
89
105
- const swatches = create ( "div" , context )
106
- . attr ( "class" , className )
107
- . attr (
108
- "style" ,
109
- `
110
- --swatchWidth: ${ + swatchWidth } px;
111
- --swatchHeight: ${ + swatchHeight } px;
112
- `
113
- ) ;
90
+ const swatches = create ( "div" , context ) . attr (
91
+ "class" ,
92
+ `${ className } -swatches ${ className } -swatches-${ columns != null ? "columns" : "wrap" } `
93
+ ) ;
114
94
115
95
let extraStyle ;
116
96
117
97
if ( columns != null ) {
118
- extraStyle = `
119
- .${ className } -swatch {
120
- display: flex;
121
- align-items: center;
122
- break-inside: avoid;
123
- padding-bottom: 1px;
124
- }
125
- .${ className } -swatch::before {
126
- flex-shrink: 0;
127
- }
128
- .${ className } -label {
129
- white-space: nowrap;
130
- overflow: hidden;
131
- text-overflow: ellipsis;
132
- }
133
- ` ;
98
+ extraStyle = `.${ className } -swatches-columns .${ className } -swatch {
99
+ display: flex;
100
+ align-items: center;
101
+ break-inside: avoid;
102
+ padding-bottom: 1px;
103
+ }
104
+ .${ className } -swatches-columns .${ className } -swatch::before {
105
+ flex-shrink: 0;
106
+ }
107
+ .${ className } -swatches-columns .${ className } -swatch-label {
108
+ white-space: nowrap;
109
+ overflow: hidden;
110
+ text-overflow: ellipsis;
111
+ }` ;
134
112
135
113
swatches
136
114
. style ( "columns" , columns )
@@ -139,59 +117,52 @@ function legendItems(scale, options = {}, swatch, swatchStyle) {
139
117
. enter ( )
140
118
. append ( "div" )
141
119
. attr ( "class" , `${ className } -swatch` )
142
- . call ( swatch , scale )
120
+ . call ( swatch , scale , swatchWidth , swatchHeight )
143
121
. call ( ( item ) =>
144
- item . append ( "div" ) . attr ( "class" , `${ className } -label` ) . attr ( "title" , tickFormat ) . text ( tickFormat )
122
+ item . append ( "div" ) . attr ( "class" , `${ className } -swatch- label` ) . attr ( "title" , tickFormat ) . text ( tickFormat )
145
123
) ;
146
124
} else {
147
- extraStyle = `
148
- .${ className } {
149
- display: flex;
150
- align-items: center;
151
- min-height: 33px;
152
- flex-wrap: wrap;
153
- }
154
- .${ className } -swatch {
155
- display: inline-flex;
156
- align-items: center;
157
- margin-right: 1em;
158
- }
159
- ` ;
125
+ extraStyle = `.${ className } -swatches-wrap {
126
+ display: flex;
127
+ align-items: center;
128
+ min-height: 33px;
129
+ flex-wrap: wrap;
130
+ }
131
+ .${ className } -swatches-wrap .${ className } -swatch {
132
+ display: inline-flex;
133
+ align-items: center;
134
+ margin-right: 1em;
135
+ }` ;
160
136
161
137
swatches
162
138
. selectAll ( )
163
139
. data ( scale . domain )
164
140
. enter ( )
165
141
. append ( "span" )
166
142
. attr ( "class" , `${ className } -swatch` )
167
- . call ( swatch , scale )
143
+ . call ( swatch , scale , swatchWidth , swatchHeight )
168
144
. append ( function ( ) {
169
145
return this . ownerDocument . createTextNode ( tickFormat . apply ( this , arguments ) ) ;
170
146
} ) ;
171
147
}
172
148
173
149
return swatches
174
150
. call ( ( div ) =>
175
- div . insert ( "style" , "*" ) . text ( `
176
- .${ className } {
177
- font-family: system-ui, sans-serif;
178
- font-size: 10px;
179
- margin-bottom: 0.5em;${
180
- marginLeft === undefined
181
- ? ""
182
- : `
183
- margin-left: ${ + marginLeft } px;`
184
- } ${
185
- width === undefined
186
- ? ""
187
- : `
188
- width: ${ width } px;`
189
- }
190
- }
191
- ${ swatchStyle ( className ) }
192
- ${ extraStyle }
193
- ` )
151
+ div . insert ( "style" , "*" ) . text (
152
+ `.${ className } -swatches {
153
+ font-family: system-ui, sans-serif;
154
+ font-size: 10px;
155
+ margin-bottom: 0.5em;
156
+ }
157
+ .${ className } -swatch > svg {
158
+ margin-right: 0.5em;
159
+ overflow: visible;
160
+ }
161
+ ${ extraStyle } `
162
+ )
194
163
)
164
+ . style ( "margin-left" , marginLeft ? `${ + marginLeft } px` : null )
165
+ . style ( "width" , width === undefined ? null : `${ + width } px` )
195
166
. style ( "font-variant" , impliedString ( fontVariant , "normal" ) )
196
167
. call ( applyInlineStyles , style )
197
168
. node ( ) ;
0 commit comments