Chart.js
Overview
Chart.js is a simple, flexible open-source JavaScript charting library that renders to an HTML5 <canvas>. It ships eight core chart types plus animations, tooltips, and responsive resizing out of the box, with a plugin and scale system for extension. Read this when you need clean, interactive charts fast and don't require the fully custom control of a low-level library like D3.
Official sources
- Docs: https://www.chartjs.org/docs/latest/
- Repo: https://github.com/chartjs/Chart.js
- Install: https://www.npmjs.com/package/chart.js
Install / setup
npm install chart.js
Command from the official installation guide (https://www.chartjs.org/docs/latest/getting-started/installation.html); CDN builds are available on jsDelivr (https://cdn.jsdelivr.net/npm/chart.js) and CDNJS.
Core concepts
- Chart instance —
new Chart(ctx, config)mounts a chart onto a canvas 2D context. - Config object —
{ type, data, options }declaratively describes the chart. - Datasets — each
data.datasets[]entry holds values, labels, colors, and per-dataset options. - Scales — cartesian (
x/y) and radial axes configured underoptions.scales. - Tree-shaking — register only the controllers/elements you use, or import
chart.js/autofor everything. - Plugins — lifecycle hooks (
beforeDraw,afterEvent) extend rendering; many community plugins exist. - Responsive — charts resize to their container by default (
responsive: true). - Animations — transitions are configurable per property under
options.animation.
Best practices
- Import from
chart.js/autofor prototypes, but register components explicitly in production to tree-shake (https://www.chartjs.org/docs/latest/getting-started/integration.html). - Give the canvas a sized wrapper element and avoid hard-coding width/height for responsiveness (https://www.chartjs.org/docs/latest/configuration/responsive.html).
- Call
chart.update()after mutatingchart.data, andchart.destroy()before recreating to avoid leaks (https://www.chartjs.org/docs/latest/developers/api.html). - Use official framework wrappers (react-chartjs-2, vue-chartjs) instead of manually bridging lifecycles.
Common pitfalls
- "category is not a registered scale" error → you tree-shook too aggressively; register the needed scales/controllers or use
chart.js/auto. - Chart grows infinitely tall in a flex/grid parent → set
maintainAspectRatio: falseand size the wrapper. - Re-rendering over an existing canvas → destroy the previous instance first (
chart.destroy()).
Examples
import { Chart } from "chart.js/auto";
const ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, {
type: "bar",
data: {
labels: ["Jan", "Feb", "Mar", "Apr"],
datasets: [{ label: "Sales", data: [12, 19, 7, 15], backgroundColor: "steelblue" }]
},
options: { responsive: true, scales: { y: { beginAtZero: true } } }
});
Further reading
- https://www.chartjs.org/docs/latest/charts/line.html — per-chart-type configuration reference.
- https://www.chartjs.org/docs/latest/samples/ — official runnable samples.
Related skills
- ../d3 — drop down to D3 when you need fully custom, non-standard charts.
- ../plotly — alternative when you need scientific/3D/statistical chart types.