SeriesTooltip
Name: Series Tooltip
Description: Tooltip used for Series data type
params$
Type:
Partial<SeriesTooltipParams>
Fields:
Name Description Type backgroundColorType Background color type ColorType
backgroundOpacity Background opacity number
strokeColorType Border color type ColorType
textColorType Text color type ColorType
offset Mouse cursor offset (x, y) [number, number]
padding Inner padding number
renderFn Render function, can return three formats:
(1) Plain text string (string
)
(2) Array of strings for multiline text (string[]
)
(3) SVG tag content string (string
)(eventData: EventSeries, context: { styles: BaseTooltipStyle; utils: BaseTooltipUtils }) => string[] | string
Detailed Types
type ColorType = 'none' | 'label' | 'labelContrast' | 'primary' | 'secondary' | 'background'
interface BaseTooltipStyle {
backgroundColor: string
backgroundOpacity: number
strokeColor: string
offset: [number, number]
padding: number
textColor: string
textSize: number | string // chartParams setting
textSizePx: number
seriesColors: string[]
}
interface BaseTooltipUtils {
toCurrency: (num: number | null) => string
measureTextWidth (text: string, size?: number): number
}
- Default values:
{
backgroundColorType: 'background',
strokeColorType: 'primary',
backgroundOpacity: 0.8,
textColorType: 'primary',
offset: [20, 5],
padding: 10,
renderFn: (eventData, { styles, utils }) => {
const hasSeriesLabel = eventData.seriesLabel.slice(0, 7) === 'series_' ? false : true
const hasDatumLabel = eventData.datum.label.slice(0, 7) === 'series_' ? false : true
const valueText = utils.toCurrency(eventData.datum.value)
const bulletWidth = styles.textSizePx * 0.7
const offset = (styles.textSizePx / 2) - (bulletWidth / 2)
const seriesSvg = hasSeriesLabel
? `<rect width="${bulletWidth}" height="${bulletWidth}" x="${offset}" y="${offset - 1}" rx="${bulletWidth / 2}" fill="${eventData.datum.color}"></rect>
<text x="${styles.textSizePx * 1.5}" font-size="${styles.textSizePx}" dominant-baseline="hanging" fill="${styles.textColor}">
<tspan>${eventData.seriesLabel}</tspan>
</text>`
: ''
const datumLabelSvg = hasDatumLabel
? `<tspan>${eventData.datum.label}</tspan> `
: ''
const seriesLabelTextWidth = hasSeriesLabel
? utils.measureTextWidth(`${eventData.seriesLabel}${valueText}`, styles.textSizePx) + styles.textSizePx * 1.5
: 0
const datumLabelTextWidth = hasDatumLabel
? utils.measureTextWidth(`${eventData.datum.label}${valueText}`, styles.textSizePx)
: 0
const maxTextWidth = Math.max(seriesLabelTextWidth, datumLabelTextWidth)
const lineEndX = hasDatumLabel
? maxTextWidth + styles.textSizePx * 1.5
: 0
const valueTextAnchor = hasDatumLabel ? 'end' : 'start'
const datumSvg = `<text font-size="${styles.textSizePx}" dominant-baseline="hanging" fill="${styles.textColor}">
${datumLabelSvg}<tspan font-weight="bold" text-anchor="${valueTextAnchor}" x="${lineEndX}">${valueText}</tspan>
</text>`
return `${seriesSvg}
<g ${hasSeriesLabel ? `transform="translate(0, ${styles.textSizePx * 2})"` : ''}>
${datumSvg}
</g>`
},
}