Sunday, October 4, 2026
HomePHPReact Charts and Graphs with Recharts: Visualize Knowledge Fantastically

React Charts and Graphs with Recharts: Visualize Knowledge Fantastically


Representing knowledge in a chart view is a large topic in Arithmetic and Pc Science. When you have the talent to rework uncooked numbers right into a chart view, it’s a superb illustration that makes customers perceive any complicated knowledge shortly.

Studying to construct a chart in React will elevate your UI and make your software really feel actually skilled.

This React instance contains dashboard visualization to show the next chart view.

  1. Gross sales & income pattern within the type of line chart.
  2. Product-wise efficiency utilizing column chart.
  3. Browser distribution in pie chart.
  4. Space chart to visualise customers’ lively time.

It makes use of Recharts library to point out the uncooked knowledge in a graph or chart kind, as proven under.

React Charts Graphs Recharts Data Visualization

Foremost React part containing chart parts

The DataVisualization part manages React states and useEffect. It manages the separate state variables for every sort of charts.

The React useEffect fetches the JSON knowledge and set them to bind for the charts.

The chart UI are created as separate parts and used within the dashboard wrapper.

src/parts/DataVisualization.jsx

import { useState, useEffect } from "react";
import Header from "../parts/Header";
import StatsCards from "../parts/StatsCards";
import LineChartBox from "../parts/LineChartBox";
import BarChartBox from "../parts/BarChartBox";
import PieChartBox from "../parts/PieChartBox";
import AreaChartBox from "../parts/AreaChartBox";
import Footer from "../parts/Footer";

const DataVisualization = () => {
  const [lineData, setLineData] = useState([]);
  const [barData, setBarData] = useState([]);
  const [pieData, setPieData] = useState([]);
  const [areaData, setAreaData] = useState([]);
  useEffect(() => {
    fetch("/knowledge/dashboard.json")
      .then((res) => res.json())
      .then((knowledge) => {
        setLineData(knowledge.lineData);
        setBarData(knowledge.barData);
        setPieData(knowledge.pieData);
        setAreaData(knowledge.areaData);
      })
      .catch((err) => console.error("Error loading knowledge:", err));
  }, []);
  return (
    <div className="dashboard-wrapper">
      <div className="dashboard-container">
        <Header />
        <StatsCards />
        <div className="charts-grid">
          <LineChartBox knowledge={lineData} />
          <BarChartBox knowledge={barData} />
          <PieChartBox knowledge={pieData} />
          <AreaChartBox knowledge={areaData} />
        </div>
        <Footer />
      </div>
    </div>
  );
};
export default DataVisualization;

Chart visualization header with trending knowledge playing cards

On this code, the chart parts are created for rendering in a dashboard. Added to the chart illustration, the dashboard exhibits playing cards to show trending knowledge.

You may exchange it together with your purposes progressive knowledge on this playing cards. For demonstration function, they’re all static knowledge from the client-side. It’s a matter of minute to plug your dynamic knowledge from the database.

react chart graph visualization header

src/parts/Header.jsx

const Header = () => (
  <header className="dashboard-header">
    <h1>Knowledge Visualization Dashboard</h1>
    <p>Lovely charts and graphs powered by Recharts in React</p>
  </header>
);
export default Header;

react chart graph statscards

src/parts/StatsCards.jsx

import { TrendingUp, BarChart3, Customers, Exercise } from "lucide-react";
const StatsCards = () => {
  const stats = [
    { icon: <TrendingUp strokeWidth={1} />, label: "Total Sales", value: "$24,850", change: "+12.5%" },
    { icon: <BarChart3 strokeWidth={1} />, label: "Revenue", value: "$48,200", change: "+8.2%" },
    { icon: <Users strokeWidth={1} />, label: "Users", value: "12,543", change: "+23.1%" },
    { icon: <Activity strokeWidth={1} />, label: "Growth", value: "34.8%", change: "+5.4%" },
  ];
  return (
    <div className="stats-grid">
      {stats.map((stat, index) => (
        <div key={index} className="stat-card">
          <div className="stat-top">
            <span className="stat-icon">{stat.icon}</span>
            <span className="stat-change">{stat.change}</span>
          </div>

          <p className="stat-label">{stat.label}</p>
          <p className="stat-value">{stat.worth}</p>
        </div>
      ))}
    </div>
  );
};
export default StatsCards;

Line chart to point out the gross sales income graph

This script imports the Recharts part required for rendering a line chart. The next parts are largely required for all of the charts.

  • XAxis, YAxis
  • Tooltip and Legend
  • CartisanGrid
  • ResponsiveContainer

The chart is rendered in a ResponsiveContainer block which assist chart scaling primarily based on the viewport measurement. The Tooltip and CartisanGrid will likely be proven on hovering the chart. The X,Y axis and chart legend are very normal a part of the chart view.

The LineChart and Line parts are unique to such a chart. It accepts Recharts attributes to set the stroke width and colour of the road graph.

react data visualization line chart

src/parts/LineChartBox.jsx

