简体   繁体   English

更改chart.js图表​​的宽度而不更改高度

[英]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? 有谁知道如何调整宽度同时保持高度相同?

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 如果您正在尝试使图表填充容器(并使用它来调整大小),则需要将maintainAspectRatio = false与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/ 小提琴 - http://jsfiddle.net/s816dk83/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM