]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
display tooltips only at points in chart area (#10289)
authort-mangoe <m.takashi.xpair@gmail.com>
Sat, 7 May 2022 14:43:47 +0000 (23:43 +0900)
committerGitHub <noreply@github.com>
Sat, 7 May 2022 14:43:47 +0000 (10:43 -0400)
* show only points in chart area

* use the _isPointInArea helper function

src/core/core.interaction.js
test/specs/core.interaction.tests.js

index 2e18acc9d7eea2499073ada6c9073aa0eb4a3101..4a8df47fa10c6dc60ae2c66fa11afa1fd363a12a 100644 (file)
@@ -1,6 +1,7 @@
 import {_lookupByKey, _rlookupByKey} from '../helpers/helpers.collection';
 import {getRelativePosition} from '../helpers/helpers.dom';
 import {_angleBetween, getAngleFromPoint} from '../helpers/helpers.math';
+import {_isPointInArea} from '../helpers';
 
 /**
  * @typedef { import("./core.controller").default } Chart
@@ -97,6 +98,9 @@ function getIntersectItems(chart, position, axis, useFinalPosition) {
   }
 
   const evaluationFunc = function(element, datasetIndex, index) {
+    if (!_isPointInArea(element, chart.chartArea, 0)) {
+      return;
+    }
     if (element.inRange(position.x, position.y, useFinalPosition)) {
       items.push({element, datasetIndex, index});
     }
index 79d9b1048942d8d55a922060f288cdf40ddcf928..34ee6438c2794461710ab5779f0298d4c1360a64 100644 (file)
@@ -834,4 +834,41 @@ describe('Core.Interaction', function() {
       expect(elements).toEqual([meta0.data[1], meta1.data[0], meta1.data[1], meta1.data[2]]);
     });
   });
+
+  describe('tooltip element of scatter chart', function() {
+    it ('out-of-range datapoints are not shown in tooltip', function() {
+      let data = [];
+      for (let i = 0; i < 1000; i++) {
+        data.push({x: i, y: i});
+      }
+
+      const chart = window.acquireChart({
+        type: 'scatter',
+        data: {
+          datasets: [{data}]
+        },
+        options: {
+          scales: {
+            x: {
+              min: 2
+            }
+          }
+        }
+      });
+
+      const meta0 = chart.getDatasetMeta(0);
+      const firstElement = meta0.data[0];
+
+      const evt = {
+        type: 'click',
+        chart: chart,
+        native: true, // needed otherwise things its a DOM event
+        x: firstElement.x,
+        y: firstElement.y
+      };
+
+      const elements = Chart.Interaction.modes.point(chart, evt, {intersect: true}).map(item => item.element);
+      expect(elements).not.toContain(firstElement);
+    });
+  });
 });