]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix interaction on partially visible bar (#9446)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Tue, 20 Jul 2021 11:57:56 +0000 (14:57 +0300)
committerGitHub <noreply@github.com>
Tue, 20 Jul 2021 11:57:56 +0000 (07:57 -0400)
src/core/core.interaction.js
test/fixtures/core.interaction/nearest-partial-bar.js [new file with mode: 0644]
test/fixtures/core.interaction/nearest-partial-bar.png [new file with mode: 0644]
test/fixtures/core.interaction/nearest-point-behind-scale.js [new file with mode: 0644]
test/fixtures/core.interaction/nearest-point-behind-scale.png [new file with mode: 0644]
test/specs/core.interaction.tests.js

index fbfaf6ebbd739b427b61d3399d33c1c5e65cc337..5365f6c9108e56a00ee2c0f2f04fdf42e9514362 100644 (file)
@@ -166,7 +166,7 @@ function getNearestItems(chart, position, axis, intersect, useFinalPosition) {
     }
 
     const center = element.getCenterPoint(useFinalPosition);
-    if (!_isPointInArea(center, chart.chartArea, chart._minPadding)) {
+    if (!_isPointInArea(center, chart.chartArea, chart._minPadding) && !element.inRange(position.x, position.y, useFinalPosition)) {
       return;
     }
     const distance = distanceMetric(position, center);
diff --git a/test/fixtures/core.interaction/nearest-partial-bar.js b/test/fixtures/core.interaction/nearest-partial-bar.js
new file mode 100644 (file)
index 0000000..420f849
--- /dev/null
@@ -0,0 +1,42 @@
+module.exports = {
+  config: {
+    type: 'bar',
+    data: {
+      labels: ['a', 'b', 'c'],
+      datasets: [
+        {
+          data: [220, 250, 225],
+        },
+      ],
+    },
+    options: {
+      events: ['click'],
+      interaction: {
+        mode: 'nearest'
+      },
+      plugins: {
+        tooltip: true,
+        legend: false
+      },
+      scales: {
+        y: {
+          beginAtZero: false
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      width: 256,
+      height: 256
+    },
+    async run(chart) {
+      const point = {
+        x: chart.chartArea.left + chart.chartArea.width / 2,
+        y: chart.chartArea.top + chart.chartArea.height / 2,
+      };
+      await jasmine.triggerMouseEvent(chart, 'click', point);
+    }
+  }
+};
diff --git a/test/fixtures/core.interaction/nearest-partial-bar.png b/test/fixtures/core.interaction/nearest-partial-bar.png
new file mode 100644 (file)
index 0000000..9077952
Binary files /dev/null and b/test/fixtures/core.interaction/nearest-partial-bar.png differ
diff --git a/test/fixtures/core.interaction/nearest-point-behind-scale.js b/test/fixtures/core.interaction/nearest-point-behind-scale.js
new file mode 100644 (file)
index 0000000..6dfa25c
--- /dev/null
@@ -0,0 +1,45 @@
+module.exports = {
+  config: {
+    type: 'scatter',
+    data: {
+      datasets: [{
+        data: [{x: 1, y: 1}, {x: 48, y: 1}]
+      }]
+    },
+    options: {
+      events: ['click'],
+      interaction: {
+        mode: 'nearest',
+        intersect: false
+      },
+      plugins: {
+        tooltip: true,
+        legend: false
+      },
+      scales: {
+        x: {
+          min: 5,
+          max: 50
+        },
+        y: {
+          min: 0,
+          max: 2
+        }
+      },
+      layout: {
+        padding: 50
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      width: 256,
+      height: 256
+    },
+    async run(chart) {
+      const point = chart.getDatasetMeta(0).data[0];
+      await jasmine.triggerMouseEvent(chart, 'click', {y: point.y, x: chart.chartArea.left});
+    }
+  }
+};
diff --git a/test/fixtures/core.interaction/nearest-point-behind-scale.png b/test/fixtures/core.interaction/nearest-point-behind-scale.png
new file mode 100644 (file)
index 0000000..42d9287
Binary files /dev/null and b/test/fixtures/core.interaction/nearest-point-behind-scale.png differ
index a3ce810db7f6c1c882674bc69332620a0177fda0..f3e27aacb73030d78cf07ae0e24d7fcef8f6c676 100644 (file)
@@ -1,7 +1,6 @@
-// Tests of the interaction handlers in Core.Interaction
-
-// Test the rectangle element
 describe('Core.Interaction', function() {
+  describe('auto', jasmine.fixture.specs('core.interaction'));
+
   describe('point mode', function() {
     beforeEach(function() {
       this.chart = window.acquireChart({