Setting width and height

后端 未结 10 2046
广开言路
广开言路 2020-12-07 19:50

I\'m trying out the example code for Chart.js given in the docs.

Width and height is specified inline on the canvas element at 400px/400px.

But when renderin

相关标签:
10条回答
  • 2020-12-07 20:12

    Use this, it works fine.

    <canvas id="totalschart" style="height:400px;width: content-box;"></canvas>
    

    and under options,

    responsive:true,
    
    0 讨论(0)
  • 2020-12-07 20:23

    You can override the canvas style width !important ...

    canvas{
    
      width:1000px !important;
      height:600px !important;
    
    }
    

    also

    specify responsive:true, property under options..

    options: {
        responsive: true,
        maintainAspectRatio: false,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
    

    update under options added : maintainAspectRatio: false,

    link : http://codepen.io/theConstructor/pen/KMpqvo

    0 讨论(0)
  • 2020-12-07 20:27

    In my case, passing responsive: false under options solved the problem. I'm not sure why everybody is telling you to do the opposite, especially since true is the default.

    0 讨论(0)
  • 2020-12-07 20:27

    You can change the aspectRatio according to your needs:

    options:{
         aspectRatio:4 //(width/height)
    }
    
    0 讨论(0)
  • 2020-12-07 20:32

    Works for me too

    responsive:true
    maintainAspectRatio: false
    
    
    <div class="row">
            <div class="col-xs-12">
                <canvas id="mycanvas" width="500" height="300"></canvas>
            </div>
        </div>
    

    Thank You

    0 讨论(0)
  • 2020-12-07 20:32

    The below worked for me - but dont forget to put this in the "options" param.

    var myChart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        maintainAspectRatio: false,
        responsive:true,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
    });
    
    0 讨论(0)
提交回复
热议问题