Interactions

Namespace: options.interaction, the global interaction configuration is at Chart.defaults.interaction. To configure which events trigger chart interactions, see events.

NameTypeDefaultDescription
modestring‘nearest’Sets which elements appear in the interaction. See Interaction Modes for details.
intersectbooleantrueif true, the interaction mode only applies when the mouse position intersects an item on the chart.
axisstring‘x’Can be set to ‘x’, ‘y’, ‘xy’ or ‘r’ to define which directions are used in calculating distances. Defaults to ‘x’ for ‘index’ mode and ‘xy’ in dataset and ‘nearest’ modes.
includeInvisiblebooleanfalseif true, the invisible points that are outside of the chart area will also be included when evaluating interactions.

By default, these options apply to both the hover and tooltip interactions. The same options can be set in the options.hover namespace, in which case they will only affect the hover interaction. Similarly, the options can be set in the options.plugins.tooltip namespace to independently configure the tooltip interactions.

Events

The following properties define how the chart interacts with events. Namespace: options

NameTypeDefaultDescription
eventsstring[][‘mousemove’, ‘mouseout’, ‘click’, ‘touchstart’, ‘touchmove’]The events option defines the browser events that the chart should listen to for. Each of these events trigger hover and are passed to plugins. more…
onHoverfunctionnullCalled when any of the events fire over chartArea. Passed the event, an array of active elements (bars, points, etc), and the chart.
onClickfunctionnullCalled if the event is of type ‘mouseup’, ‘click’ or ‘‘contextmenu’ over chartArea. Passed the event, an array of active elements, and the chart.

Event Option

For example, to have the chart only respond to click events, you could do:

  1. const chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. // This chart will not respond to mousemove, etc
  6. events: ['click']
  7. }
  8. });

Events for each plugin can be further limited by defining (allowed) events array in plugin options:

  1. const chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. // All of these (default) events trigger a hover and are passed to all plugins,
  6. // unless limited at plugin options
  7. events: ['mousemove', 'mouseout', 'click', 'touchstart', 'touchmove'],
  8. plugins: {
  9. tooltip: {
  10. // Tooltip will only receive click events
  11. events: ['click']
  12. }
  13. }
  14. }
  15. });

Events that do not fire over chartArea, like mouseout, can be captured using a simple plugin:

  1. const chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. // these are the default events:
  6. // events: ['mousemove', 'mouseout', 'click', 'touchstart', 'touchmove'],
  7. },
  8. plugins: [{
  9. id: 'myEventCatcher',
  10. beforeEvent(chart, args, pluginOptions) {
  11. const event = args.event;
  12. if (event.type === 'mouseout') {
  13. // process the event
  14. }
  15. }
  16. }]
  17. });

For more information about plugins, see Plugins

Converting Events to Data Values

A common occurrence is taking an event, such as a click, and finding the data coordinates on the chart where the event occurred. Chart.js provides helpers that make this a straightforward process.

  1. const chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. onClick: (e) => {
  6. const canvasPosition = Chart.helpers.getRelativePosition(e, chart);
  7. // Substitute the appropriate scale IDs
  8. const dataX = chart.scales.x.getValueForPixel(canvasPosition.x);
  9. const dataY = chart.scales.y.getValueForPixel(canvasPosition.y);
  10. }
  11. }
  12. });

When using a bundler, the helper functions have to be imported seperatly, for a full explanation of this please head over to the integration page

Modes

When configuring the interaction with the graph via interaction, hover or tooltips, a number of different modes are available.

options.hover and options.plugins.tooltip extend from options.interaction. So if mode, intersect or any other common settings are configured only in options.interaction, both hover and tooltips obey that.

The modes are detailed below and how they behave in conjunction with the intersect setting.

See how different modes work with the tooltip in tooltip interactions sample

point

Finds all of the items that intersect the point.

  1. const chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. interaction: {
  6. mode: 'point'
  7. }
  8. }
  9. });

nearest

Gets the items that are at the nearest distance to the point. The nearest item is determined based on the distance to the center of the chart item (point, bar). You can use the axis setting to define which coordinates are considered in distance calculation. If intersect is true, this is only triggered when the mouse position intersects an item in the graph. This is very useful for combo charts where points are hidden behind bars.

  1. const chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. interaction: {
  6. mode: 'nearest'
  7. }
  8. }
  9. });

index

Finds item at the same index. If the intersect setting is true, the first intersecting item is used to determine the index in the data. If intersect false the nearest item, in the x direction, is used to determine the index.

  1. const chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. interaction: {
  6. mode: 'index'
  7. }
  8. }
  9. });

To use index mode in a chart like the horizontal bar chart, where we search along the y direction, you can use the axis setting introduced in v2.7.0. By setting this value to 'y' on the y direction is used.

  1. const chart = new Chart(ctx, {
  2. type: 'bar',
  3. data: data,
  4. options: {
  5. interaction: {
  6. mode: 'index',
  7. axis: 'y'
  8. }
  9. }
  10. });

dataset

Finds items in the same dataset. If the intersect setting is true, the first intersecting item is used to determine the index in the data. If intersect false the nearest item is used to determine the index.

  1. const chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. interaction: {
  6. mode: 'dataset'
  7. }
  8. }
  9. });

x

Returns all items that would intersect based on the X coordinate of the position only. Would be useful for a vertical cursor implementation. Note that this only applies to cartesian charts.

  1. const chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. interaction: {
  6. mode: 'x'
  7. }
  8. }
  9. });

y

Returns all items that would intersect based on the Y coordinate of the position. This would be useful for a horizontal cursor implementation. Note that this only applies to cartesian charts.

  1. const chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. interaction: {
  6. mode: 'y'
  7. }
  8. }
  9. });

Custom Interaction Modes

New modes can be defined by adding functions to the Chart.Interaction.modes map. You can use the Chart.Interaction.evaluateInteractionItems function to help implement these.

Example:

  1. import { Interaction } from 'chart.js';
  2. import { getRelativePosition } from 'chart.js/helpers';
  3. /**
  4. * Custom interaction mode
  5. * @function Interaction.modes.myCustomMode
  6. * @param {Chart} chart - the chart we are returning items from
  7. * @param {Event} e - the event we are find things at
  8. * @param {InteractionOptions} options - options to use
  9. * @param {boolean} [useFinalPosition] - use final element position (animation target)
  10. * @return {InteractionItem[]} - items that are found
  11. */
  12. Interaction.modes.myCustomMode = function(chart, e, options, useFinalPosition) {
  13. const position = getRelativePosition(e, chart);
  14. const items = [];
  15. Interaction.evaluateInteractionItems(chart, 'x', position, (element, datasetIndex, index) => {
  16. if (element.inXRange(position.x, useFinalPosition) && myCustomLogic(element)) {
  17. items.push({element, datasetIndex, index});
  18. }
  19. });
  20. return items;
  21. };
  22. // Then, to use it...
  23. new Chart.js(ctx, {
  24. type: 'line',
  25. data: data,
  26. options: {
  27. interaction: {
  28. mode: 'myCustomMode'
  29. }
  30. }
  31. })

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

  1. declare module 'chart.js' {
  2. interface InteractionModeMap {
  3. myCustomMode: InteractionModeFunction;
  4. }
  5. }