我正在尝试使用 Chart.js 库创建折线图。我有一个尺寸为 600px 宽 x 250px 高的 div
,根据我的阅读,该库旨在使用这些父尺寸创建折线图。
下面显示了我的 HTML 元素:
<div style="width:600px;height:250px">
<canvas id="myChart"></canvas>
</div>
这是我正在使用的代码:
$(document).ready(function(){
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
},
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}
]
};
var ctx = $("#myChart").get(0).getContext("2d");
new Chart(ctx).Line(data, {
responsive:true
});
});
这显示了我在 jsfiddle 中遇到的问题:https://jsfiddle.net/Lr88htnp/ (请注意,呈现的图表尺寸为 600x300)
最佳答案
您需要将选项 maintainAspectRatio
设置为 false
....
new Chart(ctx).Line(data, {
responsive:true,
maintainAspectRatio: false
});
fiddle - https://jsfiddle.net/3cxeyLc8/
关于javascript - Chart.js 不遵守我的容器尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32459948/