chartjs / chartjs-plugin-annotation

Annotation plugin for Chart.js
MIT License
607 stars 329 forks source link

New doughnutLabel does not take spacing into account #952

Open ThenTech opened 4 weeks ago

ThenTech commented 4 weeks ago

In #825, a centered doughnut label annotation was added, though the spacing option does not seem to reduce the label size, only the border and background sizes.

Ideally, I want to maximize the font size for the center label, while still keeping some padding around the label, so it doesn't hug the inside of the doughnut chart like it does now. Before the doughnutLabel annotation, I used my own plugin based on this StackOverflow answer, and there I can just add some padding around the label.

For example, with the text in blue, the background color in green, and in this case a spacing of 10, results in the background correctly getting padding, but the label is still the same size as if there was no spacing.

image


Additional bug: when increasing the spacing parameter until it becomes bigger than the innerRadius, results in the following error being thrown from the drawArc function:

IndexSizeError: Failed to execute 'arc' on 'CanvasRenderingContext2D': The radius provided (-38.5) is negative.

As used here (with _radius): https://github.com/chartjs/chartjs-plugin-annotation/blob/0d6246df894560217ed2bf5ce1ded050b400f3df/src/types/doughnutLabel.js#L188

Caused by this subtraction: https://github.com/chartjs/chartjs-plugin-annotation/blob/0d6246df894560217ed2bf5ce1ded050b400f3df/src/types/doughnutLabel.js#L139


On another note, this function: https://github.com/chartjs/chartjs-plugin-annotation/blob/0d6246df894560217ed2bf5ce1ded050b400f3df/src/types/doughnutLabel.js#L157-L160

could also use the built-in Math.hypot function, like so:

 function getFitRatio({width, height}, radius) { 
   return (radius * 2) / Math.hypot(width, height); 
 } 
stockiNail commented 2 weeks ago

@ThenTech thank you very much for feedback! I have prepared a PR https://github.com/chartjs/chartjs-plugin-annotation/pull/955. I need to test all possible cases (mainly when the circumference is <= 180 degrees. I'll set it "ready for review" asap.