score:91

Accepted answer

seems like var ctx = $('#mychart'); is returning a list of elements. you would need to reference the first by using ctx[0]. also height is a property, not a function.

i did it this way in my code:

var chartel = document.getelementbyid("mychart");
chartel.height = 500;

score:0

setting the size of a chartjs diagram can be simply achieved by setting the with of a surrounding container:

   <div style="width:400px;">
     <canvas id="mychart"></canvas>
   </div>

but an important detail is, that you may need to resize your diagram after creation:

var ctx = $('#mychart');
var mychart = new chart(ctx, {
    ......
    data: {
           ........
    },
    options: {
        responsive: false,
        maintainaspectratio: true
    }
});
// now resize the new chart to fit into the canvas....
mychart.resize();

score:0

for react, doing this works:

<doughnut
  data={data}
  height="200px"
  width="200px"
  options={{ maintainaspectratio: false }}
/>

score:0

the problem here is that maintainaspectratio needs to be inside the options node, not at the same level as in the example.

score:1

just to add on to the answer given by @numediaweb

in case you're banging your head against the wall because after following the instructions to set maintainaspectratio=false: i originally copied my code from an example i got on a website on using chart.js with angular 2+:

        <div style="display: block">
          <canvas basechart
                  [datasets]="chartdata"
                  [labels]="chartlabels"
                  [options]="chartoptions"
                  [legend]="chartlegend"
                  [colors]="chartcolors"
                  [charttype]="charttype">
          </canvas>
        </div>

to make this work correctly you must remove the embedded (and unnecessary) style="display: block" instead define a class for the enclosing div, and define its height in css.

once you do that, the chart should have responsive width but fixed height.

score:1

needed the chart to fill the parent element 100%, rather than setting height manually, and the problem was to force parent div to always fill remaining space.

after setting responsive and ratio options (check out related chartjs doc), the following css did the trick:

html

<div class="panel">
  <div class="chart-container">
    <canvas id="chart"></canvas>
  </div>
</div>

scss:

.panel {
  display: flex;

  .chart-container {
    position: relative;
    flex-grow: 1;
    min-height: 0;
  }
}

score:3

set the aspectratio property of the chart to 0 did the trick for me...

    var ctx = $('#mychart');

    ctx.height(500);

    var mychart = new chart(ctx, {
        type: 'horizontalbar',
        data: {
            labels: ["red", "blue", "yellow", "green", "purple", "orange"],
            datasets: [{
                label: '# of votes',
                data: [12, 19, 3, 5, 2, 3],
                backgroundcolor: [
                    'rgba(255, 99, 132, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(75, 192, 192, 0.2)',
                    'rgba(153, 102, 255, 0.2)',
                    'rgba(255, 159, 64, 0.2)'
                ],
                bordercolor: [
                    'rgba(255,99,132,1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)'
                ],
                borderwidth: 1
            }]
        },
        maintainaspectratio: false,
        options: {
            scales: {
                yaxes: [{
                    ticks: {
                        beginatzero:true
                    }
                }]
            }
        }
    });
mychart.aspectratio = 0;

score:4

you should use html height attribute for the canvas element as:

<div class="chart">
    <canvas id="mychart" height="100"></canvas>
</div>

score:6

he's right. if you want to stay with jquery you could do this

var ctx = $('#mychart')[0];
ctx.height = 500;

or

var ctx = $('#mychart');
ctx.attr('height',500);

score:7

i created a container and set it the desired height of the view port (depending on the number of charts or chart specific sizes):

.graph-container {
width: 100%;
height: 30vh;
}

to be dynamic to screen sizes i set the container as follows:

*small media devices specific styles*/
@media screen and (max-width: 800px) {
.graph-container {
        display: block;
        float: none;
        width: 100%;
        margin-top: 0px;
        margin-right:0px;
        margin-left:0px;
        height: auto;
    }
}

of course very important (as have been referred to numerous times) set the following option properties of your chart:

options:{
    maintainaspectratio: false,
    responsive: true,
}

score:11

you can also set the dimensions to the canvas

<canvas id="mychart" width="400" height="400"></canvas>

and then set the responsive options to false to always maintain the chart at the size specified.

options: {
    responsive: false,
}

score:15

this one worked for me:

i set the height from html

canvas#scorelinetoall.ct-chart.tab-pane.active[height="200"]
canvas#scorelineto3months.ct-chart.tab-pane[height="200"]
canvas#scorelinetoyear.ct-chart.tab-pane[height="200"]

then i disabled to maintaining aspect ratio

options: {
  responsive: true,
  maintainaspectratio: false,

score:21

you can wrap your canvas element in a parent div, relatively positioned, then give that div the height you want, setting maintainaspectratio: false in your options

//html
<div id="canvaswrapper" style="position: relative; height: 80vh/500px/whatever">
<canvas id="chart"></canvas>
</div>

<script>
new chart(somechart, {
options: {
    responsive: true,
    maintainaspectratio: false

/*, your other options*/

}
});
</script>

score:188

the easiest way is to create a container for the canvas and set its height:

<div style="height: 300px">
  <canvas id="chart"></canvas>
</div>

and set

options: {  
    responsive: true,
    maintainaspectratio: false
}

score:502

if you disable the maintain aspect ratio in options then it uses the available height:

var chart = new chart('blabla', {
    type: 'bar',
    data: {
    },
    options: {
        maintainaspectratio: false,
    }
});

Related Query

More Query from same tag