score: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; }
score: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;
score: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>
score: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>
score: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
score: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, .....
score:8
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>
score:13
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.
score:28
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,
}
});
}
score:65
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.
score:89
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
}
});
}
Source: stackoverflow.com
Related Query
- Charts.js graph not scaling to canvas size
- Canvas size not changing
- chart js graph not showing on canvas
- The dataset for bar graph is not set properly using ng2 charts and ng5-slider in Angular
- The dataset in ng2 charts for bar graph is not setting properly
- Chart.js canvas rendered by ng-repeat does not show charts
- how to not repeat code while creating multiple charts in chart js
- Why is chart.js canvas not respecting the padding of the container element?
- ChartJS canvas not displaying rgba colors in IE, Safari and Firefox
- Bootstrap 3 tabs & Chart.js - charts not loading on tabs
- How to add images as labels to Canvas Charts using chart.js
- chartjs + Angular6 is not showing charts or any error
- Bootstrap grid not working with canvas
- Chart.js v2 charts do not display inside angular 2 (but Chart.js v1 works perfectly)
- Wkhtmltopdf does not render Chart.JS 2.5.0 graph
- Charts js not showing
- ChartJs canvas showing previous graph when changing Graph types
- Chart.js Bar graph will not start at zero as minimum value
- Chart.js - Multiple Doughnut Charts on same Canvas
- Last value not showing in bar graph of charts.js-library
- Multiple charts on same page not working - ng2-charts
- Chart.js pie chart not showing in Google Chrome canvas
- Chartjs Graph is not showing in colorbox
- ChartJS charts not generating within tabs
- ChartJS / MomentJS - Unable to remove deprecation warning. Graph not showing in firefox/opera
- Why chart.js charts are not plotting data in Safari (works in Chrome)
- Angular2 charts (chart.js) Labels not vertical even though option is set
- How do I keep chart.js charts in one JS file, and not get errors when the ID from the JS file don't exist on one specific html page?
- Chartjs (Non Vue) Not Rendering Graph Chart inside V-if/V-show
- charts are not being show with wicked_pdf
More Query from same tag
- Chart.js Example Code not working
- Adding vertical and horizontal scroll to Chart js Bar graph
- Chart.js canvas rendered by ng-repeat does not show charts
- Limit data points with chart js
- How to create a charjs bar graph with many bars?
- applying background-color in bar chart using chart.js
- Chart.js animate chart after clicking a button
- Display data labels on a pie chart in angular-chart.js
- Chart.js with React
- How to load chart data to an appended canvas
- How to Create Single line bubble Chartjs chart?
- Hide points in ChartJS LineGraph
- How to Change the Y axis of an Primefaces 7.0 ChartJS Linechart with Java?
- Chart.JS Vertical Line with Moment.JS Horizontal Axis
- ChartJS bar chart with legend which corresponds to each bar
- React COREUI CChart problem not rendering
- how to use chart.js in angular 7
- Jinja throughs "SyntaxError: expected property name, got '%' " error
- Change label color over a specific value in Chart.js
- Chart.js with factory and webservices (Ionic)
- Adding X axis title causes Uncaught RangeError: minimumFractionDigits value is out of range in Chart.js
- Chartjs isn't rendering bar chart, even though chart variable shows correctly when console logged
- Canvas not rendering after animation without resizing viewport
- How to fix page layout with embedded frame
- Chart.js multiple datasets on one y axis
- Why my ng2-chart data isn't scaled properly?
- The scale on the axis doesn't automatically narrow
- Dates not displaying on X Axis using Chart.Js on MVC 5
- Angular-ChartJS align legend right center
- Frontend and backend for chart using chartjs, mongodb, and controller