> Chart.js
Build responsive charts and dashboards with Chart.js. Use when creating bar charts, line charts, pie charts, doughnut charts, radar charts, or real-time updating dashboards in JavaScript/TypeScript applications.
curl "https://skillshub.wtf/skillshub-team/gap-fillers/chartjs?format=md"Chart.js Skill
Use when building charts and dashboards with Chart.js (v4+).
Key Patterns
Installation
npm install chart.js
Basic Chart
import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);
const ctx = document.getElementById('myChart') as HTMLCanvasElement;
new Chart(ctx, {
type: 'bar', // 'line' | 'pie' | 'doughnut' | 'radar' | 'scatter' | 'bubble'
data: {
labels: ['Jan', 'Feb', 'Mar'],
datasets: [{
label: 'Revenue',
data: [12, 19, 3],
backgroundColor: 'rgba(54, 162, 235, 0.5)',
borderColor: 'rgb(54, 162, 235)',
borderWidth: 1
}]
},
options: {
responsive: true,
plugins: { legend: { position: 'top' }, title: { display: true, text: 'Monthly Revenue' } },
scales: { y: { beginAtZero: true } }
}
});
React Integration (react-chartjs-2)
import { Bar, Line, Pie } from 'react-chartjs-2';
import { Chart as ChartJS, CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend } from 'chart.js';
ChartJS.register(CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend);
export function Dashboard({ data }) {
return <Bar data={data} options={{ responsive: true, maintainAspectRatio: false }} />;
}
Real-time Updates
function addData(chart: Chart, label: string, newData: number) {
chart.data.labels!.push(label);
chart.data.datasets.forEach((dataset) => dataset.data.push(newData));
chart.update('none'); // skip animation for perf
}
Dashboard Layout Best Practices
- Use
responsive: trueandmaintainAspectRatio: falsefor grid layouts - Register only needed components for smaller bundle:
Chart.register(BarElement, CategoryScale, ...) - Use
chart.destroy()before recreating to prevent memory leaks - For 10k+ data points, use
decimationplugin or sample data
Plugin System
const customPlugin = {
id: 'customBackground',
beforeDraw: (chart: Chart) => {
const { ctx, chartArea } = chart;
ctx.save();
ctx.fillStyle = '#f0f0f0';
ctx.fillRect(chartArea.left, chartArea.top, chartArea.width, chartArea.height);
ctx.restore();
}
};
Common Pitfalls
- Canvas not found: ensure DOM is ready before creating chart
- Memory leaks: always call
chart.destroy()in cleanup/unmount - Tree-shaking: use named imports from 'chart.js' for smaller bundles
- SSR: Chart.js requires
<canvas>— use dynamic import ornext/dynamicwithssr: false
> related_skills --same-repo
> Node.js Security Review
Audit Node.js applications for security vulnerabilities. Use when reviewing npm dependencies, checking for injection attacks, securing Express/Fastify APIs, implementing auth, preventing prototype pollution, or hardening Node.js deployments.
> Docker Compose Orchestration
Orchestrate multi-container applications with Docker Compose. Use when defining services, networks, volumes, health checks, dependency ordering, environment configuration, or production deployments with docker compose.
> Rust CLI with Clap
Build command-line tools in Rust using clap for argument parsing. Use when creating CLI applications, argument parsers, subcommand handlers, or terminal tools with clap derive macros or builder patterns.