DEV Community

Cover image for Building a React-Based Analytics Dashboard from Scratch (3-Parts in One Guide)
Ali Dadashzadeh
Ali Dadashzadeh

Posted on

2 1 1

Building a React-Based Analytics Dashboard from Scratch (3-Parts in One Guide)

In this comprehensive guide, we’ll walk through building a React-based analytics dashboard from scratch. We’ll cover everything from project setup and data visualization to real-time updates. By the end, you’ll have a fully functional dashboard that you can customize and deploy for your own projects.

Let’s dive in!


Part 1: Setting Up the Foundation

Step 1: Initialize the Project

We’ll use Vite for a fast and modern React setup. Run the following commands to get started:

npm create vite@latest react-analytics-dashboard --template react
cd react-analytics-dashboard
npm install
npm run dev
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Essential Libraries

We’ll need a few libraries for routing, data fetching, and styling:

npm install react-router-dom axios tailwindcss chart.js react-chartjs-2
Enter fullscreen mode Exit fullscreen mode

Step 3: Set Up the Folder Structure

Organize your project like this:

src/
├── components/
│   ├── Header.jsx
│   ├── Sidebar.jsx
│   ├── Chart.jsx
├── pages/
│   ├── Dashboard.jsx
│   ├── Analytics.jsx
├── App.jsx
├── main.jsx
Enter fullscreen mode Exit fullscreen mode

Step 4: Create Basic Components

Here’s a simple Header and Sidebar component:

// src/components/Header.jsx
export default function Header() {
  return (
    <header className="bg-blue-500 text-white p-4">
      <h1 className="text-2xl font-bold">Analytics Dashboard</h1>
    </header>
  );
}
Enter fullscreen mode Exit fullscreen mode
// src/components/Sidebar.jsx
export default function Sidebar() {
  return (
    <aside className="bg-gray-800 text-white w-64 p-4">
      <nav>
        <ul>
          <li className="mb-2">
            <a href="/dashboard" className="hover:text-blue-500">Dashboard</a>
          </li>
          <li>
            <a href="/analytics" className="hover:text-blue-500">Analytics</a>
          </li>
        </ul>
      </nav>
    </aside>
  );
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Set Up Routing

Update App.jsx to include routing:

// src/App.jsx
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Header from './components/Header';
import Sidebar from './components/Sidebar';
import Dashboard from './pages/Dashboard';
import Analytics from './pages/Analytics';

export default function App() {
  return (
    <Router>
      <div className="flex">
        <Sidebar />
        <div className="flex-1">
          <Header />
          <Routes>
            <Route path="/dashboard" element={<Dashboard />} />
            <Route path="/analytics" element={<Analytics />} />
          </Routes>
        </div>
      </div>
    </Router>
  );
}
Enter fullscreen mode Exit fullscreen mode

Part 2: Data Visualization with Charting Libraries

Step 1: Create a Reusable Chart Component

We’ll use Chart.js and react-chartjs-2 for visualization. Here’s how to create a reusable Chart component:

// src/components/Chart.jsx
import { Bar } 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 default function Chart({ data, title }) {
  const options = {
    responsive: true,
    plugins: {
      legend: { position: 'top' },
      title: { display: true, text: title },
    },
  };

  return <Bar data={data} options={options} />;
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Add Sample Data to the Dashboard

Update the Dashboard.jsx page to display a bar chart:

// src/pages/Dashboard.jsx
import Chart from '../components/Chart';

const dashboardData = {
  labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  datasets: [
    {
      label: 'Sales',
      data: [65, 59, 80, 81, 56, 55, 40],
      backgroundColor: 'rgba(75, 192, 192, 0.2)',
      borderColor: 'rgba(75, 192, 192, 1)',
      borderWidth: 1,
    },
  ],
};

export default function Dashboard() {
  return (
    <div className="p-6">
      <h2 className="text-xl font-bold mb-4">Dashboard</h2>
      <Chart data={dashboardData} title="Monthly Sales Data" />
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Part 3: Real-Time Updates and Advanced Features

Step 1: Set Up Real-Time Data with WebSockets

We’ll use WebSockets to push real-time updates to the dashboard. First, install a WebSocket library:

npm install socket.io-client
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a WebSocket Connection

Update the Dashboard.jsx to include real-time data:

// src/pages/Dashboard.jsx
import { useEffect, useState } from 'react';
import io from 'socket.io-client';
import Chart from '../components/Chart';

const socket = io('http://localhost:4000'); // Replace with your backend URL

export default function Dashboard() {
  const [data, setData] = useState({
    labels: [],
    datasets: [{ label: 'Real-Time Sales', data: [], backgroundColor: 'rgba(255, 99, 132, 0.2)' }],
  });

  useEffect(() => {
    socket.on('dataUpdate', (newData) => {
      setData((prev) => ({
        labels: [...prev.labels, newData.label],
        datasets: [
          {
            ...prev.datasets[0],
            data: [...prev.datasets[0].data, newData.value],
          },
        ],
      }));
    });

    return () => socket.disconnect();
  }, []);

  return (
    <div className="p-6">
      <h2 className="text-xl font-bold mb-4">Real-Time Dashboard</h2>
      <Chart data={data} title="Real-Time Sales Data" />
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Deploy Your Dashboard

Deploy your dashboard using Vercel or Netlify:

  1. Push your code to GitHub.
  2. Connect your repository to Vercel/Netlify.
  3. Deploy!

Final Thoughts

Congratulations! You’ve built a React-based analytics dashboard with real-time updates. This project can be extended with features like user authentication, additional chart types, or integration with third-party APIs.


Happy coding! 🚀

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Image of PulumiUP 2025

From Cloud to Platforms: What Top Engineers Are Doing Differently

Hear insights from industry leaders about the current state and future of cloud and IaC, platform engineering, and security.

Save Your Spot

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay