Skip to content

Commit

Permalink
Fix doughnut rotation on float edge cases (#9121)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed May 18, 2021
1 parent ccff2fd commit a553d57
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/controllers/controller.doughnut.js
Expand Up @@ -20,8 +20,8 @@ function getRatioAndOffset(rotation, circumference, cutout) {
const startY = Math.sin(startAngle);
const endX = Math.cos(endAngle);
const endY = Math.sin(endAngle);
const calcMax = (angle, a, b) => _angleBetween(angle, startAngle, endAngle) ? 1 : Math.max(a, a * cutout, b, b * cutout);
const calcMin = (angle, a, b) => _angleBetween(angle, startAngle, endAngle) ? -1 : Math.min(a, a * cutout, b, b * cutout);
const calcMax = (angle, a, b) => _angleBetween(angle, startAngle, endAngle, true) ? 1 : Math.max(a, a * cutout, b, b * cutout);
const calcMin = (angle, a, b) => _angleBetween(angle, startAngle, endAngle, true) ? -1 : Math.min(a, a * cutout, b, b * cutout);
const maxX = calcMax(0, startX, endX);
const maxY = calcMax(HALF_PI, startY, endY);
const minX = calcMin(PI, startX, endX);
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/helpers.math.js
Expand Up @@ -148,15 +148,16 @@ export function _normalizeAngle(a) {
/**
* @private
*/
export function _angleBetween(angle, start, end) {
export function _angleBetween(angle, start, end, sameAngleIsFullCircle) {
const a = _normalizeAngle(angle);
const s = _normalizeAngle(start);
const e = _normalizeAngle(end);
const angleToStart = _normalizeAngle(s - a);
const angleToEnd = _normalizeAngle(e - a);
const startToAngle = _normalizeAngle(a - s);
const endToAngle = _normalizeAngle(a - e);
return a === s || a === e || (angleToStart > angleToEnd && startToAngle < endToAngle);
return a === s || a === e || (sameAngleIsFullCircle && s === e)
|| (angleToStart > angleToEnd && startToAngle < endToAngle);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions test/fixtures/controller.doughnut/doughnut-rotation-300.js
@@ -0,0 +1,28 @@
module.exports = {
config: {
type: 'doughnut',
data: {
labels: ['A', 'B', 'C', 'D', 'E'],
datasets: [{
data: [1, 5, 10, 50, 100],
backgroundColor: [
'rgba(255, 99, 132, 0.8)',
'rgba(54, 162, 235, 0.8)',
'rgba(255, 206, 86, 0.8)',
'rgba(75, 192, 192, 0.8)',
'rgba(153, 102, 255, 0.8)'
],
borderColor: [
'rgb(255, 99, 132)',
'rgb(54, 162, 235)',
'rgb(255, 206, 86)',
'rgb(75, 192, 192)',
'rgb(153, 102, 255)'
]
}]
},
options: {
rotation: 300
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a553d57

Please sign in to comment.