98π
The width and height property that you set for the canvas only work if the Chartjsβ responsive mode is false (which is true by default). Change your stats_tab.js to this and it will work.
window.onload=function(){
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [1,2,3,4,5,6,7,8,9,10],
datasets: [
{
label: "My First dataset",
data: [1,2,3,2,1,2,3,4,5,4]
}
]
},
options: {
responsive: false
}
});
}
69π
The important point is: width
and height
properties are not the size in px but the ratio.
<canvas id="myChart" width="400" height="400"></canvas>
In this example, itβs a 1:1 ratio. So, if your html contenair is 676 px width then your chart will be 676*675px
That can explain some common mistakes.
You can disable this feature by setting maintainAspectRatio
to false.
- [Chartjs]-In Chart.js set chart title, name of x axis and y axis?
- [Chartjs]-How to use two Y axes in Chart.js v2?
36π
In your options, set the maintainAspectRatio
to false
, and responsive
to true
. This will initially try to scale your chart to match the dimensions of your canvas. If the canvas doesnβt fit the screen, i.e. on mobiles, your chart will be re-scaled to fit on the page.
window.onload=function(){
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [1,2,3,4,5,6,7,8,9,10],
datasets: [
{
label: "My First dataset",
data: [1,2,3,2,1,2,3,4,5,4]
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
}
});
}
14π
I had this exact same problem where my graph would not resize properly. To get around this issue I did two things.
- Set the canvas tag style to a width and height of 100%. This will make sure that it always fits 100% of its containers width and height.
- For the options value set responsive: true & maintainAspectRatio: false. This will make sure that the graph responds to updates in size while ignoring the aspect ratio.
below is the code I used:
css
#myGraph {
width: 100%;
height: 100%;
}
html
<canvas id="myGraph" />
When you initialise the chart with the ctx set the options as below:
options: {
responsive: true,
maintainAspectRatio: false
}
now when you resize your screen the graph should resize accordingly.
Note: The graph will fill the size of its container now so what ever you put your canvas into it will fill the size of that container.
- [Chartjs]-Chart.js β drawing an arbitrary vertical line
- [Chartjs]-How to display data values on Chart.js
9π
A little late to the party, but I struggled with this problem a lot, particularly on a page with multiple charts.
This little tidbit solved it all β my charts all now fit nicely, and resize exactly with the divs they are in.
Add this to your page styles (The 8px is personal preference. Change if needed):
<style type="text/css">
#canvas-holder {
background-color: #FFFFFF;
position: absolute;
top: 8px;
left: 8px;
right: 8px;
bottom: 8px;
}
</style>
For the appropriate Divs:
<div id="canvas-holder">
<canvas id="chart-area"></canvas>
</div>
4π
This should work. Basically, just add the width and height properties to your javascript:
var ctx = document.getElementById("myChart").getContext("2d");
ctx.canvas.width = 200;
ctx.canvas.height = 200;
var myChart = new Chart(ctx,.........
Reference: Chart.js canvas resize
- [Chartjs]-Chart.js β How to set a line chart dataset as disabled on load
- [Chartjs]-Comparison between d3.js and chart.js (only for charts)
4π
Below code from Chart.js documentation worked for me.
βhttps://www.chartjs.org/docs/latest/general/responsive.htmlβ
var ctx = document.getElementById('myChart').getContext('2d');
ctx.canvas.parentNode.style.width = "500px";
ctx.canvas.parentNode.style.height = "500px";
var myChart = new Chart(ctx, .....
3π
If your <canvas>
element doesnβt have a width
& a height
βattributeβ (not the css attribute). Try setting each of them them to 1 (Since its just a ratio)
<canvas id="activeUsersPieChart" width="1" height="1"></canvas>
1π
The following worked for me, and I was able to keep the responsiveness of the chart!
var ctxx = document.getElementById("myChart");
ctxx.height = 80;
1π
The real issue is that the canvas will re-scale responsively to its parent. I wanted to keep the responsiveness. So something like this will solve the issue:
<div style="width:50%">
<canvas id="myChart"></canvas>
</div>
0π
-
Make a div element and place the chart canvas inside of it, name it as canvas-holder. See below:
<div id="canvas-holder"> <canvas id="myChart"></canvas> </div>
-
Using CSS, specify the height and width of the canvas holder
#canvas-holder { background-color: #FFFFFF; position: absolute; width: 400px; height: 400px; }
- [Chartjs]-Chart.js Failed to create chart: can't acquire context from the given item
- [Chartjs]-Charts.js Formatting Y Axis with both Currency and Thousands Separator
0π
I was having an issue with the chart scaling down without refresh. Per the chart.js documentation, a container with relative positioning and vh/vw assigned height was needed.
My chart was previously in a flex container and I think that was causing a lot of my scaling issues
Detecting when the canvas size changes can not be done directly from
the canvas element. Chart.js uses its parent container to update the
canvas render and display sizes. However, this method requires the
container to be relatively positioned and dedicated to the chart
canvas only. Responsiveness can then be achieved by setting relative
values for the container size
<div class="chart-container" style="position: relative; height:40vh; width:80vw">
<canvas id="chart" style="width:100%; height:100%;"></canvas>
</div>
https://www.chartjs.org/docs/latest/configuration/responsive.html