The Wayback Machine - https://web.archive.org/web/20250527012105/https://www.geeksforgeeks.org/how-to-resize-the-chart-and-maintainaspectratio-in-chartjs/
Open In App

How to Resize the Chart and maintainAspectRatio in ChartJS?

Last Updated : 23 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Responsiveness in a web application is important for providing an optimal user experience across various devices and screen sizes. In Chart.js, the responsive property makes sure that charts adapt to different screen sizes automatically, while the maintainAspectRatio property allows you to control whether the chart maintains its original aspect ratio during resizing.

 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.min.js"></script>

These are the following approaches:

Using maintainAspectRatio and responsive Properties

In this approach, we are using the responsive and maintainAspectRatio properties in Chart.js to make sure the chart adjusts to different screen sizes while preserving its aspect ratio. The responsive: true property allows the chart to resize dynamically, and maintainAspectRatio: true keeps the chart's original aspect ratio intact.

Example: The below example uses maintainAspectRatio and responsive Properties to Resize the Chart and maintainAspectRatio in ChartJS.

HTML
<!DOCTYPE html>
<html>

<head>
    <title> Resize the Chart and Maintain AspectRatio</title>
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.min.js">
    </script>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            font-family: Arial, sans-serif;
        }

        h1 {
            color: green;
        }

        canvas {
            max-width: 400px;
            width: 100%;
            height: auto;
        }
    </style>
</head>

<body>
    <h3>Using maintainAspectRatio and responsive Properties</h3>
    <canvas id="barChart1"></canvas>

    <script>
        const data1 = {
            labels: ['January', 'February', 'March', 'April'],
            datasets: [{
                label: 'Monthly Sales',
                data: [30, 50, 70, 90],
                backgroundColor: '#ff5722'
            }]
        };

        const config1 = {
            type: 'bar',
            data: data1,
            options: {
                responsive: true,
                maintainAspectRatio: true,
                scales: {
                    x: {
                        beginAtZero: true
                    },
                    y: {
                        beginAtZero: true
                    }
                }
            }
        };

        const barChart1 = new Chart(
            document.getElementById('barChart1'),
            config1
        );
    </script>
</body>

</html>

Output:

Custom Aspect Ratio with aspectRatio Property

In this approach, we are using the aspectRatio property within the Chart.js configuration to set a custom aspect ratio for the chart. By specifying an aspect ratio of 2, the width of the chart will be twice its height.

Syntax:

options: {
responsive: true,
aspectRatio: 2, // Custom aspect ratio (width:height)
}

Example: The below example uses custom aspect ratio with aspectRatio Property to Resize the Chart and maintainAspectRatio in Chart JS.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Maintain aspect ratio</title>
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.min.js">
   </script>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            font-family: Arial, sans-serif;
        }

        h1 {
            color: green;
        }

        canvas {
            max-width: 400px;
            width: 100%;
            height: auto;
        }
    </style>
</head>

<body>
    <h3>Custom Aspect Ratio with `aspectRatio` Property</h3>
    <canvas id="lineChart2"></canvas>

    <script>
        const data2 = {
            labels: ['Q1', 'Q2', 'Q3', 'Q4'],
            datasets: [{
                label: 'Quarterly Revenue',
                data: [150, 200, 250, 300],
                borderColor: '#2196f3',
                fill: false
            }]
        };

        const config2 = {
            type: 'line',
            data: data2,
            options: {
                responsive: true,
                aspectRatio: 2, // Custom aspect ratio (width / height)
                scales: {
                    x: {
                        beginAtZero: true
                    },
                    y: {
                        beginAtZero: true
                    }
                }
            }
        };

        const lineChart2 = new Chart(
            document.getElementById('lineChart2'),
            config2
        );
    </script>
</body>

</html>

Output:


Next Article

Similar Reads