@@ -5,9 +5,9 @@ import {defined} from "../defined.js";
55import { formatDefault } from "../format.js" ;
66import { anchorX , anchorY } from "../interactions/pointer.js" ;
77import { 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" ;
99import { 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" ;
1111import { inferTickFormat } from "./axis.js" ;
1212import { applyIndirectTextStyles , defaultWidth , ellipsis , monospaceWidth } from "./text.js" ;
1313import { cut , clipper , splitter , maybeTextOverflow } from "./text.js" ;
@@ -42,6 +42,7 @@ export class Tip extends Mark {
4242 lineHeight = 1 ,
4343 lineWidth = 20 ,
4444 frameAnchor,
45+ format,
4546 textAnchor = "start" ,
4647 textOverflow,
4748 textPadding = 8 ,
@@ -82,6 +83,7 @@ export class Tip extends Mark {
8283 for ( const key in defaults ) if ( key in this . channels ) this [ key ] = defaults [ key ] ; // apply default even if channel
8384 this . splitLines = splitter ( this ) ;
8485 this . clipLine = clipper ( this ) ;
86+ this . format = maybeFunction ( format ) ;
8587 }
8688 render ( index , scales , values , dimensions , context ) {
8789 const mark = this ;
@@ -116,7 +118,9 @@ export class Tip extends Mark {
116118
117119 // Determine the appropriate formatter.
118120 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
120124 ? formatTitle // display the title as-is
121125 : index . fi == null // if this mark is not faceted
122126 ? formatChannels // display name-value pairs for channels
@@ -312,6 +316,14 @@ function getSources({channels}) {
312316 return sources ;
313317}
314318
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+
315327function formatTitle ( i , { title} ) {
316328 return formatDefault ( title . value [ i ] ) ;
317329}
0 commit comments