chartjs-quickref
Chart.js Quick Reference (v4.5.1)
Copy-paste snippets for common Chart.js patterns.
Data Formatting
Currency Labels
ticks: {
callback: (value) => '$' + value.toLocaleString()
}
Percentage Labels
ticks: {
callback: (value) => value + '%'
}
Abbreviated Numbers (1K, 1M)
ticks: {
callback: (value) => {
if (value >= 1000000) return (value / 1000000).toFixed(1) + 'M';
if (value >= 1000) return (value / 1000).toFixed(1) + 'K';
return value;
}
}
Date/Time Labels
// Requires: import 'chartjs-adapter-date-fns'; (or luxon, moment)
scales: {
x: {
type: 'time',
time: {
unit: 'day',
displayFormats: { day: 'MMM d' }
}
}
}
Common Configurations
Hide Legend
plugins: { legend: { display: false } }
Hide Tooltip
plugins: { tooltip: { enabled: false } }
Hide Title
plugins: { title: { display: false } }
Start Y-axis at Zero
scales: { y: { beginAtZero: true } }
Set Axis Range
scales: { y: { min: 0, max: 100 } }
Horizontal Bar Chart
type: 'bar',
options: { indexAxis: 'y' }
Disable Animation
animation: false
Stacked Bar/Line Chart
scales: {
x: { stacked: true },
y: { stacked: true }
}
Styling
Chart.js Default Palette
const colors = [
'rgb(255, 99, 132)', // red
'rgb(54, 162, 235)', // blue
'rgb(255, 206, 86)', // yellow
'rgb(75, 192, 192)', // teal
'rgb(153, 102, 255)', // purple
'rgb(255, 159, 64)' // orange
];
Semi-Transparent Backgrounds
backgroundColor: 'rgba(54, 162, 235, 0.5)',
borderColor: 'rgb(54, 162, 235)'
Hide Grid Lines
scales: {
x: { grid: { display: false } },
y: { grid: { display: false } }
}
Custom Font
Chart.defaults.font.family = "'Inter', sans-serif";
Chart.defaults.font.size = 14;
Responsiveness
Fill Container
options: {
responsive: true,
maintainAspectRatio: false
}
<div style="height: 400px;">
<canvas id="myChart"></canvas>
</div>
Fixed Aspect Ratio
options: {
responsive: true,
aspectRatio: 2 // width:height = 2:1
}
Square Chart
options: { aspectRatio: 1 }
Dynamic Updates
Update Data
chart.data.datasets[0].data = [1, 2, 3, 4, 5];
chart.update();
Update Without Animation
chart.update('none');
Add Data Point
chart.data.labels.push('New Label');
chart.data.datasets[0].data.push(42);
chart.update();
Remove Data Point
chart.data.labels.pop();
chart.data.datasets[0].data.pop();
chart.update();
Destroy Chart
chart.destroy();
Event Handling
Click Handler
options: {
onClick: (event, elements) => {
if (elements.length > 0) {
const index = elements[0].index;
console.log('Clicked:', chart.data.labels[index]);
}
}
}
Export as Image
const imageUrl = chart.toBase64Image();
Tree-Shaking Imports
Bar Chart
import { Chart, BarController, BarElement, CategoryScale, LinearScale } from 'chart.js';
Chart.register(BarController, BarElement, CategoryScale, LinearScale);
Line Chart
import { Chart, LineController, LineElement, PointElement, CategoryScale, LinearScale } from 'chart.js';
Chart.register(LineController, LineElement, PointElement, CategoryScale, LinearScale);
Pie/Doughnut Chart
import { Chart, PieController, ArcElement, Legend, Tooltip } from 'chart.js';
Chart.register(PieController, ArcElement, Legend, Tooltip);
// Use DoughnutController for doughnut charts
With Legend and Tooltip
import { Legend, Tooltip } from 'chart.js';
Chart.register(Legend, Tooltip);
Quick Start Templates
Minimal Bar Chart
new Chart(ctx, {
type: 'bar',
data: {
labels: ['A', 'B', 'C'],
datasets: [{ data: [10, 20, 30] }]
}
});
Minimal Line Chart
new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan', 'Feb', 'Mar'],
datasets: [{ data: [10, 20, 15] }]
}
});
Minimal Pie Chart
new Chart(ctx, {
type: 'pie',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{ data: [30, 50, 20] }]
}
});
More from sjnims/chartjs-expert
chartjs-plugins
This skill should be used when the user asks "Chart.js plugins", "custom Chart.js plugin", "Chart.js plugin hooks", "beforeDraw plugin", "afterDraw plugin", "Chart.js plugin API", "register Chart.js plugin", "Chart.js plugin options", "Chart.js plugin lifecycle", "Chart.js plugin TypeScript", or needs help creating custom plugins for Chart.js v4.5.1.
1chartjs-tooltips
This skill should be used when the user asks "Chart.js tooltips", "Chart.js tooltip callbacks", "custom Chart.js tooltip", "HTML tooltip Chart.js", "Chart.js tooltip formatting", "Chart.js tooltip position", "Chart.js tooltip label", "external tooltip", "Chart.js tooltip styling", or needs help configuring tooltips in Chart.js v4.5.1.
1chartjs-advanced
This skill should be used when the user asks "Chart.js gradients", "Chart.js linear gradient", "Chart.js radial gradient", "custom Chart.js chart type", "extend Chart.js", "derived chart type", "custom Chart.js scale", "Chart.js programmatic events", "Chart.js setActiveElements", "Chart.js custom controller", or needs help with advanced Chart.js v4.5.1 techniques.
1chartjs-axes
This skill should be used when the user asks "Chart.js axes", "Chart.js scales", "Chart.js x-axis", "Chart.js y-axis", "Chart.js time axis", "Chart.js logarithmic scale", "Chart.js axis labels", "Chart.js ticks", "Chart.js grid lines", "Chart.js multiple axes", "Chart.js dual axis", "Chart.js axis title", "Chart.js axis range", "Chart.js min max", "stacked axes", "Chart.js radial axis", or needs help configuring Chart.js v4.5.1 axes and scales.
1chartjs-overview
This skill should be used when the user asks "how to install Chart.js", "Chart.js setup", "getting started with Chart.js", "Chart.js CDN", "Chart.js npm install", "tree-shaking Chart.js", "Chart.js bundle optimization", "import Chart.js", "Chart.js module loaders", "Chart.js CommonJS", "Chart.js RequireJS", "chart.js/auto vs manual registration", "Chart.js defaults", "update chart data", "chart instance methods", "destroy chart", "Chart.js helpers", "resize chart", "responsive chart configuration", "Chart.js global configuration", "getRelativePosition", or needs help with initial Chart.js v4.5.1 project setup, configuration, and chart manipulation.
1chartjs-accessibility
This skill should be used when the user asks "Chart.js accessibility", "accessible charts", "WCAG Chart.js", "Chart.js ARIA labels", "Chart.js screen reader", "colorblind-safe charts", "Chart.js keyboard navigation", "Chart.js reduced motion", "prefers-reduced-motion Chart.js", "Chart.js alt text", "Chart.js color contrast", "accessible data visualization", "Chart.js focus management", "Chart.js fallback content", "aria-label canvas", or needs help making Chart.js visualizations accessible and WCAG-compliant in v4.5.1.
1