Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't make chart js responsive

Tags:

sorry if I missed anything to fix my issue, I've read and tried many solutions without any of them being adapted to the problem.

I have several charts (from chart.js) on a single page, but I can't succeed to make them responsive, despite :

responsive: true, 

The best responsive display I could get was when enlarging canvas width and height, but on the desktop version charts were displayed the entire screen.

Here is a fiddle.net

Anybody to help me ? Thank you very much.

like image 247
Blue Agency Avatar asked Feb 20 '17 09:02

Blue Agency


People also ask

What is responsive in chart JS?

Chart. js provides a few options to enable responsiveness and control the resize behavior of charts by detecting when the canvas display size changes and update the render size accordingly.

How do I set the size of a ChartJS?

To set the chart size in ChartJS, we recommend using the responsive option, which makes the Chart fill its container. You must wrap the chart canvas tag in a div in order for responsive to take effect. You cannot set the canvas element size directly with responsive .

Does chart JS work offline?

Is it possible to use chart. js for a desktop/mobile application using html that connects through an esp8266 access point or does it need a wifi connection? @marionboynton, CanvasJS is a standalone library that can work offline without any internet connection.


2 Answers

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)

like image 163
Blue Agency Avatar answered Oct 12 '22 01:10

Blue Agency


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.

Dasbhoard Example showing Reponsive Chart

like image 39
Krishna Vedula Avatar answered Oct 12 '22 03:10

Krishna Vedula