import { LineChart, Line, XAxis, YAxis, Tooltip, CartesianGrid, Legend, ResponsiveContainer } from "recharts";
const LineChartBox = ({ knowledge }) => (
  <div className="chart-card">
    <h2>Gross sales & Income Development</h2>
    <ResponsiveContainer width="100%" top={300}>
      <LineChart knowledge={knowledge}>
        <CartesianGrid strokeDasharray="3 3" stroke="#475569" />
        <XAxis stroke="#94a3b8" />
        <YAxis stroke="#94a3b8" />
        <Tooltip />
        <Legend />
        <Line sort="monotone" dataKey="gross sales" stroke="#3b82f6" strokeWidth={1} />
        <Line sort="monotone" dataKey="income" stroke="#10b981" strokeWidth={1} />
      </LineChart>
    </ResponsiveContainer>
  </div>
);
export default LineChartBox;

Bar chart fir displaying efficiency graph

The BarChart and Bar part of the Recharts library are used on this script. The BarChart accepts chart knowledge and Bar aspect requires the colour specification to fill the column bar.

src/parts/BarChartBox.jsx

import { BarChart, Bar, XAxis, YAxis, Tooltip, CartesianGrid, ResponsiveContainer } from "recharts";
const BarChartBox = ({ knowledge }) => (
  <div className="chart-card">
    <h2>Product Efficiency</h2>
    <ResponsiveContainer width="100%" top={300}>
      <BarChart knowledge={knowledge}>
        <CartesianGrid strokeDasharray="3 3" stroke="#475569" />
        <XAxis dataKey="class" stroke="#94a3b8" />
        <YAxis stroke="#94a3b8" />
        <Tooltip />
        <Bar dataKey="worth" fill="#8b5cf6" />
      </BarChart>
    </ResponsiveContainer>
  </div>
);
export default BarChartBox;

Pie chart – Recharts – displaying browser distribution

This Recharts part accepts the inner-radius, outer-radius, cy, cx properties to attract the pie chart.

This chart sort may have a Pie part which is to attract the pie wedges within the wrapper. The Cell is to attract every pie slices.

It receives the information and datakey within the <Pie /> part of this Recharts library.

react data visualization pie chart

src/parts/PieChartBox.jsx

import { PieChart, Pie, Tooltip, ResponsiveContainer, Cell } from "recharts";
const PieChartBox = ({ knowledge }) => (
  <div className="chart-card">
    <h2>Browser Distribution</h2>
    <ResponsiveContainer width="100%" top={300}>
      <PieChart>
        <Pie
          knowledge={knowledge}
          dataKey="worth"
          innerRadius={60}
          outerRadius={100}
          paddingAngle={2}
          cx="50%"
          cy="50%"
        >
          {knowledge.map((entry, i) => (
            <Cell key={i} fill={entry.colour} />
          ))}
        </Pie>
        <Tooltip />
      </PieChart>
    </ResponsiveContainer>
    <div className="pie-legend">
      {knowledge.map((merchandise, i) => (
        <div key={i} className="legend-item">
          <span className="legend-color" fashion={{ background: merchandise.colour }} />
          <span>{merchandise.title}: {merchandise.worth}%</span>
        </div>
      ))}
    </div>
  </div>
);
export default PieChartBox;

Space chart utilizing the Recharts library

On this graph, it exhibits the lively customers depend by time. It represents what number of customers are actively utilizing your app at a specific level of time. It can assist to watch the spikes and drop within the site visitors, product or companies usages and extra.

The AreaChart is the wrapper that comprises the Space aspect which is to point out the shaded a part of the chart as proven under.

The JSON knowledge comprises the time and consumer depend for every stage of the graph. The world is highlighted inside a LinearGadient definition.

react data visualization area chart

src/parts/AreaChartBox.jsx

import { AreaChart, Space, XAxis, YAxis, Tooltip, CartesianGrid, ResponsiveContainer } from "recharts";
const AreaChartBox = ({ knowledge }) => (
  <div className="chart-card">
    <h2>Lively Customers Over Time</h2>
    <ResponsiveContainer width="100%" top={300}>
      <AreaChart knowledge={knowledge}>
        <defs>
          <linearGradient id="colorUsers" x1="0" y1="0" x2="0" y2="1">
            <cease offset="5%" stopColor="#f59e0b" stopOpacity={0.8} />
            <cease offset="95%" stopColor="#f59e0b" stopOpacity={0} />
          </linearGradient>
        </defs>
        <CartesianGrid strokeDasharray="3 3" stroke="#475569" />
        <XAxis dataKey="time" stroke="#94a3b8" />
        <YAxis stroke="#94a3b8" />
        <Tooltip />
        <Space
          sort="monotone"
          dataKey="customers"
          stroke="#f59e0b"
          fill="url(#colorUsers)"
        />
      </AreaChart>
    </ResponsiveContainer>
  </div>
);
export default AreaChartBox;

react chart graph data visualization

Conclusion

The React charts present graphical view to grasp your trending knowledge. The road chart plots the efficiency vary over time, the pie chart slices down browser distribution at a fast view, and the world chart visualizes the consumer exercise patterns. By utilizing Recharts responsive wrapper, every graph match for various display measurement. General, these charts rework uncooked numbers into visible tales to assist environment friendly choice making.

References:

  1. Recharts library documentation
  2. Greatest chart libraries for React

Obtain

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments