JS Chart.js
In this page:
new Chart(canvasElement, {
type: "bar",
data: { labels: [], datasets: [{ data: [] }] },
options: {}
});
Setting Up Chart.js
Chart.js is included via a <script> tag (from a CDN or a local copy), after which the global Chart constructor becomes available -- new Chart(canvasElement, config) then creates and renders a chart onto that canvas immediately.
Example: Setting Up Chart.js
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
// Draw a bar chart into the canvas, with two bars labeled "A" and "B"
new Chart(document.getElementById("myChart"), {
type: "bar",
data: { labels: ["A", "B"], datasets: [{ data: [10, 20] }] },
});
</script>
Chart Types
The type field in the configuration object selects the chart style -- "bar" for bar charts, "line" for line graphs, "pie" and "doughnut" for proportion charts, "radar" for multi-axis comparisons -- with the same data/options structure adapting to each.
Example: Chart Types
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
// Draw a pie chart into the canvas, split between "Yes" and "No"
new Chart(document.getElementById("myChart"), {
type: "pie",
data: { labels: ["Yes", "No"], datasets: [{ data: [70, 30] }] },
});
</script>
Configuring Data and Datasets
The data object holds labels (the categories shown along an axis or in a legend) and one or more datasets, each with its own values and visual styling (like backgroundColor) -- multiple datasets on one chart let you compare several data series at once.
Example: Configuring Data and Datasets
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
// Draw a bar chart comparing two datasets (2023 vs 2024) across Jan/Feb
new Chart(document.getElementById("myChart"), {
type: "bar",
data: {
labels: ["Jan", "Feb"],
datasets: [
{ label: "2023", data: [10, 20], backgroundColor: "blue" },
{ label: "2024", data: [15, 25], backgroundColor: "green" },
],
},
});
</script>
Updating a Chart Dynamically
A chart instance's .data property can be modified directly (like reassigning a dataset's data array), and calling .update() on the chart afterward redraws it with an animated transition to the new values, without needing to destroy and recreate the entire chart.
Example: Updating a Chart Dynamically
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const chart = new Chart(document.getElementById("myChart"), {
type: "bar",
data: { labels: ["A", "B"], datasets: [{ data: [10, 20] }] },
});
chart.data.datasets[0].data = [30, 5];
chart.update(); // animated transition to new values
</script>
Destroying a Chart Before Recreating It
Attempting to create a second Chart instance on the same canvas without first destroying the original produces overlapping or broken rendering -- calling .destroy() on the existing chart instance cleans it up properly before a new one is created.
Example: Destroying a Chart Before Recreating It
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
let chart = new Chart(document.getElementById("myChart"), {
type: "bar",
data: { labels: ["A"], datasets: [{ data: [5] }] },
});
chart.destroy(); // clean up before recreating
chart = new Chart(document.getElementById("myChart"), {
type: "line",
data: { labels: ["A"], datasets: [{ data: [10] }] },
});
</script>
- Forgetting to include the Chart.js script itself before using the Chart constructor, causing a "Chart is not defined" error.
- Passing a canvas element's ID as a string when the API expects the element itself (or vice versa) -- Chart.js accepts either, but mixing them up in your own helper code can cause confusion.
- Not calling .destroy() on an existing chart instance before creating a new one on the same canvas, leaving overlapping or ghost charts behind.
- new Chart(canvasElement, config) creates a chart, where config specifies the chart type, data, and options.
- Chart.js supports many built-in chart types (bar, line, pie, doughnut, radar, and more) via the type field.
- Updating a chart's data and calling .update() refreshes it with smooth transitions, without needing to recreate the whole chart.
Chart.js works in every modern browser supporting Canvas, which is effectively universal among current browsers.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: