DEV Community

Cover image for Building a Beautiful, Performant Analytics Dashboard with React & Recharts (Part 2)
Alex
Alex

Posted on

Building a Beautiful, Performant Analytics Dashboard with React & Recharts (Part 2)

In Part 1 of this series, I broke down how I built the serverless, privacy-first backend for my analytics tool, Gridpoint Analytics, using the Cloudflare stack. But let's be honest: a powerful backend is useless if the frontend is a pain to use.

My goal was to create an analytics dashboard that people would actually want to open. It needed to be fast, intuitive, and present data with absolute clarity. Here’s a look at how I built the frontend with React, Recharts, and Tailwind CSS.

Choosing the Right Tools for the Job

The frontend stack was chosen for speed and developer experience:

  • React: For its component-based architecture and mature ecosystem. It allows for building a complex UI that is still easy to maintain.
  • Recharts: A composable charting library for React. It’s simple to get started with and flexible enough for creating beautiful, interactive charts.
  • Tailwind CSS: For styling. Utility-first CSS is a game-changer for building consistent, responsive designs without writing a ton of custom CSS.

The Charting Challenge: Bringing Data to Life

The centerpiece of any analytics dashboard is the chart. This component is responsible for fetching data from the API we built in Part 1 and rendering it visually.

Here’s a simplified version of my primary chart component. It fetches data when the component mounts or when the user changes the date range, and it handles the loading state to avoid a jarring user experience.

JavaScript

import React, { useState, useEffect } from 'react';
import { LineChart, Line, XAxis, YAxis, Tooltip, ResponsiveContainer } from 'recharts';

function AnalyticsChart({ siteId, dateRange }) {
  const [data, setData] = useState([]);
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    const fetchData = async () => {
      setIsLoading(true);
      // Fetch data from the Cloudflare Worker API we built in Part 1
      const response = await fetch('/api/get-chart-data', {
        method: 'POST',
        body: JSON.stringify({ siteId, dateRange }),
      });
      const result = await response.json();
      // Here you would format the result for the chart
      setData(result.formattedData);
      setIsLoading(false);
    };

    fetchData();
  }, [siteId, dateRange]); // Re-fetch when props change

  if (isLoading) {
    return <div>Loading Chart...</div>;
  }

  return (
    <ResponsiveContainer width="100%" height={300}>
      <LineChart data={data}>
        <XAxis dataKey="date" />
        <YAxis />
        <Tooltip />
        <Line type="monotone" dataKey="pageviews" stroke="#8884d8" strokeWidth={2} />
      </LineChart>
    </ResponsiveContainer>
  );
}
Enter fullscreen mode Exit fullscreen mode

Check out the official Recharts docs for more examples.

From Data to Direction: Displaying Insights

A core feature of Gridpoint Analytics is the "AI Signal Engine," which surfaces plain-English insights. On the frontend, this means taking the raw data from our API and presenting it in a clear, narrative format.

Instead of just showing a list of top referrers, I created a dedicated "Insights" component. It might take data points like { referrer: 't.co', views: 500, trend: '+35%' } and transform it into a human-readable sentence: "Traffic from Twitter is up 35% this week." This simple translation from data to direction is a huge step toward making analytics more accessible.

Conclusion: Tying It All Together

Building a product is a story in two parts: the powerful engine under the hood and the beautiful, intuitive vehicle you interact with. By combining a privacy-first serverless backend with a clean and performant React frontend, I was able to build the analytics tool I always wanted.

This two-part series covers the core of how Gridpoint Analytics was built. I hope it was helpful for anyone else on a similar journey. The best way to understand the philosophy is to see it in action.

You can check out the final result and try Gridpoint Analytics for free on our website.

Thanks for reading!

Top comments (0)