Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js Doughnut Chart Sizing

I'm using ng2-charts in an Angular project. I have two versions of the project. The current production version is old and using an old version of ng2-charts (and, by extension, chart.js). I'm working on upgrading the project. I've run across something weird though.

I'm using the doughnut chart from ng2-charts, and the default size that the chart loads at in the old version is a 1:1 ratio:

enter image description here

You can see that it loads with a height and width of 240px. The weird thing is that in the newer version of ng2-charts/chart.js, the width to height ratio is 2:1:

enter image description here

I am using the same options in the new version, the same setup, etc. The only difference is the package version.

Is there a setting for Chart.js that would help me fix it? I like the size of the old chart, but can't get the new one to do that without allowing the canvas of the chart to bleed outside my component, which for obvious reasons I don't want.

like image 502
pjlamb12 Avatar asked Aug 26 '26 20:08

pjlamb12


1 Answers

Set aspectRatio: 1 in the options object provided to Chart.js to get what you're looking for. The default aspectRatio is 2 in the current version (there was a time when it was 1, but that was reverted to improve backwards compatibility). As you might guess, setting it to .5 causes the height of the element to be double that of the width.

Codepen (this pen taken from this Github issue)

like image 62
Peter G Avatar answered Aug 29 '26 17:08

Peter G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!