]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix: Arc getCenterPoint when full circle (#9152)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Mon, 24 May 2021 20:14:35 +0000 (23:14 +0300)
committerGitHub <noreply@github.com>
Mon, 24 May 2021 20:14:35 +0000 (16:14 -0400)
src/elements/element.arc.js
test/specs/element.arc.tests.js

index 3b8e500759fc28ca6454871af419b30eebc9aee5..93a4a88fd6bd7b271c98d2798b5bd0618ffaf7f0 100644 (file)
@@ -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,
index 5fe3339deec9e36b5d862d1162e11a4ec7538aec..dffb3f3fbaa8be5359e0c37ef251a81e90a64659 100644 (file)
@@ -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();