Skip to content

How to Implement Graphs in React Web Application using Nivo?

Graphs and data visualizations play a crucial role in conveying complex information in a visually appealing and understandable manner. Nivo is one such powerful and customizable data visualization library, which can be integrated with any modern framework. In this tutorial, we will explore how to implement graphs in a React web application using Nivo and how different types of components work together to make data visually appealing.

Getting Started with Nivo

Before we dive into the implementation details, let’s start by setting up a new React project and installing the necessary dependencies.

Step 1: Create a new React project

To create a new React project, you can use Create React App, a popular tool for bootstrapping React applications. Open your terminal and run the following command:

npx create-react-app my-graph-app

This command creates a new directory named `my-graph-app` with a basic React project structure.

Step 2: Install Nivo

Navigate to the project directory by running `cd my-graph-app`, and then install Nivo by executing the following command:

npm install @nivo/core @nivo/bar

This command installs two Nivo packages: `@nivo/core` and `@nivo/bar`. `@nivo/core` provides the core functionality of Nivo, while `@nivo/bar` specifically focuses on bar charts.

Now that we have our project set up and Nivo installed, let’s move on to implementing a bar chart in our React application.

Implementing a Bar Chart using Nivo

In this section, we will create a simple React component that renders a bar chart using Nivo. Create a new file named `BarChart.js` in the `src` directory and add the following code:

import React from 'react';
import { ResponsiveBar } from '@nivo/bar';
const BarChart = ({ data }) => {
const data = [
    { country: 'USA', population: 328_200_000 },
    { country: 'Brazil', population: 209_300_000 },
    { country: 'India', population: 1_366_400_000 },
    { country: 'China', population: 1_409_000_000 },
  ];
return (
    <div style={{ height: '500px' }}>
      <ResponsiveBar
        data={data}
        keys={['population']}
        indexBy="country"
        margin={{ top: 50, right: 50, bottom: 50, left: 50 }}
        padding={0.3}
        axisBottom={{
          tickSize: 5,
          tickPadding: 5,
          tickRotation: 0,
          legend: 'Labels',
          legendPosition: 'middle',
          legendOffset: 32,
        }}
axisLeft={{
          tickSize: 5,
          tickPadding: 5,
          tickRotation: 0,
          legend: 'Values',
          legendPosition: 'middle',
          legendOffset: -40,
        }}
        labelSkipWidth={12}
        labelSkipHeight={12}
        labelTextColor={{ from: 'color', modifiers: [['darker', 1.6]] }}
        legends={[
          {
            dataFrom: 'keys',
            anchor: 'top-right',
            direction: 'column',
            justify: false,
            translateX: 120,
            translateY: 0,
            itemsSpacing: 2,
            itemWidth: 100,
            itemHeight: 20,
            itemDirection: 'left-to-right',
            itemOpacity: 0.85,
            symbolSize: 20,
            effects: [
              {
                on: 'hover',
                style: {
                  itemOpacity: 1,
                },
              },
            ],
          },
        ]}
        animate={true}
        motionStiffness={90}
        motionDamping={15}
      />
    </div>
  );
};
export default BarChart;

We passed the data array to our ResponsiveBar component. The ResponsiveBar component needs an indexBy string to index the data and a keys property, which is an array of string to use to determine each series. We passed our population property as keys and indexed our data by country. 

After following these steps, we have a beautiful visualization component:

Customizing the Bar Chart

Nivo provides a wide range of customization options to tailor the appearance and behavior of your charts. Let’s explore a few common customization options you can use with Nivo’s bar charts.

Colors

You can customize the colors of the bars by specifying color schemes or providing custom color functions. Nivo offers various predefined color schemes, or you can create your own. Color schemes can be applied to different chart elements such as bars, legends, and tooltips.

Axis Configuration

Nivo allows you to customize the appearance and behavior of the X-axis and Y-axis. You can modify the tick formats, tick counts, orientations, labels, and other properties of the axes. This customization enables you to adapt the chart to your specific requirements.

Legends

Legends provide additional information about the data displayed in the chart. Nivo enables you to customize the position, orientation, style, and formatting of legends. You can show or hide legends and modify their appearance to match the overall design of your application.

Tooltips

Tooltips provide interactive information when users hover over chart elements. Nivo allows you to customize the content and appearance of tooltips. You can display specific data values, labels, or even pass your own custom functions.

Conclusion

Nivo provides many different components using which you can build visually compelling and interactive graphs and charts that enhance the user experience and enable effective data analysis. 

You can build other different types of charts besides the bar chart using the same techniques we applied here. The main motive was to demonstrate the seamless integration with React makes Nivo a go-to choice for developers seeking to create visually appealing data visualizations. 
Nivo is open source and the documentation is well written and concise. One can learn to make different visualizations and use some components in no time.

More like this:

Mastering React Router Dom for Seamless Navigation

Mastering React Router Dom for Seamless Navigation

In the world of web development, efficient navigation is crucial for creating seamless user experiences. React Router Dom…
Uncovering Next.js Essentials for Revolutionizing Web Development

Uncovering Next.js Essentials for Revolutionizing Web Development

Next.js, a robust open-source React framework, is designed to simplify modern web development. Its user-friendly interface and intuitive features…
Animations in React: A Dive into AOS, Anime JS, and Motion

Animations in React: A Dive into AOS, Anime JS, and Motion

Animation in ReactJs apps is a popular topic, and there are many ways to create different animations. Many…