作者:初语2265_781 | 来源:互联网 | 2023-05-24 20:01
我使用Chart.js和简单的折线图,但Chart.js计算的宽度和高度属性似乎是基于忽略填充的父元素的总宽度和高度.
var optiOns= {
maintainAspectRatio: false,
responsive: true
};
var data = {
labels: ["", "", "", "", "", "", ""],
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 ctx1 = document.getElementById("mychart1").getContext("2d");
var myNewChart = new Chart(ctx1).Line(data, options);
.container {
padding: 15px 15px 15px 15px;
width: 300px;
height: 200px;
border: 1px solid black;
}
.child {
display: inline-block;
border: 1px solid red;
width:100%;
height:100%;
}
的jsfiddle
第二个容器和子容器显示了我期待的行为.这是Chart.js如何计算画布的宽度和高度的错误,还是我造成了样式错误?
1> gwg..:
我还需要一个响应式画布并且遇到了这个问题.这是我的修复:
由于Chart.js将画布缩放到容器的宽度,忽略填充,我只是将画布包裹在一个div
.该div
尺度与填充容器,尊重填充,然后响应chart.js之canvas
扩展到了div
.