]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Don't draw points outside chartArea (#9443)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Tue, 20 Jul 2021 11:52:41 +0000 (14:52 +0300)
committerGitHub <noreply@github.com>
Tue, 20 Jul 2021 11:52:41 +0000 (07:52 -0400)
src/elements/element.point.js
src/helpers/helpers.canvas.js
test/fixtures/controller.line/clip/false.js [new file with mode: 0644]
test/fixtures/controller.line/clip/false.png [new file with mode: 0644]

index 8c4e094cc6e6f8808c880b7cf39a4087539a0ebb..05b521910f4b15800636c3a72b8fdaa54b1a6f5f 100644 (file)
@@ -1,5 +1,5 @@
 import Element from '../core/core.element';
-import {drawPoint} from '../helpers/helpers.canvas';
+import {drawPoint, _isPointInArea} from '../helpers/helpers.canvas';
 
 function inRange(el, pos, axis, useFinalPosition) {
   const options = el.options;
@@ -50,11 +50,11 @@ export default class PointElement extends Element {
     return (radius + borderWidth) * 2;
   }
 
-  draw(ctx) {
+  draw(ctx, area) {
     const me = this;
     const options = me.options;
 
-    if (me.skip || options.radius < 0.1) {
+    if (me.skip || options.radius < 0.1 || !_isPointInArea(me, area, me.size(options) / 2)) {
       return;
     }
 
index faae4e5db348782afa144cb0c56945d4e20e1c7e..c65f581156713d1cb6a4753ba0a99b1c5d3bafaa 100644 (file)
@@ -251,7 +251,7 @@ export function drawPoint(ctx, options, x, y) {
 export function _isPointInArea(point, area, margin) {
   margin = margin || 0.5; // margin - default is to match rounded decimals
 
-  return point && point.x > area.left - margin && point.x < area.right + margin &&
+  return point && area && point.x > area.left - margin && point.x < area.right + margin &&
                point.y > area.top - margin && point.y < area.bottom + margin;
 }
 
diff --git a/test/fixtures/controller.line/clip/false.js b/test/fixtures/controller.line/clip/false.js
new file mode 100644 (file)
index 0000000..1f86abd
--- /dev/null
@@ -0,0 +1,46 @@
+const data = [];
+for (let x = 0.95; x < 1.15; x += 0.002) {
+  data.push({x, y: x});
+}
+
+for (let x = 0.95; x < 1.15; x += 0.001) {
+  data.push({x, y: 2.1 - x});
+}
+
+module.exports = {
+  config: {
+    type: 'scatter',
+    data: {
+      datasets: [{
+        clip: false,
+        radius: 8,
+        borderWidth: 0,
+        backgroundColor: (ctx) => ctx.type !== 'data' || ctx.raw.x < 1 || ctx.raw.x > 1.1 ? 'rgba(255,0,0,0.7)' : 'rgba(0,0,255,0.05)',
+        data
+      }]
+    },
+    options: {
+      plugins: false,
+      scales: {
+        x: {
+          min: 1,
+          max: 1.1
+        },
+        y: {
+          min: 1,
+          max: 1.1
+        },
+      },
+      layout: {
+        padding: 32
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      height: 240,
+      width: 320
+    }
+  }
+};
diff --git a/test/fixtures/controller.line/clip/false.png b/test/fixtures/controller.line/clip/false.png
new file mode 100644 (file)
index 0000000..8b9347b
Binary files /dev/null and b/test/fixtures/controller.line/clip/false.png differ