Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Fix offset on doughnut charts

Open Zivangu9 opened this issue 1 year ago • 14 comments

Fix https://github.com/chartjs/Chart.js/issues/10465

Adding another half offset can fix the cropped graphics error.

Reproducible sample

I use the examples that doc-code-hub gave in the issue https://stackblitz.com/edit/web-platform-dr6rsv?file=js%2Fdata.js,index.html

Zivangu9 avatar Jul 06 '22 20:07 Zivangu9

Do you think the solution is correct, or should I look for a different one?

Zivangu9 avatar Jul 08 '22 17:07 Zivangu9

@Zivangu9 sorry, I didn't have internet yesterday

I think I understand where this is coming from. Would it be possible to add a test for the 2 arcs that are each 180 degrees? I'd be curious to see how this performs

etimberg avatar Jul 09 '22 11:07 etimberg

I was testing with two 180 degree arcs and now I understand a little better. The center offset is always 1/4, but the radius increment is 1/2 for 180 degree arcs, so when it's 2 180 arcs it's just 1.5 offset. When it is only one arc greater than 180 it becomes 1.25. And if there are no arcs greater than 180, the increment is 1 offset. https://stackblitz.com/edit/web-platform-dr6rsv?file=js%2Fdata.js,index.html

Zivangu9 avatar Jul 09 '22 15:07 Zivangu9

It almost looks like the angle changes should only apply to arcs that are not 180 degrees exactly.

etimberg avatar Jul 09 '22 17:07 etimberg

There is a jump at the end of the animation, when the arcs reach 180 degree circumference. That is another problem.

I think a proper fix would be getting rid of this kind of condition in the arc element.

kurkle avatar Jul 10 '22 19:07 kurkle

I experimented a little with it. Replaced

https://github.com/chartjs/Chart.js/blob/7432b609c487e1b17c8000fe9f94c58fa65936b8/src/elements/element.arc.js#L340-L342

with

      const fix = 1 + Math.sin(-Math.min(PI, circumference));
      radiusOffset = offset / 2 * fix;

to make the animations smooth for large arcs. @etimberg any thoughts?

https://stackblitz.com/edit/web-platform-ayr13m?file=js%2Fdata.js

kurkle avatar Jul 10 '22 21:07 kurkle

I experimented a little with it. Replaced

https://github.com/chartjs/Chart.js/blob/7432b609c487e1b17c8000fe9f94c58fa65936b8/src/elements/element.arc.js#L340-L342

with

      const fix = 1 + Math.sin(-Math.min(PI, circumference));
      radiusOffset = offset / 2 * fix;

to make the animations smooth for large arcs. @etimberg any thoughts?

https://stackblitz.com/edit/web-platform-ayr13m?file=js%2Fdata.js

The animation in that version looks really good 👍

etimberg avatar Jul 11 '22 00:07 etimberg

That seems like a better solution to the problem, do I make the changes in the PR?

Zivangu9 avatar Jul 11 '22 03:07 Zivangu9

sorry if I'm jumping in the thread. Issue #10220 was addressing the issue about hover offset (similar of the offset one). I didn't check the PR in deep, but I was thinking if make sense to address hoverOffset option and not only the offset one.

stockiNail avatar Jul 11 '22 07:07 stockiNail

@stockiNail probably the same issue.

kurkle avatar Jul 11 '22 08:07 kurkle

Since this can drastically change how some charts are shown I am not sure if this should be considered a bug fix or a breaking change

LeeLenaleee avatar Jul 11 '22 11:07 LeeLenaleee

Hmm, not sure if this is breaking or not. The previous behaviour was pretty buggy.

etimberg avatar Jul 27 '22 12:07 etimberg

That is true, but since we want to start working on V4 after 3.9 I think it will be the safer option to postpone it till V4

LeeLenaleee avatar Jul 27 '22 15:07 LeeLenaleee

Fair, if we aim to get a 3.9 out quickly this won't have to sit around for a while

etimberg avatar Jul 27 '22 18:07 etimberg