Open In App
Related Articles

How to import recharts.js library to ReactJS file ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Recharts.js is a redefined chart library built with React and D3. It helps in creating interactive line graphs, bar graphs, pie graphs, etc. One of the main principles of it is that all its components are independent, lightweight, and can be deployed easily.

Prerequisites:

Steps to Create React Application and Installing Recharts Module

Step 1: Create a React application using the following command:

npx create-react-app foldername

Step 2: After creating your project folder i.e. folder name, move to it using the following command:

cd foldername

Step 3: Now move to the command line and install the rechart.js library

npm i recharts

Project Structure:

The updated dependencies in package.json file will look like

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"recharts": "^2.9.0",
"web-vitals": "^2.1.4"
}

Example: This example shows the basic implementation of Recharts.js libary by creating a simple graph from the Line and LineChart component.

Javascript

// Filename - App.js
 
import React from "react";
import {
    LineChart,
    Line,
    XAxis,
    YAxis,
    CartesianGrid,
    Tooltip,
    Legend,
    ResponsiveContainer,
} from "recharts";
 
const data = [
    {
        name: "Block A",
        l1: 4000,
        l2: 2400,
        amt: 2400,
    },
    {
        name: "Block B",
        l1: 3000,
        l2: 1398,
        amt: 2210,
    },
    {
        name: "Block C",
        l1: 2000,
        l2: 9800,
        amt: 2290,
    },
    {
        name: "Block D",
        l1: 2780,
        l2: 3908,
        amt: 2000,
    },
];
 
export default function App() {
    return (
        <div
            style={{
                width: "1100px",
                height: "600px",
                backgroundColor: "black",
            }}
        >
            <ResponsiveContainer width="100%" height="100%">
                <LineChart
                    width={500}
                    height={300}
                    data={data}
                    margin={{
                        top: 5,
                        right: 30,
                        left: 20,
                        bottom: 5,
                    }}
                >
                    <CartesianGrid strokeDasharray="3 3" />
                    <XAxis dataKey="name" />
                    <YAxis />
                    <Tooltip />
                    <Legend />
                    <Line
                        type="monotone"
                        dataKey="l2"
                        stroke="#8884d8"
                        activeDot={{ r: 8 }}
                    />
                    <Line
                        type="monotone"
                        dataKey="l1"
                        stroke="#82ca9d"
                    />
                </LineChart>
            </ResponsiveContainer>
        </div>
    );
}

                    

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start


Output: Now open your browser and go to http://localhost:3000/, you will see the following output.



Last Updated : 17 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads