Open In App

Chart.js Failed To Create Chart: can't acquire context from the given item

Last Updated : 05 Aug, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report

Chart.js is a popular JavaScript library used for creating interactive and visually appealing charts on the web. One common error developers encounter when using Chart.js is: "Failed to create chart: can't acquire context from the given item." This error typically occurs when Chart.js fails to access the rendering context of the <canvas> element needed to draw the chart.

What Does This Error Mean?

To create a chart, Chart.js needs to acquire a 2D rendering context from a valid HTML <canvas> element. The error message "can't acquire context from the given item" means that Chart.js cannot find or access the <canvas> element or that the element is not set up correctly in the DOM.

Common Causes of the Error

  • Incorrect or Missing <canvas> Element: The <canvas> element is missing or improperly defined in the HTML.
  • Incorrect Element ID or Selector: The ID or selector used to reference the <canvas> element in the JavaScript code does not match any existing element in the DOM.
  • DOM Not Fully Loaded: The JavaScript code is executed before the DOM is fully loaded, so the <canvas> element is not yet available.
  • Element is Not a <canvas> Element: The element being referenced is not a <canvas> element, or the reference is incorrectly pointing to a different type of element.
  • JavaScript Errors or Syntax Issues: There could be other errors or issues in the JavaScript code that prevent the proper execution of Chart.js.

How to Resolve "Failed to create chart: can't acquire context from the given item"

1. Ensure the <canvas> Element is Correctly Defined

Make sure the HTML document has a properly defined <canvas> element with the correct ID. Here is an example:

<canvas id="myChart" width="400" height="400"></canvas>

2. Check the JavaScript Code for Correct Element Reference

Ensure the JavaScript code correctly references the <canvas> element by its ID or selector. Here is how you should define the chart initialization:

const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});

Make sure:

  • The ID in document.getElementById('myChart') matches the ID of the <canvas> element in the HTML (id="myChart").
  • The getContext('2d') method is called correctly to obtain the 2D rendering context.

3. Ensure the DOM is Fully Loaded Before Executing the JavaScript

If the JavaScript code runs before the DOM is fully loaded, the <canvas> element might not be accessible. You can wrap the Chart.js initialization code inside a DOMContentLoaded event listener to ensure it only runs after the DOM is ready:

document.addEventListener('DOMContentLoaded', function () {
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: { /* your data here */ },
options: { /* your options here */ }
});
});

4. Verify the Element is a <canvas> Element

Ensure that the element selected is actually a <canvas> element. For instance, avoid selecting elements like <div>, <span>, etc., which do not have a getContext() method.

5. Check for JavaScript Errors or Syntax Issues

Make sure there are no syntax errors or JavaScript exceptions in your code that might prevent the chart from rendering. Use browser developer tools (such as the console in Chrome or Firefox) to check for errors or warnings that might provide clues.

Example: Here’s an example of a correct setup for creating a chart using Chart.js:

HTML:

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>
</head>

<body>
    <canvas id="myChart" width="400" height="400"></canvas>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="app.js"></script> <!-- Your JavaScript file -->
</body>

</html>

JavaScript (app.js):

JavaScript
document.addEventListener('DOMContentLoaded', function () {
    const ctx = document.getElementById('myChart').getContext('2d');
    const myChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
            datasets: [{
                label: '# of Votes',
                data: [12, 19, 3, 5, 2, 3],
                backgroundColor: [
                    'rgba(255, 99, 132, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(75, 192, 192, 0.2)',
                    'rgba(153, 102, 255, 0.2)',
                    'rgba(255, 159, 64, 0.2)'
                ],
                borderColor: [
                    'rgba(255, 99, 132, 1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)'
                ],
                borderWidth: 1
            }]
        },
        options: {
            scales: {
                y: {
                    beginAtZero: true
                }
            }
        }
    });
});

Output:


Explore