Setting width and height

后端 未结 10 2047
广开言路
广开言路 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:34

    This helped in my case:

    options: {
        responsive: true,
        scales: {
            yAxes: [{
                display: true,
                ticks: {
                    min:0,
                    max:100
                }
            }]
        }
    }
    
    0 讨论(0)
  • 2020-12-07 20:36

    You can also simply surround the chart with container (according to official doc http://www.chartjs.org/docs/latest/general/responsive.html#important-note)

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

    CSS

    .chart-container {
        width: 1000px;
        height:600px
    }
    

    and with options

    responsive:true
    maintainAspectRatio: false
    
    0 讨论(0)
  • 2020-12-07 20:36

    Not mentioned above but using max-width or max-height on the canvas element is also a possibility.

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

    I cannot believe nobody talked about using a relative parent element.

    Code:

    <div class="chart-container" style="position: relative; height:40vh; width:80vw">
      <canvas id="chart"></canvas>
    </div>
    

    Sources: Official documentation

    0 讨论(0)
提交回复
热议问题