The Wayback Machine - https://web.archive.org/web/20240619002603/https://www.geeksforgeeks.org/how-to-set-height-and-width-of-a-chart-in-chart-js/
Open In App

How to Set Height and Width of a Chart in Chart.js ?

Last Updated : 09 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Chart.js is a popular JavaScript library that allows developers to create interactive and visually appealing charts for web applications. One of the key aspects of chart customization is controlling its dimensions. In this article, we’ll explore how to set the height and width of a Chart.js chart.

Chart.js CDN link

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

Approach 1: By using the style attribute

We can set the height and the width as CSS properties for the container containing the canvas of our chart inside the style attribute.

Syntax:

<div style=" height:40vh; width:80vw">
<canvas id="myChart"></canvas>
</div>

Example: The below code will explain the use of the style attribute to set the height and the width of the chart.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Chart.js - Set Height and Width</title>
    <script src=
      </script>
</head>
 
<body>
    <div>
        <div style="height:40vh;
                    width:80vw">
            <canvas id="myChart">
              </canvas>
        </div>
    </div>
 
    <script>
 
        var ctx = document.
        getElementById('myChart').
        getContext('2d');
 
 
        new Chart(ctx, {
            type: 'bar',
            data: {
                labels:
['January', 'February', 'March', 'April', 'May', 'June', 'July'],
                datasets: [{
                    label: 'My Dataset',
                    data:
              [65, 71, 62, 81, 34, 55, 47],
                    backgroundColor:
              'rgba(75, 192, 192, 0.2)',
                    borderColor:
              'rgba(75, 192, 192, 1)',
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });
    </script>
</body>
 
</html>


Output:

vwvh

Approach 2: By setting height and width as attribuutes

We can set the height and the width by passing them as attributes directly to the canvas of our chart. We also have to set responsive to false within options for width to work correctly.

Syntax:

 <canvas id="myChart" height="200" width="500"></canvas>
// In Options
options: {
responsive: false,
}

Example: The below example will show how you can set height nad width of the chart using the attributes.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Chart.js - Set Height and Width</title>
    <script src=
      </script>
</head>
 
<body>
    <div>
        <div>
            <canvas id="myChart"
                    height="300"
                    width="600">
              </canvas>
        </div>
    </div>
 
    <script>
 
        const ctx = document.
        getElementById('myChart').
        getContext('2d');
 
 
        new Chart(ctx, {
            type: 'bar',
            data: {
                labels:
['January', 'February', 'March', 'April', 'May', 'June', 'July'],
                datasets: [{
                    label: 'My Dataset',
                    data:
                [65, 71, 62, 81, 34, 55, 47],
                    backgroundColor:
                'rgba(75, 192, 192, 0.2)',
                    borderColor:
                'rgba(75, 192, 192, 1)',
                    borderWidth: 1
                }]
            },
            options: {
                responsive: false,
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });
    </script>
</body>
 
</html>


Output:

onCanvas

Appraoch 3: Using .resize() method

we can use .resize(width, height) method to resize our chart by invoking it using the two parameters as height and the width.

Syntax:

function resize() {
myChart.resize(700, 450);
};

Example: The below code example uses the resize() method to set height and width of the chart in chart.js.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>
      Chart.js Example
      </title>
    <script src=
      </script>
</head>
 
<body>
    <canvas id="myChart">
      </canvas>
 
    <script>
 
        var ctx = document.
        getElementById('myChart');
        var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels:
               ['Red', 'Blue', 'Yellow', 'Green'],
                datasets: [{
                    label: '# of Votes',
                    data: [12, 19, 3, 5]
                }]
            },
            options: {
                responsive: false,
            }
        });
 
 
        function resize() {
            myChart.resize(600, 450);
        };
        resize();
    </script>
</body>
 
</html>


Output:

resizewala



Similar Reads

HTML width/height Attribute vs CSS width/height Property
In HTML 5, few elements follow the width and height attributes and maximum elements do not follow this attribute. Like img, iframe, canvas, and svg kind of elements follow the width and height attributes but div, span, article and section type of elements don't follow them.The width and height attributes are affected in img, svg tags, those are wea
3 min read
How to Set Chart Title and Name of X Axis and Y Axis for a Chart in Chart.js ?
In this article, we will learn how to set the title, the name of the x-axis, and the name of the y-axis for a chart using the ChartJS CDN library. Approach:In the HTML template, use the &lt;canvas&gt; tag to show the line graph.In the script section of the code, instantiate the ChartJS object by setting the type, data, and options properties of the
3 min read
How to set width and height of background image in percent with respect to parent element in CSS ?
If we are using an image as a child and want to set the height and width in percentage then we need to set the parent element with some fixed size. Approach 1: Here we will use CSS inside the tags which are also known as inline CSS.For the parent div we will give a fixed size by giving height: 500px and width: 40% according to screen size, and we w
2 min read
How to dynamically set the height and width of a div element using jQuery ?
Set the height of a div element using jQuery The content height of a div can dynamically set or change using height(), innerHeight(), and outerHeight() methods depending upon the user requirement. If the user wants to change the content height of a div dynamically, it includes changing the actual height, actual height with padding, and actual heigh
7 min read
How to set the height and width of background image inline style in react?
In React, one can create an object with styling information like background image, height, width, etc and refer to the object in the style attribute in the HTML element. Since the inline CSS is written in a JavaScript object, properties with two names, like background-color, must be written with camel case syntax. Syntax : class MyHeader extends Re
2 min read
How to set the height and width of the video player in HTML5 ?
In this article, we will learn to set the width and height of the video player in HTML5. The following are the two scenarios The width and height of the video are fixed.Make the video respond to your screen width. Approach: The HTML5 width and height attributes of the video tag are used to set the height and width of the video player respectively.
2 min read
How to set browser width and height in Selenium WebDriver?
Using Selenium WebDriver, we can set the browser width and height. Various methods are available to set browser width and height. When we run any test, the browser will open in default size. So, if we need to change the size of the browser, we can do it using selenium WebDriver keywords. A few of them are listed below. Using maximize_window() Using
3 min read
How to set the width and height of an image using HTML ?
The height and width of an image can be set using the height and width attribute. The height and width can be set in terms of pixels. The &lt;img&gt; height attribute is used to set the height of the image in pixels. The &lt;img&gt; width attribute is used to set the width of the image in pixels. Note: Always mention the height and width of images
2 min read
How to Set Width and Height of Span Element using CSS ?
The &lt;span&gt; tag is used to apply styles or scripting to a specific part of text within a larger block of content. The &lt;span&gt; tag is an inline element, you may encounter scenarios where setting its width and height becomes necessary. This article explores various approaches to set the width and height of a &lt;span&gt; element using CSS.
2 min read
How to set the Width and Height in Tailwind CSS ?
Setting the width and height in Tailwind CSS involves using utility classes like w-[size] and h-[size]. These classes allow you to easily define the width and height of elements in your project. Setting WidthTo define the width of an element, employ the w-[size] classes, replacing [size] them To establish an element's heightwith options like 1/2, f
1 min read