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.

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

    Example:

    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.

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

    The following values for the yAlign setting are supported.

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

    Text Alignment

    The titleAlign, bodyAlign and footerAlign options define the horizontal position of the text lines with respect to the tooltip box. The following values are supported.

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

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

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

    Filter Callback

    Allows filtering of . Must implement at minimum a function that can be passed to Array.prototype.filterTooltip - 图2 (opens new window). 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.

    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.

    1. const chart = new Chart(ctx, {
    2. type: 'line',
    3. data: data,
    4. options: {
    5. plugins: {
    6. tooltip: {
    7. callbacks: {
    8. label: function(context) {
    9. let label = context.dataset.label || '';
    10. if (label) {
    11. label += ': ';
    12. }
    13. if (context.parsed.y !== null) {
    14. label += new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(context.parsed.y);
    15. }
    16. return label;
    17. }
    18. }
    19. }
    20. }
    21. }

    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:

    Label Point Style Callback

    For example, to draw triangles instead of the regular color box for each item in the tooltip you could do:

    1. const chart = new Chart(ctx, {
    2. data: data,
    3. options: {
    4. plugins: {
    5. tooltip: {
    6. usePointStyle: true,
    7. callbacks: {
    8. labelPointStyle: function(context) {
    9. return {
    10. pointStyle: 'triangle',
    11. rotation: 0
    12. };
    13. }
    14. }
    15. }
    16. }
    17. }
    18. });

    Tooltip Item Context

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

    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:

    1. const myPieChart = new Chart(ctx, {
    2. type: 'pie',
    3. data: data,
    4. options: {
    5. plugins: {
    6. tooltip: {
    7. // Disable the on-canvas tooltip
    8. enabled: false,
    9. external: function(context) {
    10. // Tooltip Element
    11. let tooltipEl = document.getElementById('chartjs-tooltip');
    12. // Create element on first render
    13. if (!tooltipEl) {
    14. tooltipEl = document.createElement('div');
    15. tooltipEl.id = 'chartjs-tooltip';
    16. tooltipEl.innerHTML = '<table></table>';
    17. document.body.appendChild(tooltipEl);
    18. }
    19. // Hide if no tooltip
    20. const tooltipModel = context.tooltip;
    21. if (tooltipModel.opacity === 0) {
    22. tooltipEl.style.opacity = 0;
    23. return;
    24. }
    25. // Set caret Position
    26. tooltipEl.classList.remove('above', 'below', 'no-transform');
    27. if (tooltipModel.yAlign) {
    28. } else {
    29. tooltipEl.classList.add('no-transform');
    30. }
    31. function getBody(bodyItem) {
    32. return bodyItem.lines;
    33. }
    34. // Set Text
    35. if (tooltipModel.body) {
    36. const titleLines = tooltipModel.title || [];
    37. const bodyLines = tooltipModel.body.map(getBody);
    38. let innerHtml = '<thead>';
    39. titleLines.forEach(function(title) {
    40. innerHtml += '<tr><th>' + title + '</th></tr>';
    41. });
    42. innerHtml += '</thead><tbody>';
    43. bodyLines.forEach(function(body, i) {
    44. const colors = tooltipModel.labelColors[i];
    45. let style = 'background:' + colors.backgroundColor;
    46. style += '; border-color:' + colors.borderColor;
    47. style += '; border-width: 2px';
    48. const span = '<span style="' + style + '"></span>';
    49. innerHtml += '<tr><td>' + span + body + '</td></tr>';
    50. });
    51. innerHtml += '</tbody>';
    52. let tableRoot = tooltipEl.querySelector('table');
    53. tableRoot.innerHTML = innerHtml;
    54. }
    55. const position = context.chart.canvas.getBoundingClientRect();
    56. const bodyFont = Chart.helpers.toFont(tooltipModel.options.bodyFont);
    57. // Display, position, and set styles for font
    58. tooltipEl.style.opacity = 1;
    59. tooltipEl.style.position = 'absolute';
    60. tooltipEl.style.left = position.left + window.pageXOffset + tooltipModel.caretX + 'px';
    61. tooltipEl.style.top = position.top + window.pageYOffset + tooltipModel.caretY + 'px';
    62. tooltipEl.style.font = bodyFont.string;
    63. tooltipEl.style.padding = tooltipModel.padding + 'px ' + tooltipModel.padding + 'px';
    64. tooltipEl.style.pointerEvents = 'none';
    65. }
    66. }
    67. }
    68. });

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

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