Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing width of chart.js chart without changing height

I am using the sample of the line chart.

When I change width to another value here.

    <div style="width:30%">
        <div>
            <canvas id="canvas" height="450" width="600"></canvas>
        </div>
    </div>

The chart increases in height as well as width. I have tried specifying height too, but it never works. It always makes the height big too.

Does anyone know how to adjust the width while keeping the height the same?

like image 747
user3572565 Avatar asked Jan 22 '26 11:01

user3572565


1 Answers

If you are trying to make the chart fill the container (and use that for sizing) you need maintainAspectRatio = false in conjunction with responsive = true

var ctx = document.getElementById("canvas").getContext("2d");
var myLine1 = new Chart(ctx).Line(lineChartData1, {
    maintainAspectRatio: false,
    responsive: true
});

Your canvas height and width won't be respected in this case.

Fiddle - http://jsfiddle.net/s816dk83/

like image 152
potatopeelings Avatar answered Jan 25 '26 02:01

potatopeelings



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!