]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix/getRelativePosition NaN value with native event (#8459)
authorJacco van den Berg <39033624+LeeLenaleee@users.noreply.github.com>
Fri, 19 Feb 2021 22:57:59 +0000 (23:57 +0100)
committerGitHub <noreply@github.com>
Fri, 19 Feb 2021 22:57:59 +0000 (17:57 -0500)
* fix bug where onClick value returned NaN because originalEvent does not exist
* add test for this behaviour
* test to async

src/helpers/helpers.dom.js
test/specs/helpers.dom.tests.js

index 4a06c9f80adc9c799e0d653ecd45b6612b4fca82..05e1aa6a499f8ff0d46cedbd2b8d60e540d12073 100644 (file)
@@ -53,7 +53,7 @@ function getPositionedStyle(styles, style, suffix) {
 const useOffsetPos = (x, y, target) => (x > 0 || y > 0) && (!target || !target.shadowRoot);
 
 function getCanvasPosition(evt, canvas) {
-  const e = evt.originalEvent || evt;
+  const e = evt.native || evt;
   const touches = e.touches;
   const source = touches && touches.length ? touches[0] : e;
   const {offsetX, offsetY} = source;
index d3b3f4c6bf364d9893daff9559b7a96a3d2f54d0..fd0d0de14c781c1f9c18bf34e3342bc626c939b4 100644 (file)
@@ -399,5 +399,33 @@ describe('DOM helpers tests', function() {
       expect(Math.abs(pos3.x - Math.round((event.clientX - rect3.x - 10) / 360 * 400))).toBeLessThanOrEqual(1);
       expect(Math.abs(pos3.y - Math.round((event.clientY - rect3.y - 10) / 360 * 400))).toBeLessThanOrEqual(1);
     });
+
+    it('Should not return NaN with a custom event', async function() {
+      let dataX = null;
+      let dataY = null;
+      const chart = window.acquireChart(
+        {
+          type: 'bar',
+          data: {
+            datasets: [{
+              data: [{x: 'first', y: 10}, {x: 'second', y: 5}, {x: 'third', y: 15}]
+            }]
+          },
+          options: {
+            onHover: (e) => {
+              const canvasPosition = Chart.helpers.getRelativePosition(e, chart);
+
+              dataX = canvasPosition.x;
+              dataY = canvasPosition.y;
+            }
+          }
+        });
+
+      const point = chart.getDatasetMeta(0).data[1];
+      await jasmine.triggerMouseEvent(chart, 'mousemove', point);
+
+      expect(dataX).not.toEqual(NaN);
+      expect(dataY).not.toEqual(NaN);
+    });
   });
 });