score:31
options: {
responsive: true,
maintainAspectRatio: false
}
It works perfectly with these options.
score:4
If you want to break the aspect ratio you can use the maintainAspectRatio
option from the docs. Your height and width should be taken from the canvas set values then or use css to set styles for the canvas element.
options: {
responsive: false,
maintainAspectRatio: false
}
score:6
This question has an accepted answer, but it was only part of the answer for me. What isn't clear from the documentation, at least to me, is that you must set the height of the parent element of the canvas, otherwise with maintainAspectRatio: false the height will be zero.
This is what worked for me using Bootstrap 4 cards (inline style for illustration):
<div class="card">
<div class="card-body" style="height: 400px;">
<canvas id="myChart"></canvas>
</div>
</div>
Then adding these this to the options:
options: {
maintainAspectRatio: false
}
By default responsive is set to true.
This also allows the easy use of responsive breakpoints and media queries to resize the chart.
score:11
To make it work, you also need to wrap your chart in a dedicated element in addition of using these options:
options: {
responsive: true,
maintainAspectRatio: false
}
Example:
<div>
<canvas id="xxx"></canvas>
</div>
See official documentation:
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.
Source: stackoverflow.com
Related Query
- Canvas static height Chartjs
- Truncating canvas labels in ChartJS while keeping the full label value in the tooltips
- ChartJS canvas not displaying rgba colors in IE, Safari and Firefox
- chartjs tooltip is cut off when out of canvas
- Chart.js ignoring canvas height & width
- ChartJS with AngularJS - Canvas won't display anything
- chartjs height does not follow parent container
- ChartJs canvas showing previous graph when changing Graph types
- Chartjs 2.0 - Increase height of x-axis labels
- How can I force my ChartJS canvas legend to stay in a single column?
- unable to add background color to the canvas using jspdf and chartjs
- How to add ChartJS code in Html2Pdf to view image
- ChartJS add text to canvas in linechart
- chartjs not following height when parent's height is 200px
- React chart : prevent chart's canvas scaling with height and width
- Why would a ChartJS chart end up with width 0 and height 0?
- canvas fill text vanishes when hovering over chartjs pie chart
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- can't set ChartJS canvas width
- I am using chartjs node canvas to render a chart however the graph options are being ignored
- ChartJS 2.6 - Bar chart fixed height scrollable-X
- Chartjs not working with d3 from csv source
- Chartjs 2.0 alpha, how to have a static scale for y-axis
- canvas does not display proper width and height chart.js
- HTML 5 chartjs not working as static file
- Chartjs graphs are not respecting height of container
- Bar chart with min height and zero values - ChartJs
- Full size download Chartjs canvas on mobile device
- Change height of chartjs dynamically
- Unable to set width and height on canvas in chart js
More Query from same tag
- ChartJS non overlapping lines
- How to set max smaller than min in ChartJS
- In ChartJS how do you format the hover over label in a time series?
- Chart.JS: How can I only display data labels when the doughnut size is big enough for the text?
- Plot lap times with Chart.js from time strings
- Showing border for columns in barchart of chart js on hover?
- Chart.js sciptable options: accessing the values of the data object?
- How can I prevent the hover data from displaying in Chart.JS?
- Set default line style in chart.js
- Showing only entire units on ChartJS
- React-Chart-JS-2 ^3.0.5 | TypeError: Cannot read properties of undefined (reading 'visible') at Chart._getSortedDatasetMetas
- Force ChartJS to show Doughnut chart with null values
- How to display axis borders only (no grid lines) in chartjs?
- How to create a charjs bar graph with many bars?
- How to show tick marks and labels with grid line for the specific one on the X axis with Chart.js?
- Can I offset the bars in chart.js stacked bar chart?
- How to add colored points with white shadow border in chart.js?
- Chart Js flickering or switching as i move mouse on canvas
- (Vue, ChartJS) Create gradient background for chart from child component canvas context
- How to change position of label of y-axes in chartjs?
- ng2-charts - bar chart, showing only one color
- Chart.Js - Display only specific (fixed) X axis labels
- Chart JS Error: Chart is not defined Firefox
- Grab array from div text to plot with ChartJS
- Chart.js doughnut chart tooltip size?
- Loading json file to chartjs
- Chart.js calculates the width of my time x-axis wrong
- math.max() doesn't work in IE
- Using chart.js with laravel passing data from controller to view
- chart.js onClick event function, access angular component