score:3
As from Docs for Chart.js it's recommended to wrap canvas into container div and change width/height of the container. But basically it's changing either by given width or height.
Found a more flexible custom solution from lannymcnie that can be used for any canvas responsiveness:
var stage = new createjs.Stage("canvas");
var c = new createjs.Shape();
c.graphics.f("#f00").dc(0,0,50); // Drawn a 100x100 circle from the center
var t = new createjs.Text("Resize the browser/frame to redraw", "24px Arial bold", "#000");
t.x = t.y = 20;
stage.addChild(c, t);
window.addEventListener("resize", handleResize);
function handleResize() {
var w = window.innerWidth-2; // -2 accounts for the border
var h = window.innerHeight-2;
stage.canvas.width = w;
stage.canvas.height = h;
//
var ratio = 100/100; // 100 is the width and height of the circle content.
var windowRatio = w/h;
var scale = w/100;
if (windowRatio > ratio) {
scale = h/100;
}
// Scale up to fit width or height
c.scaleX= c.scaleY = scale;
// Center the shape
c.x = w / 2;
c.y = h / 2;
stage.update();
}
handleResize(); // First draw
html, body {
padding: 0; margin: 0;
overflow:hidden;
}
canvas {
border: 1px solid #f00;
}
<script src="https://code.createjs.com/easeljs-0.8.2.min.js"></script>
<canvas id="canvas" width="800" height="600"></canvas>
score:0
You can have it responsive and set a different aspect ratio. My issue was that the chart tend to be squeezed on mobile device taking barely no height.
To remedy, with chart.js 3.2.1, I am using the AspectRatio option:
options: {
aspectRatio: 1,
}
This way the chart is square and stay square, you can play with this number usually between 0.5 to 2 to find a ratio that suits you on both displays.
score:12
- Create Class
.chart-container {
position: relative;
margin: auto;
height: 80vh;
width: 80vw;
}
- Create Div With class chart-container and place canvas tag inside it.
<div class="chart-container">
<canvas id="canvas"></canvas>
</div>
- Chart options, use property
maintainAspectRatio
set to false:
options: {
maintainAspectRatio: false,
responsive: true,
...
}
Source: stackoverflow.com
Related Query
- Chart.js responsive option - Chart not filling container
- Chart.js not height responsive
- Chart.js: width and height of a pie chart not respected
- Css style not working well when resizing chart height in angular application
- Responsive Chart.js Doughnut Chart with minimum height
- VueJs and ChartJs - Chart is responsive in width, but not height?
- Chartjs does not render chart when set responsive option to true
- Google Pie Chart Is Not Responsive
- chartjs - Not taking the height in reponsive chart
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- angular-chart.js - chart height not working on iOS but works fine on Android and Web
- How to set a responsive chart.js chart to not fill screen?
- how to not repeat code while creating multiple charts in chart js
- Set height of chart in Chart.js
- ReferenceError: Chart is not defined - chartjs
- Can't make chart js responsive
- Chart.js: bar chart first bar and last bar not displaying in full
- show label in tooltip but not in x axis for chartjs line chart
- Making the labels responsive in chart js
- Html chart does not fit a small Android WebView
- Chart.js ng2-charts colors in pie chart not showing
- Chart.JS Mixed Chart - Bars Not Showing
- chartjs height does not follow parent container
- Chart looks only grey, does not show the color - Chartjs,discordjs
- Angular-chart.js - Make line chart does not curve
- angular-chartjs line chart TypeError: t.merge is not a function
- Chart.JS full-width, responsive doughnut chart with Bootstrap
- Chart.js line chart is not displaying
- Uncaught TypeError: Chart is not a constructor when using Chart.js
- Line chart is not being displayed in Chart.js
More Query from same tag
- How to create a fixed number of y axes for a chartsjs linear graph?
- ChartJs: HorizontalBar Missing Background Colours
- Chart.js in AngularJS tabset does not render
- Chart.js move x axis labels to the right
- Cannot make my chart to work with data from mongoDB
- Passing the same props while rendering different elements conditionally in React.js
- Add data to dynamically created Chart.js dataset
- Are you required to make an http request when trying to use Chart.js?
- Chartjs - Stacked bar chart data order causes invisible other values
- Chart.js Date and Time Bar Chart Not Rendering - Line Works Though
- Chartjs not giving 'xLabel' properly on tooltip
- Chart Js Stacked Bar Chart with Multiple levels or categories
- Chart.js what is the new syntax for extending?
- Line chart with combination of fill and line
- i can't create a line vertically with react annotation
- Creating categories with different widths in chart.js
- How to achieve the best possible performance with mutable data and real-time charts in React?
- Show data values in Chart.js bars (version 3)
- Canvas size not changing
- How to ensure that a Chart.js line chart uses all space available on the y-axis?
- chartjs-plugin-datalabels not showing on stacked horizontal bar
- How to load a chart in a diferent page
- Angular 12 + Chartjs 3.3.2 TypeError: Cannot read property 'left'
- ChartJS Datalabels plugin and negative numbers
- Chartjs hovering over one Chart, shows tooltip across all charts
- Move tooltip further from data point for Chart.js?
- Chart.js v2.6: Add arrows to pie chart output values
- Chart.js combine two pieces of data into one bar
- Show base64 image next to canvas with charts.js
- Laravel - production.ERROR: Undefined variable: department_chart_data in Chartjs barchart