From: Jukka Kurkela Date: Mon, 24 May 2021 20:14:35 +0000 (+0300) Subject: Fix: Arc getCenterPoint when full circle (#9152) X-Git-Tag: v3.3.1~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d047355e7d602a14b0439ee08ac1bd998dc4638;p=thirdparty%2FChart.js.git Fix: Arc getCenterPoint when full circle (#9152) --- diff --git a/src/elements/element.arc.js b/src/elements/element.arc.js index 3b8e50075..93a4a88fd 100644 --- a/src/elements/element.arc.js +++ b/src/elements/element.arc.js @@ -278,15 +278,16 @@ export default class ArcElement extends Element { * @param {boolean} [useFinalPosition] */ getCenterPoint(useFinalPosition) { - const {x, y, startAngle, endAngle, innerRadius, outerRadius} = this.getProps([ + const {x, y, startAngle, endAngle, innerRadius, outerRadius, circumference} = this.getProps([ 'x', 'y', 'startAngle', 'endAngle', 'innerRadius', - 'outerRadius' + 'outerRadius', + 'circumference' ], useFinalPosition); - const halfAngle = (startAngle + endAngle) / 2; + const halfAngle = isNaN(circumference) ? (startAngle + endAngle) / 2 : startAngle + circumference / 2; const halfRadius = (innerRadius + outerRadius) / 2; return { x: x + Math.cos(halfAngle) * halfRadius, diff --git a/test/specs/element.arc.tests.js b/test/specs/element.arc.tests.js index 5fe3339de..dffb3f3fb 100644 --- a/test/specs/element.arc.tests.js +++ b/test/specs/element.arc.tests.js @@ -66,6 +66,39 @@ describe('Arc element tests', function() { expect(center.y).toBeCloseTo(0.5, 6); }); + it ('should get the center of full circle using endAngle', function() { + // Mock out the arc as if the controller put it there + var arc = new Chart.elements.ArcElement({ + startAngle: 0, + endAngle: Math.PI * 2, + x: 2, + y: 2, + innerRadius: 0, + outerRadius: 2 + }); + + var center = arc.getCenterPoint(); + expect(center.x).toBeCloseTo(1, 6); + expect(center.y).toBeCloseTo(2, 6); + }); + + it ('should get the center of full circle using circumference', function() { + // Mock out the arc as if the controller put it there + var arc = new Chart.elements.ArcElement({ + startAngle: 0, + endAngle: 0, + x: 2, + y: 2, + innerRadius: 0, + outerRadius: 2, + circumference: Math.PI * 2 + }); + + var center = arc.getCenterPoint(); + expect(center.x).toBeCloseTo(1, 6); + expect(center.y).toBeCloseTo(2, 6); + }); + it('should not draw when radius < 0', function() { var ctx = window.createMockContext();