mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
Fix: Arc getCenterPoint when full circle (#9152)
This commit is contained in:
parent
0d6fa55714
commit
1d047355e7
@ -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,
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user