I'm currently utilizing Chart.js for a basic line chart, however I noticed that the width and height properties calculated by Chart.js appear to be based on the overall width and height of the parent element, disregarding any padding.
let options = {
maintainAspectRatio: false,
responsive: true
};
let 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]
}
]
};
let ctx1 = document.getElementById("mychart1").getContext("2d");
let myNewChart = new Chart(ctx1).Line(data, options);
.container {
padding: 15px;
width: 300px;
height: 200px;
border: 1px solid black;
}
.child {
display: inline-block;
border: 1px solid red;
width:100%;
height:100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.1.1/Chart.js"></script>
<div class='container'>
<canvas id='mychart1' class='child'></canvas>
</div>
<br>
<div class='container'>
<div class='child'>test</div>
</div>
The second container and child exhibit the behavior that I anticipated. Could this be a bug in how Chart.js calculates canvas width and height or is it possible that I am making a styling error?