Tooltip

    WARNING

    The bubble, doughnut, pie, polar area, and scatter charts override the tooltip defaults. To change the overrides for those chart types, the options are defined in Chart.overrides[type].plugins.tooltip.

    Possible modes are:

    • 'average'
    • 'nearest'

    'average' mode will place the tooltip at the average position of the items displayed in the tooltip. 'nearest' will place the tooltip at the position of the element closest to the event position.

    You can also define custom position modes.

    Tooltip Alignment

    The xAlign and yAlign options define the position of the tooltip caret. If these parameters are unset, the optimal caret position is determined.

    The following values for the xAlign setting are supported.

    • 'left'
    • 'center'
    • 'right'

    The following values for the yAlign setting are supported.

    • 'top'
    • 'center'
    • 'bottom'

    Text Alignment

    • 'left' (default)
    • 'right'
    • 'center'

    These options are only applied to text lines. Color boxes are always aligned to the left edge.

    Allows sorting of . Must implement at minimum a function that can be passed to Array.prototype.sort (opens new window). This function can also accept a third parameter that is the data object passed to the chart.

    Filter Callback

    Allows filtering of tooltip items. Must implement at minimum a function that can be passed to . This function can also accept a fourth parameter that is the data object passed to the chart.

    Namespace: options.plugins.tooltip.callbacks, the tooltip has the following callbacks for providing text. For all functions, this will be the tooltip object created from the Tooltip constructor.

    Namespace: data.datasets[].tooltip.callbacks, items marked with Yes in the column Dataset override can be overridden per dataset.

    A tooltip item context is generated for each item that appears in the tooltip. This is the primary model that the callback methods interact with. For functions that return text, arrays of strings are treated as multiple lines of text.

    NameArgumentsReturn TypeDataset overrideDescription
    beforeTitleTooltipItem[]string | string[]Returns the text to render before the title.
    titleTooltipItem[]string | string[]Returns text to render as the title of the tooltip.
    afterTitleTooltipItem[]string | string[]Returns text to render after the title.
    beforeBodyTooltipItem[]string | string[]Returns text to render before the body section.
    beforeLabelTooltipItemstring | string[]YesReturns text to render before an individual label. This will be called for each item in the tooltip.
    labelTooltipItemstring | string[]YesReturns text to render for an individual item in the tooltip.
    labelColorTooltipItemobjectYesReturns the colors to render for the tooltip item. more…
    labelTextColorTooltipItemColorYesReturns the colors for the text of the label for the tooltip item.
    labelPointStyleTooltipItemobjectYesReturns the point style to use instead of color boxes if usePointStyle is true (object with values pointStyle and rotation). Default implementation uses the point style from the dataset points.
    afterLabelTooltipItemstring | string[]YesReturns text to render after an individual label.
    afterBodyTooltipItem[]string | string[]Returns text to render after the body section.
    beforeFooterTooltipItem[]string | string[]Returns text to render before the footer section.
    footerTooltipItem[]string | string[]Returns text to render as the footer of the tooltip.
    afterFooterTooltipItem[]string | string[]Text to render after the footer section.

    Label Callback

    The label callback can change the text that displays for a given data point. A common example to show a unit. The example below puts a '$' before every row.

    For example, to return a red box with a blue dashed border that has a border radius for each item in the tooltip you could do:

    1. const chart = new Chart(ctx, {
    2. type: 'line',
    3. data: data,
    4. options: {
    5. plugins: {
    6. tooltip: {
    7. callbacks: {
    8. labelColor: function(context) {
    9. return {
    10. borderColor: 'rgb(0, 0, 255)',
    11. backgroundColor: 'rgb(255, 0, 0)',
    12. borderDash: [2, 2],
    13. borderRadius: 2,
    14. };
    15. },
    16. labelTextColor: function(context) {
    17. return '#543453';
    18. }
    19. }
    20. }
    21. }
    22. });

    Label Point Style Callback

    Tooltip Item Context

    The tooltip items passed to the tooltip callbacks implement the following interface.

    1. {
    2. // The chart the tooltip is being shown on
    3. chart: Chart
    4. // Label for the tooltip
    5. label: string,
    6. // Parsed data values for the given `dataIndex` and `datasetIndex`
    7. parsed: object,
    8. // Raw data values for the given `dataIndex` and `datasetIndex`
    9. raw: object,
    10. // Formatted value for the tooltip
    11. formattedValue: string,
    12. // The dataset the item comes from
    13. dataset: object
    14. // Index of the dataset the item comes from
    15. datasetIndex: number,
    16. // Index of this data item in the dataset
    17. dataIndex: number,
    18. // The chart element (point, arc, bar, etc.) for this tooltip item
    19. element: Element,
    20. }

    External tooltips allow you to hook into the tooltip rendering process so that you can render the tooltip in your own custom way. Generally this is used to create an HTML tooltip instead of an on-canvas tooltip. The external option takes a function which is passed a context parameter containing the chart and tooltip. You can enable external tooltips in the global or chart configuration like so:

    See for examples on how to get started with external tooltips.

    The tooltip model contains parameters that can be used to render the tooltip.

    1. {
    2. chart: Chart,
    3. // The items that we are rendering in the tooltip. See Tooltip Item Interface section
    4. dataPoints: TooltipItem[],
    5. // Positioning
    6. xAlign: string,
    7. yAlign: string,
    8. x: number,
    9. y: number,
    10. width: number,
    11. height: number,
    12. // Where the tooltip points to
    13. caretX: number,
    14. caretY: number,
    15. // Body
    16. // The body lines that need to be rendered
    17. // Each object contains 3 parameters
    18. // before: string[] // lines of text before the line with the color square
    19. // lines: string[], // lines of text to render as the main item with color square
    20. // after: string[], // lines of text to render after the main lines
    21. body: object[],
    22. // lines of text that appear after the title but before the body
    23. beforeBody: string[],
    24. // line of text that appear after the body and before the footer
    25. afterBody: string[],
    26. // Title
    27. // lines of text that form the title
    28. title: string[],
    29. // Footer
    30. // lines of text that form the footer
    31. footer: string[],
    32. // colors to render for each item in body[]. This is the color of the squares in the tooltip
    33. labelColors: Color[],
    34. labelTextColors: Color[],
    35. // 0 opacity is a hidden tooltip
    36. opacity: number,
    37. // tooltip options
    38. options: Object
    39. }

    New modes can be defined by adding functions to the Chart.Tooltip.positioners map.

    Example:

    See samples for a more detailed example.

    If you’re using TypeScript, you’ll also need to register the new mode:

    1. declare module 'chart.js' {
    2. interface TooltipPositionerMap {
    3. myCustomPositioner: TooltipPositionerFunction<ChartType>;
    4. }