Interaction Modes

This sample shows how to use the tooltip position mode setting.

Interaction Modes - 图1

config setup actions

  1. const config = {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. interaction: {
  6. intersect: false,
  7. mode: 'index',
  8. },
  9. plugins: {
  10. title: {
  11. display: true,
  12. text: (ctx) => {
  13. const {intersect, mode} = ctx.chart.options.interaction;
  14. return 'Mode: ' + mode + ', intersect: ' + intersect;
  15. }
  16. },
  17. }
  18. }
  19. };
  1. const DATA_COUNT = 7;
  2. const NUMBER_CFG = {count: DATA_COUNT, min: -100, max: 100};
  3. const data = {
  4. labels: Utils.months({count: DATA_COUNT}),
  5. datasets: [
  6. {
  7. label: 'Dataset 1',
  8. data: Utils.numbers(NUMBER_CFG),
  9. fill: false,
  10. borderColor: Utils.CHART_COLORS.red,
  11. backgroundColor: Utils.transparentize(Utils.CHART_COLORS.red, 0.5),
  12. },
  13. {
  14. label: 'Dataset 2',
  15. data: Utils.numbers(NUMBER_CFG),
  16. fill: false,
  17. borderColor: Utils.CHART_COLORS.blue,
  18. backgroundColor: Utils.transparentize(Utils.CHART_COLORS.blue, 0.5),
  19. },
  20. ]
  21. };
  1. const actions = [
  2. {
  3. name: 'Mode: index',
  4. handler(chart) {
  5. chart.options.interaction.mode = 'index';
  6. chart.update();
  7. }
  8. },
  9. {
  10. name: 'Mode: dataset',
  11. handler(chart) {
  12. chart.options.interaction.mode = 'dataset';
  13. chart.update();
  14. }
  15. },
  16. {
  17. name: 'Mode: point',
  18. handler(chart) {
  19. chart.options.interaction.mode = 'point';
  20. chart.update();
  21. }
  22. },
  23. {
  24. name: 'Mode: nearest',
  25. handler(chart) {
  26. chart.options.interaction.mode = 'nearest';
  27. chart.update();
  28. }
  29. },
  30. {
  31. name: 'Mode: x',
  32. handler(chart) {
  33. chart.options.interaction.mode = 'x';
  34. chart.update();
  35. }
  36. },
  37. {
  38. name: 'Mode: y',
  39. handler(chart) {
  40. chart.options.interaction.mode = 'y';
  41. chart.update();
  42. }
  43. },
  44. {
  45. name: 'Toggle Intersect',
  46. handler(chart) {
  47. chart.options.interaction.intersect = !chart.options.interaction.intersect;
  48. chart.update();
  49. }
  50. },
  51. ];