Tooltips

Tooltip Configuration

The tooltip configuration is passed into the options.tooltips namespace. The global options for the chart tooltips is defined in Chart.defaults.global.tooltips.

NameTypeDefaultDescription
enabledBooleantrueAre tooltips enabled
customFunctionnullSee custom tooltip section.
modeString'nearest'Sets which elements appear in the tooltip. more….
intersectBooleantrueif true, the tooltip mode applies only when the mouse position intersects with an element. If false, the mode will be applied at all times.
positionString'average'The mode for positioning the tooltip. more…
callbacksObjectSee the callbacks section
itemSortFunctionSort tooltip items. more…
filterFunctionFilter tooltip items. more…
backgroundColorColor'rgba(0,0,0,0.8)'Background color of the tooltip.
titleFontFamilyString"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"title font
titleFontSizeNumber12Title font size
titleFontStyleString'bold'Title font style
titleFontColorColor'#fff'Title font color
titleSpacingNumber2Spacing to add to top and bottom of each title line.
titleMarginBottomNumber6Margin to add on bottom of title section.
bodyFontFamilyString"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"body line font
bodyFontSizeNumber12Body font size
bodyFontStyleString'normal'Body font style
bodyFontColorColor'#fff'Body font color
bodySpacingNumber2Spacing to add to top and bottom of each tooltip item.
footerFontFamilyString"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"footer font
footerFontSizeNumber12Footer font size
footerFontStyleString'bold'Footer font style
footerFontColorColor'#fff'Footer font color
footerSpacingNumber2Spacing to add to top and bottom of each fotter line.
footerMarginTopNumber6Margin to add before drawing the footer.
xPaddingNumber6Padding to add on left and right of tooltip.
yPaddingNumber6Padding to add on top and bottom of tooltip.
caretPaddingNumber2Extra distance to move the end of the tooltip arrow away from the tooltip point.
caretSizeNumber5Size, in px, of the tooltip arrow.
cornerRadiusNumber6Radius of tooltip corner curves.
multiKeyBackgroundColor'#fff'Color to draw behind the colored boxes when multiple items are in the tooltip
displayColorsBooleantrueif true, color boxes are shown in the tooltip
borderColorColor'rgba(0,0,0,0)'Color of the border
borderWidthNumber0Size of the border

Position Modes

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.

Sort Callback

Allows sorting of tooltip items. Must implement at minimum a function that can be passed to Array.prototype.sort. 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 Array.prototype.filter. This function can also accept a second parameter that is the data object passed to the chart.

Tooltip Callbacks

The tooltip label configuration is nested below the tooltip configuration using the callbacks key. The tooltip has the following callbacks for providing text. For all functions, 'this' will be the tooltip object created from the Chart.Tooltip constructor.

All functions are called with the same arguments: a tooltip item and the data object passed to the chart. All functions must return either a string or an array of strings. Arrays of strings are treated as multiple lines of text.

NameArgumentsDescription
beforeTitleArray[tooltipItem], dataReturns the text to render before the title.
titleArray[tooltipItem], dataReturns text to render as the title of the tooltip.
afterTitleArray[tooltipItem], dataReturns text to render after the title.
beforeBodyArray[tooltipItem], dataReturns text to render before the body section.
beforeLabeltooltipItem, dataReturns text to render before an individual label. This will be called for each item in the tooltip.
labeltooltipItem, dataReturns text to render for an individual item in the tooltip.
labelColortooltipItem, chartReturns the colors to render for the tooltip item. more…
labelTextColortooltipItem, chartReturns the colors for the text of the label for the tooltip item.
afterLabeltooltipItem, dataReturns text to render after an individual label.
afterBodyArray[tooltipItem], dataReturns text to render after the body section
beforeFooterArray[tooltipItem], dataReturns text to render before the footer section.
footerArray[tooltipItem], dataReturns text to render as the footer of the tooltip.
afterFooterArray[tooltipItem], dataText to render after the footer section

Label Color Callback

For example, to return a red box for each item in the tooltip you could do:

  1. var chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. tooltips: {
  6. callbacks: {
  7. labelColor: function(tooltipItem, chart) {
  8. return {
  9. borderColor: 'rgb(255, 0, 0)',
  10. backgroundColor: 'rgb(255, 0, 0)'
  11. }
  12. },
  13. labelTextColor:function(tooltipItem, chart){
  14. return '#543453';
  15. }
  16. }
  17. }
  18. }
  19. });

Tooltip Item Interface

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

  1. {
  2. // X Value of the tooltip as a string
  3. xLabel: String,
  4. // Y value of the tooltip as a string
  5. yLabel: String,
  6. // Index of the dataset the item comes from
  7. datasetIndex: Number,
  8. // Index of this data item in the dataset
  9. index: Number,
  10. // X position of matching point
  11. x: Number,
  12. // Y position of matching point
  13. y: Number,
  14. }

External (Custom) Tooltips

Custom 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 oncanvas one. You can enable custom tooltips in the global or chart configuration like so:

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

See samples/tooltips/line-customTooltips.html for examples on how to get started.

Tooltip Model

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

  1. {
  2. // The items that we are rendering in the tooltip. See Tooltip Item Interface section
  3. dataPoints: TooltipItem[],
  4. // Positioning
  5. xPadding: Number,
  6. yPadding: Number,
  7. xAlign: String,
  8. yAlign: String,
  9. // X and Y properties are the top left of the tooltip
  10. x: Number,
  11. y: Number,
  12. width: Number,
  13. height: Number,
  14. // Where the tooltip points to
  15. caretX: Number,
  16. caretY: Number,
  17. // Body
  18. // The body lines that need to be rendered
  19. // Each pbject contains 3 parameters
  20. // before: String[] // lines of text before the line with the color square
  21. // lines: String[], // lines of text to render as the main item with color square
  22. // after: String[], // lines of text to render after the main lines
  23. body: Object[],
  24. // lines of text that appear after the title but before the body
  25. beforeBody: String[],
  26. // line of text that appear after the body and before the footer
  27. afterBody: String[],
  28. bodyFontColor: Color,
  29. _bodyFontFamily: String,
  30. _bodyFontStyle: String,
  31. _bodyAlign: String,
  32. bodyFontSize: Number,
  33. bodySpacing: Number,
  34. // Title
  35. // lines of text that form the title
  36. title: String[],
  37. titleFontColor: Color,
  38. _titleFontFamily: String,
  39. _titleFontStyle: String,
  40. titleFontSize: Number,
  41. _titleAlign: String,
  42. titleSpacing: Number,
  43. titleMarginBottom: Number,
  44. // Footer
  45. // lines of text that form the footer
  46. footer: String[],
  47. footerFontColor: Color,
  48. _footerFontFamily: String,
  49. _footerFontStyle: String,
  50. footerFontSize: Number,
  51. _footerAlign: String,
  52. footerSpacing: Number,
  53. footerMarginTop: Number,
  54. // Appearance
  55. caretSize: Number,
  56. cornerRadius: Number,
  57. backgroundColor: Color,
  58. // colors to render for each item in body[]. This is the color of the squares in the tooltip
  59. labelColors: Color[],
  60. // 0 opacity is a hidden tooltip
  61. opacity: Number,
  62. legendColorBackground: Color,
  63. displayColors: Boolean,
  64. }