Fix: Arc getCenterPoint when full circle (#9152)

This commit is contained in:
Jukka Kurkela 2021-05-24 23:14:35 +03:00 committed by GitHub
parent 0d6fa55714
commit 1d047355e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 3 deletions

View 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,

View 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();