score:17
the answer is :
1. add a div around the canvas
for example :
<div class="wrapper">
<canvas width="600" height="250"></canvas>
</div>
2. add height to the div class in the css
.wrapper {
height: 500px !important;
}
(adjust height as you wish the responsive rendering)
score:0
because of can't use media queries to set width
and height
, i set width and height attribute by detect mobile or desktop, like below
html
<canvas id="chart" width="{{width}}" height="{{height}}"></canvas>
javascript - angularjs
$scope.width = '';
$scope.height = '120';
if (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.useragent)) {
$scope.width = '100';
$scope.height = '100';
}
score:0
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"></canvas> </div>
the chart can also be programmatically resized by modifying the container size:
chart.canvas.parentnode.style.height = '128px';
score:1
i've found that with chart.js, you need a containing div where you manipulate the css(height/width), which will help the responsiveness - but not fully. there are some constraints especially with the width, as the containing div might leave a lot of whitespace on the bottom/sides, since the canvas won't fully adjust to the containing width as a percentage. it isn't perfect, but it works.
so, as a solution: make a containing div for each chart, and place it using css styles for this div :)
score:1
i tried all what the documents said but no chance,so i managed by my self:
<section style="padding: 0px">
<div class="chart-container" style="position: relative; margin: auto; height:167px; width: 1492px;">
<canvas id="graph"></canvas>
</div>
</section>
<script>
$(window).load(function() {
$( window ).resize(function() {
$('#graph').closest('.chart-container').css('width',$('#graph').closest('section').width());
});
});
</script>
the idea here to change the width or the height or both when the window resized.
score:2
instead of giving height and width attribute, set them through style:
<canvas id="mychart" style="height: 20rem"></canvas>
score:3
maintainaspectratio: false
sorted me out on a similar problem. the default value is true
.
score:4
i had a similar situation and based on chartjs documentation i had to add responsive: true
in my options
section, but it didn't solve my problem! finally i noticed that parent element of canvas
is flexbox
! removing that solved my problem!
p.s official documentation says that adding some style or width/height attributes to canvas tag is invalid! below is snippet from docs
the following examples do not work:
<canvas height="40vh" width="80vw">
: invalid values, the canvas doesn't resize (example)<canvas style="height:40vh; width:80vw">
: invalid behavior, the canvas is resized but becomes blurry
hope my answer can help someone!
score:5
you can make the container divs responsive using css and make svg width: 100%
and height:100%
here is updated fiddle fiddle.net
score:5
as i saw in fiddle, you have given fixed height and width to canvas tag.
try to give both height and width 70% or 80% for canvas tag. this may solve your problem.
100% may be give full large charts bt try to limit them to 70 to 80.
score:8
i know this is an old post, but recently came across it.
please check https://www.chartjs.org/docs/latest/general/responsive.html#important-note
it basically says that,
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".
note that in order for the above code to correctly resize the chart height, the maintainaspectratio option must also be set to false."
for example,
<div class="chart-container" style="position: relative; height:40vh; width:80vw">
<canvas id="chart"></canvas>
</div>
score:10
bootstrap has an example page, which has a chart (using chartjs) that is responsive. so, i would point to that url, which can be downloaded and we have a responsive chart.
Source: stackoverflow.com
Related Query
- Can't make chart js responsive
- How to make chartjs pie chart responsive
- How to make a 30days chart responsive on mobile using chart.js?
- How can I make two of my lines in Chart JS thicker
- How can I make a stepline or stepped chart in chart.js or D3?
- Chart.js responsive option - Chart not filling container
- Making the labels responsive in chart js
- Make pie chart smaller Chart JS
- Angular-chart.js - Make line chart does not curve
- Chart.JS full-width, responsive doughnut chart with Bootstrap
- how to make a chart.js bar chart scrollable
- How to make react-chartjs-2 responsive on mobile?
- how to programmatically make a line chart point active/highlighted
- How to make Chart JS responsive?
- Make months on x-axis clickable on chart.js line chart
- How to make bar chart animation where all bars grow at the same speed?
- chart.js responsive bar chart label sizing
- How to make a chart scroll horizontally (when using Chart.js)
- How can I make line on chart thinner?
- Is it possible to make points in a line chart of Chart JS 3.7.0 look like a donut?
- How to make a chart with an associative array with chart.js?
- ChartsJS make number in middle for only one chart
- ChartJS: how to make a bar chart with a horizontal guideline:
- Responsive Chart.js Doughnut Chart with minimum height
- How to make aspecific API call from within a Line Chart class in ReactJS using react-chartjs-2?
- Chart.js responsive pie chart
- Using two JSON objects to make Chart.JS Bar Chart Dynamic in Typescript / Angular
- chart.js: How do I make the radar chart axis labels bigger?
- Chart JS, ng2-Charts - How to make the label to the right of pie chart instead on top?
- How to make the chart smaller than its container
More Query from same tag
- Chartjs won't render chart with Requirejs
- ng2 charts bar chart need spacing between 2 bars in series Angular
- I'm getting the error "TypeError: Cannot read property 'extend' of undefined" when trying to import chartjs-plugin-datalabels
- Saving chart.js chart to a folder (Not download)
- Chart.js building dynamic charts, JSON data parsing issue
- I am Creating pie chart using Chartjs 2.6.0. I want to show label on Slices
- How to create dataset in chart.js dynamically using array?
- chart.JS time axis labels should be just in hours format
- ChartJs Bubble chart - on hover bubble becomes too big
- Incorrect time chart Chart.js
- After rendering one chart when trying to render a new one chart.js gives "Check that a complete date adapter is provided."
- chart js with angular2 loading dynamic data only after zoomin
- How to push an array into an Object which is inside of another array?
- Uncaught TypeError: Cannot read property 'draw' of undefined
- Charts.js Dynamic Ticks
- Chart JS Error : Uncaught TypeError: Cannot read property 'top' of undefined
- Chart.js with plotting durations against dates
- How to draw multiple line on y axis for same x axis in chartjs v2?
- Grouped bar chart with label in Chart.js
- Chartjs, scatter with For-Loop
- Integrating TideSDK with C *.dll
- vue-chartjs with chartkick vertical bar chart
- Chart.js - Multiple JSON data object array [{x: Date, y: count}....] representing each dataset of the chart
- create Chart using Chartjs and PHP
- Input data in german data format combined with ChartsJs
- problem hidding the label in chartjs (ng2chart for angular)
- ChartJS v2.6.0 unintended behaviour in Internet Explorer
- how do I change the textsize in of doughnut in react-chartjs-2?
- $(...).getContext("2d") is not a function when using chart.js
- Starting a Chart.js Time Scale at 0