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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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>
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: