diff --git a/src/types/polygon.js b/src/types/polygon.js index 70d76d5c6..d2c528a66 100644 --- a/src/types/polygon.js +++ b/src/types/polygon.js @@ -3,8 +3,8 @@ import {PI, RAD_PER_DEG} from 'chart.js/helpers'; import {setBorderStyle, resolvePointPosition, getElementCenterPoint, setShadowStyle} from '../helpers'; export default class PolygonAnnotation extends Element { - inRange(x, y) { - return pointIsInPolygon(this.elements, x, y); + inRange(mouseX, mouseY, useFinalPosition) { + return this.elements.length > 1 && pointIsInPolygon(this.elements, mouseX, mouseY, useFinalPosition); } getCenterPoint(useFinalPosition) { @@ -38,7 +38,7 @@ export default class PolygonAnnotation extends Element { } resolveElementProperties(chart, options) { - const {x, y} = resolvePointPosition(chart, options); + const {x, y, width, height} = resolvePointPosition(chart, options); const {sides, radius, rotation, borderWidth} = options; const halfBorder = borderWidth / 2; const elements = []; @@ -54,11 +54,11 @@ export default class PolygonAnnotation extends Element { x: x + sin * radius, y: y - cos * radius, bX: x + sin * (radius + halfBorder), - by: y - cos * (radius + halfBorder) + bY: y - cos * (radius + halfBorder) } }); } - return {x, y, elements, initProperties: {x, y}}; + return {x, y, width, height, elements, initProperties: {x, y}}; } } @@ -101,13 +101,15 @@ PolygonAnnotation.defaultRoutes = { }; -function pointIsInPolygon(points, x, y) { +function pointIsInPolygon(points, x, y, useFinalPosition) { let isInside = false; - for (let i = 0, j = points.length - 1; i < points.length; j = i++) { - if ((points[i].bY > y) !== (points[j].bY > y) && - x < (points[j].bX - points[i].bX) * (y - points[i].bY) / (points[j].nY - points[i].nY) + points[i].bX) { + let A = points[points.length - 1].getProps(['bX', 'bY'], useFinalPosition); + for (const point of points) { + const B = point.getProps(['bX', 'bY'], useFinalPosition); + if ((B.bY > y) !== (A.bY > y) && x < (A.bX - B.bX) * (y - B.bY) / (A.bY - B.bY) + B.bX) { isInside = !isInside; } + A = B; } return isInside; } diff --git a/test/fixtures/box/labelPadding.js b/test/fixtures/box/labelPadding.js index 27bc467f9..393f1280a 100644 --- a/test/fixtures/box/labelPadding.js +++ b/test/fixtures/box/labelPadding.js @@ -23,7 +23,6 @@ module.exports = { yMax: 10, backgroundColor: 'rgba(255, 99, 132, 0.5)', borderColor: 'rgba(255, 99, 132)', - borderDash: [6, 6], borderWidth: 5, label: { enabled: true, @@ -40,7 +39,6 @@ module.exports = { yMax: 15, backgroundColor: 'rgba(255, 99, 132, 0.5)', borderColor: 'rgba(255, 99, 132)', - borderDash: [6, 6], borderWidth: 5, label: { enabled: true, @@ -57,7 +55,6 @@ module.exports = { yMax: 20, backgroundColor: 'rgba(255, 99, 132, 0.5)', borderColor: 'rgba(255, 99, 132)', - borderDash: [6, 6], borderWidth: 5, label: { enabled: true, @@ -74,7 +71,6 @@ module.exports = { yMax: 9, backgroundColor: 'rgba(255, 99, 132, 0.5)', borderColor: 'rgba(255, 99, 132)', - borderDash: [6, 6], borderWidth: 5, label: { enabled: true, diff --git a/test/fixtures/box/labelPadding.png b/test/fixtures/box/labelPadding.png index 7d71b90dc..b7bb450f2 100644 Binary files a/test/fixtures/box/labelPadding.png and b/test/fixtures/box/labelPadding.png differ