How to Set the Size of Canvas for Every Type of Chart ?
Last Updated :
28 Apr, 2025
In this article, we will learn various approaches to setting the size of the canvas for different chart types using Chart.js. When you work with charting libraries like Chart.js, one common requirement is to customize the size of the canvas for different types of charts.
There are multiple ways to set the canvas size in Chart.js:
Chart.js CDN Link
Include the below chart.js CDN in your HTML document.
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
Using Inline Configuration
You can set the canvas size directly within the HTML canvas element using the width and height attributes.
Syntax:
<canvas id="myChart" width="400" height="200"></canvas>
Example: The below code uses the inine configuration to set size of canvas for every type of chart.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 0;
}
/* Center the canvas */
#chartContainer {
text-align: center;
}
</style>
<script src=
"https://cdn.jsdelivr.net/npm/chart.js">
</script>
<title>
Inline Configuration - Chart.js
</title>
</head>
<body>
<div id="chartContainer">
<!-- Canvas with Inline Configuration -->
<h1>GeeksforGeeks</h1>
<!-- Canvas -->
<canvas id="inlineChart"
width="800"
height="400">
</canvas>
<p>
Example of "Using Inline
Configuration"
</p>
</div>
<script>
// Chart Data
let data = {
labels:
['January', 'February', 'March', 'April', 'May'],
datasets: [{
label: 'Monthly Sales',
data: [12, 19, 3, 5, 2],
backgroundColor:
'rgba(75, 192, 192, 0.2)',
borderColor:
'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
};
// Chart Configuration
let config = {
type: 'bar',
data: data,
options: {
responsive: false,
maintainAspectRatio: false,
}
};
// Create Chart
let ctx = document.
getElementById('inlineChart').
getContext('2d');
let inlineChart = new Chart(ctx, config);
</script>
</body>
</html>
Output:
Output of Inline ConfigurationUsing JavaScript Options
Configure the canvas size by specifying options within the JavaScript code when initializing the chart.
Syntax:
options: {
responsive: false,
maintainAspectRatio: false,
// Set your size options here
aspectRatio: 2,
// or
width: 400,
height: 200
}
Example: The below code will show how you can add the options to set the canvas size for every type of chart.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<script src=
"https://cdn.jsdelivr.net/npm/chart.js">
</script>
<title>
JavaScript Options - Chart.js
</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 0;
}
/* Center the canvas */
#chartContainer {
text-align: center;
}
</style>
</head>
<body>
<div id="chartContainer">
<h1>GeeksforGeeks</h1>
<!-- Canvas for JavaScript Options -->
<canvas id="jsOptionsChart"
width="400"
height="200">
</canvas>
<p>
Example of "Using JavaScript Options"
</p>
</div>
<script>
// Chart Data
let data = {
labels:
['January', 'February', 'March', 'April', 'May'],
datasets: [{
label: 'Monthly Sales',
data: [8, 21, 3, 15, 2],
backgroundColor:
'rgba(75, 109, 192, 0.2)',
borderColor:
'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
};
// Chart Configuration with
// JavaScript Options
let config = {
type: 'bar',
data: data,
options: {
responsive: false,
maintainAspectRatio: false,
width: 400,
height: 200
}
};
// Create Chart
let ctx = document.
getElementById('jsOptionsChart').
getContext('2d');
let jsOptionsChart = new Chart(ctx, config);
</script>
</body>
</html>
Output:
Output for Using JavaScriptUsing the CSS Styling
Apply styles to the canvas element using CSS to control its size.
Syntax:
canvas {
width: 400px;
height: 200px;
}
Example: The below example explains the use of CSS styling to set the size of the canvas.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<script src=
"https://cdn.jsdelivr.net/npm/chart.js">
</script>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
/* CSS Styling for Canvas */
#chartContainer {
text-align: center;
}
#cssStyledChart {
width: 400px;
height: 200px;
}
</style>
<title>
CSS Styling - Chart.js
</title>
</head>
<body>
<div id="chartContainer">
<h1>GeeksforGeeks</h1>
<!-- Canvas with CSS Styling -->
<canvas id="cssStyledChart"></canvas>
<p>
Example of "CSS Styling" for Chart.js
</p>
</div>
<script>
// Chart Data
let data = {
labels:
['January', 'February', 'March', 'April', 'May'],
datasets: [{
label: 'Monthly Sales',
data: [12, 19, 3, 5, 2],
backgroundColor:
'rgba(75, 192, 192, 0.2)',
borderColor:
'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
};
// Chart Configuration
let config = {
type: 'bar',
data: data,
options: {
responsive: false,
maintainAspectRatio: false,
}
};
// Create Chart
let ctx = document.
getElementById('cssStyledChart').
getContext('2d');
let cssStyledChart = new Chart(ctx, config);
</script>
</body>
</html>
Output:
Output of using CSS styling
Similar Reads
How to Set Height and Width of a Chart in Chart.js ?
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.Ch
4 min read
How to Show Values on Top of Bars in Chart.js ?
In this article, we will learn how to show values on top of bars for a chart using the ChartJS CDN library.ApproachUse the <canvas> tag to show the bar graph in the HTML template.In the script section of the code, instantiate the ChartJS object by setting the type, data, and options properties
2 min read
How to Customize the Legend in Chart.js ?
In this article, we will learn how to customize the legend of a chart using the Chart JS CDN library. The chart legend displays data about the datasets that are appearing on the chart. This legend is customizable as per the users' requirements to enhance the look and feel of the chart in conjunction
5 min read
How to Dynamically Update Values of a Chart in ChartJS ?
Chart.js is an open-source free JavaScript library that is used to visualize data-informed charts. Dynamic chart updates are useful in creating interactive and real-time data visualizations. In this article, we will learn about how to dynamically update the values of charts. Using update() methodCha
5 min read
How to Add Text Inside the Doughnut Chart Using Chart.js ?
In Chart.JS, we have a doughnut chart to represent the data in slice forms for a more visual and attractive appearance. In the Doughnut Chart, we can place a custom text inside the doughnut chart to make the chart more informative and also help the readers understand the purpose or data doughnut cha
4 min read
How to Hide the X-Axis Label/text that is Displayed in Chart.js ?
Chart.js is a popular JavaScript library for creating interactive and visually appealing charts and graphs. By default, Chart.js displays text labels for both the x and y axes but in this article, we will see the different approaches to hiding the x-axis label/text that is displayed in chart.js.Char
3 min read
How to Format X Axis Time Scale Values in ChartJS ?
In Chart.js, formatting the x-axis time values involves searching for ways to present temporal data effectively. By default, Chart.js supports time series data and provides powerful options to customize the appearance of time labels on the x-axis. There are several approaches to format x-axis time s
4 min read
How to Create a Horizontal Scrolling Chart.js Line Chart with a Locked Y Axis ?
We will learn how to create a horizontal scrolling Chart.js line chart with a locked y-axis using the ChartJS CDN library.ApproachIn the HTML template, use two <canvas> tags: one to show the y-axis which should be locked in place, and the other to show the line chart itself.Create one <div
4 min read
How to Hide y Axis Line in ChartJs ?
In Chart.JS we can display our information in visual forms. For the customization of the chart, we can remove or hide the Y-axis line from the Chart appearance as per the requirement. We can use two different approaches to hide the y-axis line in the Chart.JS. We will see the practical implementatio
3 min read
How to Show Labels on Pie Chart in ChartJS ?
Pie charts are an attractive way to represent data in an easy-to-understand manner. In the pie chart, the labels are very useful because, with the help of the labels, we can understand what type of data is represented in the chart. Below are the different approaches to show labels on a pie chart: Ta
5 min read