I was having some problem with chart.js legend. The legend with long text took up too much spaces which resulting in the reduce in the size of my pie chart:
Another example is this:
If I got more legend, the doughnut chart will eventually becomes smaller and smaller.
I tried to set maxWidth but to no avail.
var options = {
layout: {
padding: {
top: 5
}
},
responsive: true,
maintainAspectRatio: false,
legend: {
display: true,
position: 'right',
maxWidth: 100,
onClick: null
},
animation: {
animateScale: true,
animateRotate: true
},
};
Any ideas? Thanks in advanced!
I tried to follow this [solution][3] to create a html legend:
<div class="box-body" style="height:350px;">
<div class="float-left">
<div class="float-left" style="width:70%">
<canvas id="brandChart" style="position: relative; height: 350px;"></canvas>
</div>
<div class="float-left" style="width:30%">
<div id="js-legend" class="chart-legend">
</div>
</div>
</div>
It did restricted the width for legend, but then this lead to another problem which is after I collapsed and expanded the div, the canvas just went missing and I am only left with the legend div.
First off, set canvas's width and height using it's native attributes (do not use
style
attribute), like so :note: width should be twice the height
Then, set
responsive
property tofalse
in your chart options, as such :ᴅᴇᴍᴏ
This post is 9 months old but for the ones who are looking for a solution, use the following and this will make the pie/doughnut bigger in mobile/responsive view
Long legend nowrap is a known issue check https://github.com/chartjs/Chart.js/issues/3641
May be they can cover it in next releases. For now the solution is to remove the native legend and draw your custom one
I've created this plunk as an example for doghnut chart with custom legend https://embed.plnkr.co/5nuGS2KEV6hvESwGrOse/