How to Build a React Dashboard with Chart.js: Tutorial

Written by

in

TL;DR: Build a React dashboard with Chart.js by scaffolding a React app, installing Chart.js and react-chartjs-2, creating reusable chart components that fetch and transform your data, then laying them out in a responsive grid. Keep charts modular, memoize data objects, and register only the Chart.js components you use to keep bundle size small.

Step 1: Set Up the Project

Create a new app with Vite (or Create React App) and install the charting dependencies: npm create vite@latest dashboard -- --template react, then npm install chart.js react-chartjs-2. The react-chartjs-2 wrapper handles canvas lifecycle for you, so you avoid manual refs and cleanup logic.

If you want to dig deeper, check out our guide on How to Plan a Slow Travel Trip with Rail-First Itineraries.

Step 2: Register Only What You Need

Chart.js is tree-shakeable. In a shared chartSetup.js file, import and register the controllers and elements you actually use — for example CategoryScale, LinearScale, BarElement, PointElement, LineElement, Tooltip, and Legend. Registering everything bloats your bundle; registering selectively can cut chart code significantly.

Step 3: Build a Reusable Chart Component

Create a ChartCard component that accepts title, type, data, and options props and renders a Bar, Line, or Doughnut from react-chartjs-2. Pass sensible defaults for responsive: true and maintainAspectRatio: false, and give the wrapper a fixed height via CSS so the canvas resizes predictably inside a grid.

Step 4: Fetch and Shape Data

Load data in a parent component with useEffect or a data library like TanStack Query, then map your API response into Chart.js’s { labels, datasets } shape. Handle loading and error states explicitly — render a skeleton while fetching and a fallback message on failure so the dashboard never shows a blank canvas.

Step 5: Lay Out the Dashboard

Arrange ChartCard instances in a CSS Grid with grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)) and a consistent gap. Add KPI tiles above the charts for headline numbers, and keep each card’s title and units visible so the dashboard reads clearly at a glance.

Tips

Memoize data and options objects with useMemo to prevent unnecessary re-renders. Define a shared color palette and reuse it across datasets for visual consistency. Use decimation or downsampling for large time-series data, and lazy-load below-the-fold charts with React.lazy to speed up first paint.

FAQ

Q: Should I use react-chartjs-2 or plain Chart.js?
A: Use react-chartjs-2 for React apps — it manages canvas creation, updates, and destruction automatically, while plain Chart.js requires manual refs and cleanup.

Q: Why is my chart not resizing with the container?
A: Set maintainAspectRatio: false in options and give the chart’s parent element an explicit height; without a sized parent, the canvas cannot calculate dimensions.

Q: How do I update a chart when data changes?
A: Pass new data

Related Articles

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *