From: Jukka Kurkela Date: Tue, 18 May 2021 23:24:23 +0000 (+0300) Subject: Fix doughnut rotation on float edge cases (#9121) X-Git-Tag: v3.3.0~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a553d57033f2d2d05020387d4b7abdbe3bff3505;p=thirdparty%2FChart.js.git Fix doughnut rotation on float edge cases (#9121) --- diff --git a/src/controllers/controller.doughnut.js b/src/controllers/controller.doughnut.js index 47e28b57b..d6c9c020b 100644 --- a/src/controllers/controller.doughnut.js +++ b/src/controllers/controller.doughnut.js @@ -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); diff --git a/src/helpers/helpers.math.js b/src/helpers/helpers.math.js index daf849818..d9e792c08 100644 --- a/src/helpers/helpers.math.js +++ b/src/helpers/helpers.math.js @@ -148,7 +148,7 @@ 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); @@ -156,7 +156,8 @@ export function _angleBetween(angle, start, end) { 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); } /** diff --git a/test/fixtures/controller.doughnut/doughnut-rotation-300.js b/test/fixtures/controller.doughnut/doughnut-rotation-300.js new file mode 100644 index 000000000..f4fa4242b --- /dev/null +++ b/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 + } + } +}; diff --git a/test/fixtures/controller.doughnut/doughnut-rotation-300.png b/test/fixtures/controller.doughnut/doughnut-rotation-300.png new file mode 100644 index 000000000..9b8bc86c1 Binary files /dev/null and b/test/fixtures/controller.doughnut/doughnut-rotation-300.png differ