@@ -120,7 +120,7 @@ export class Tip extends Mark {
120
120
if ( "title" in sources ) {
121
121
const text = sources . title . value [ i ] ;
122
122
for ( const line of mark . splitLines ( formatDefault ( text ) ) ) {
123
- yield [ "" , mark . clipLine ( line ) ] ;
123
+ yield { name : "" , value : mark . clipLine ( line ) } ;
124
124
}
125
125
return ;
126
126
}
@@ -130,17 +130,19 @@ export class Tip extends Mark {
130
130
const channel = sources [ key ] ;
131
131
const value = channel . value [ i ] ;
132
132
if ( ! defined ( value ) && channel . scale == null ) continue ;
133
- const color = channel . scale === "color" ? values [ key ] [ i ] : undefined ;
134
133
if ( key === "x2" && "x1" in sources ) {
135
- yield [ formatLabel ( scales , channel , "x" ) , formatPair ( sources . x1 , channel , i ) ] ;
134
+ yield { name : formatLabel ( scales , channel , "x" ) , value : formatPair ( sources . x1 , channel , i ) } ;
136
135
} else if ( key === "y2" && "y1" in sources ) {
137
- yield [ formatLabel ( scales , channel , "y" ) , formatPair ( sources . y1 , channel , i ) ] ;
136
+ yield { name : formatLabel ( scales , channel , "y" ) , value : formatPair ( sources . y1 , channel , i ) } ;
138
137
} else {
139
- yield [ formatLabel ( scales , channel , key ) , formatDefault ( value ) , color ] ;
138
+ const scale = channel . scale ;
139
+ const line = { name : formatLabel ( scales , channel , key ) , value : formatDefault ( value ) } ;
140
+ if ( scale === "color" || scale === "opacity" ) line [ scale ] = values [ key ] [ i ] ;
141
+ yield line ;
140
142
}
141
143
}
142
- if ( index . fi != null && fx ) yield [ String ( fx . label ?? "fx" ) , formatFx ( index . fx ) ] ;
143
- if ( index . fi != null && fy ) yield [ String ( fy . label ?? "fy" ) , formatFy ( index . fy ) ] ;
144
+ if ( index . fi != null && fx ) yield { name : String ( fx . label ?? "fx" ) , value : formatFx ( index . fx ) } ;
145
+ if ( index . fi != null && fy ) yield { name : String ( fy . label ?? "fy" ) , value : formatFy ( index . fy ) } ;
144
146
}
145
147
146
148
// We don’t call applyChannelStyles because we only use the channels to
@@ -167,10 +169,11 @@ export class Tip extends Mark {
167
169
this . setAttribute ( "stroke" , "none" ) ;
168
170
// iteratively render each channel value
169
171
const names = new Set ( ) ;
170
- for ( const [ name , value , color ] of format ( sources , i ) ) {
172
+ for ( const line of format ( sources , i ) ) {
173
+ const name = line . name ;
171
174
if ( name && names . has ( name ) ) continue ;
172
175
else names . add ( name ) ;
173
- renderLine ( that , name , value , color ) ;
176
+ renderLine ( that , line ) ;
174
177
}
175
178
} )
176
179
)
@@ -181,7 +184,8 @@ export class Tip extends Mark {
181
184
// just the initial layout of the text; in postrender we will compute the
182
185
// exact text metrics and translate the text as needed once we know the
183
186
// tip’s orientation (anchor).
184
- function renderLine ( selection , name , value , color ) {
187
+ function renderLine ( selection , { name, value, color, opacity} ) {
188
+ const swatch = color != null || opacity != null ;
185
189
let title ;
186
190
let w = lineWidth * 100 ;
187
191
const [ j ] = cut ( name , w , widthof , ee ) ;
@@ -191,7 +195,7 @@ export class Tip extends Mark {
191
195
title = value . trim ( ) ;
192
196
value = "" ;
193
197
} else {
194
- if ( name || ( ! value && ! color ) ) value = " " + value ;
198
+ if ( name || ( ! value && ! swatch ) ) value = " " + value ;
195
199
const [ k ] = cut ( value , w - widthof ( name ) , widthof , ee ) ;
196
200
if ( k >= 0 ) {
197
201
// value is truncated
@@ -202,7 +206,7 @@ export class Tip extends Mark {
202
206
const line = selection . append ( "tspan" ) . attr ( "x" , 0 ) . attr ( "dy" , `${ lineHeight } em` ) . text ( "\u200b" ) ; // zwsp for double-click
203
207
if ( name ) line . append ( "tspan" ) . attr ( "font-weight" , "bold" ) . text ( name ) ;
204
208
if ( value ) line . append ( ( ) => document . createTextNode ( value ) ) ;
205
- if ( color ) line . append ( "tspan" ) . text ( " ■" ) . attr ( "fill" , color ) . style ( "user-select" , "none" ) ;
209
+ if ( swatch ) line . append ( "tspan" ) . text ( " ■" ) . attr ( "fill" , color ) . attr ( "fill-opacity" , opacity ) . style ( "user-select" , "none" ) ; // prettier-ignore
206
210
if ( title ) line . append ( "title" ) . text ( title ) ;
207
211
}
208
212
0 commit comments