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.

    The following values for the xAlign setting are supported.

    • 'left'
    • 'center'
    • 'right'
    • '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.filter Tooltip - 图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.

    NameArgumentsDataset overrideDescription
    beforeTitleTooltipItem[], objectReturns the text to render before the title.
    titleTooltipItem[], objectReturns text to render as the title of the tooltip.
    afterTitleTooltipItem[], objectReturns text to render after the title.
    beforeBodyTooltipItem[], objectReturns text to render before the body section.
    beforeLabelTooltipItem, objectYesReturns text to render before an individual label. This will be called for each item in the tooltip.
    labelTooltipItem, objectYesReturns text to render for an individual item in the tooltip.
    labelColorTooltipItem, ChartYesReturns the colors to render for the tooltip item. more…
    labelTextColorTooltipItem, ChartYesReturns the colors for the text of the label for the tooltip item.
    labelPointStyleTooltipItem, ChartYesReturns 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.
    afterLabelTooltipItem, objectYesReturns text to render after an individual label.
    afterBodyTooltipItem[], objectReturns text to render after the body section.
    beforeFooterTooltipItem[], objectReturns text to render before the footer section.
    footerTooltipItem[], objectReturns text to render as the footer of the tooltip.
    afterFooterTooltipItem[], objectText 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. var chart = new Chart(ctx, {
    2. type: 'line',
    3. data: data,
    4. options: {
    5. plugins: {
    6. tooltip: {
    7. callbacks: {
    8. label: function(context) {
    9. var 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. }
    22. });

    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. var chart = new Chart(ctx, {
    2. data: data,
    3. plugins: {
    4. tooltip: {
    5. usePointStyle: true,
    6. callbacks: {
    7. labelPointStyle: function(context) {
    8. return {
    9. pointStyle: 'triangle',
    10. rotation: 0
    11. };
    12. }
    13. }
    14. }
    15. }
    16. }
    17. });

    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. var 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. var 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. var 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. var titleLines = tooltipModel.title || [];
    37. var bodyLines = tooltipModel.body.map(getBody);
    38. var 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. var colors = tooltipModel.labelColors[i];
    45. var style = 'background:' + colors.backgroundColor;
    46. style += '; border-color:' + colors.borderColor;
    47. style += '; border-width: 2px';
    48. var span = '<span style="' + style + '"></span>';
    49. innerHtml += '<tr><td>' + span + body + '</td></tr>';
    50. });
    51. innerHtml += '</tbody>';
    52. var tableRoot = tooltipEl.querySelector('table');
    53. tableRoot.innerHTML = innerHtml;
    54. }
    55. var position = context.chart.canvas.getBoundingClientRect();
    56. var 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.