@@ -5,9 +5,9 @@ import {defined} from "../defined.js";
5
5
import { formatDefault } from "../format.js" ;
6
6
import { anchorX , anchorY } from "../interactions/pointer.js" ;
7
7
import { Mark } from "../mark.js" ;
8
- import { maybeAnchor , maybeFrameAnchor , maybeTuple , number , string } from "../options.js" ;
8
+ import { maybeAnchor , maybeFrameAnchor , maybeFunction , maybeTuple , number , string } from "../options.js" ;
9
9
import { applyDirectStyles , applyFrameAnchor , applyIndirectStyles , applyTransform , impliedString } from "../style.js" ;
10
- import { identity , isIterable , isTextual } from "../options.js" ;
10
+ import { identity , isIterable , isTextual , isObject } from "../options.js" ;
11
11
import { inferTickFormat } from "./axis.js" ;
12
12
import { applyIndirectTextStyles , defaultWidth , ellipsis , monospaceWidth } from "./text.js" ;
13
13
import { cut , clipper , splitter , maybeTextOverflow } from "./text.js" ;
@@ -42,6 +42,7 @@ export class Tip extends Mark {
42
42
lineHeight = 1 ,
43
43
lineWidth = 20 ,
44
44
frameAnchor,
45
+ format,
45
46
textAnchor = "start" ,
46
47
textOverflow,
47
48
textPadding = 8 ,
@@ -82,6 +83,7 @@ export class Tip extends Mark {
82
83
for ( const key in defaults ) if ( key in this . channels ) this [ key ] = defaults [ key ] ; // apply default even if channel
83
84
this . splitLines = splitter ( this ) ;
84
85
this . clipLine = clipper ( this ) ;
86
+ this . format = maybeFunction ( format ) ;
85
87
}
86
88
render ( index , scales , values , dimensions , context ) {
87
89
const mark = this ;
@@ -116,7 +118,9 @@ export class Tip extends Mark {
116
118
117
119
// Determine the appropriate formatter.
118
120
const format =
119
- "title" in sources // if there is a title channel
121
+ this . format !== undefined
122
+ ? formatData ( this . format , values . data ) // use the custom format, if any
123
+ : "title" in sources // if there is a title channel
120
124
? formatTitle // display the title as-is
121
125
: index . fi == null // if this mark is not faceted
122
126
? formatChannels // display name-value pairs for channels
@@ -312,6 +316,14 @@ function getSources({channels}) {
312
316
return sources ;
313
317
}
314
318
319
+ function formatData ( format , data ) {
320
+ return function ( i ) {
321
+ let result = format . call ( this , data [ i ] , i ) ;
322
+ if ( isObject ( result ) ) result = Object . entries ( result ) . map ( ( [ name , value ] ) => ( { name, value} ) ) ;
323
+ return result ;
324
+ } ;
325
+ }
326
+
315
327
function formatTitle ( i , { title} ) {
316
328
return formatDefault ( title . value [ i ] ) ;
317
329
}
0 commit comments