Create Interactive Charts With Recharts

Using React and Recharts to create an interactive dashboard

Uğur Ertaş
Webtips
11 min readSep 27, 2020

--

Dashboard made with Recharts for this toturial
Dashboard made with Recharts for this tutorial

In this article, we will focus on a library named Recharts to create interactive 2d charts. For this, we will use the Covid-19 api to display not only Covid19 data to compare countries but also show historical data of each country. We will first create a table showing all the countries and use a click function to show the historical data of a country.

Setting up our developer environment

For this tutorial, we are going to use React. Instead of setting up your own React project, I have built a basic project so you can start off right away with coding instead of needing to configure your project. clone this repository and follow the steps below.

For this project we are going to use the following tools to create a Covid-19 dashboard:

  • Recharts — charts library to draw graphs.
  • Moment.js — Parse and display dates.
  • Bulma — CSS framework for styling and grid.
  • Sass CSS — easy reuse of CSS and mixins.

After you’re done you should see the following chart on your web-app:

First chart made with Recharts
First chart made with Recharts

Great now that is all set and done we can start building the dashboard. In your App.jsx you can see the following code that displays your chart. As you can see Recharts requires very little code to build a complex chart. First, we import the needed components we need for our chart. Because we used a Line Chart in this example we also needed to import Line Chart and Line. If you wanted to use for example an Area Chart you could replace them with AreaChart and Area. The YAxis is used to display the values onto the graph and the XAxes to spread them across horizontally. Later we will use the XAxes to display the dates so you can see what value occurs at which date. The Tooltip component gives you an entire tooltip without any extra code when you hover over every data point. On the Line element, you can also see a property named dataKey. With this, you can choose which field from your data you want to display on your chart. In this case, you can see that for the first line it shows the pv value for every Page and for the second line it uses the uv field.

Let’s try to change this chart so you can get an idea of how Recharts works. Try to replace Line with Area and LineChart with AreaChart. If you take a look at your chart now u should see the same data displayed with an area chart instead.

Adjusted chart using an Area graph
Adjusted chart using an Area graph

You can also play around with the data a bit by changing the values to see how it affects the chart. Below the chart, you can also see the legend that is displayed for each graph we draw representing the names of the dataset.

Now that we have an idea of how to draw a chart in Recharts let’s use this to display a chart of real API data instead of mock-up data. As stated above we will use the novelcovid19 API for this to display Covid-19 data of a single country.

To get the data from an API we need to store it first in a state. For this, we will use useState and useEffect from React like below

With useEffect we can call the FetchData() function which in turn will do an API call through the fetch method. In my case I choose but u can choose whatever country you prefer. By using SetData we can store the API data in the variable data. To be sure the API call works correctly we will console.logthe data. If done correctly you should get a similar output as below.

Output from Covid-19 API
Output from Covid-19 API

Now you are probably wondering why your chart does not show anything even though you have pointed to data in AreaChart. This is because Recharts also needs to know what values need to be displayed on the XAxis and the datakey for the Area let’s do that right now.

For the XAxes we can add Date as adatakey and in the Area, we will add a value to display the Covid-19 cases. In the output above you can see which values we can choose from. You can choose whichever you like. For example, if I choose Active I get all the active cases per day on my chart like below.

Covid-19 API data displayed in a Recharts chart
Covid-19 API data displayed in a Recharts chart

If done correctly you should see a chart appearing like the one above showing the Covid-19 cases. One thing that we do notice is that the date is not yet formatted correctly in the X-axes. This can be fixed by using moment.js so let’s do that quickly. so let’s import moment.js and then use moment to format the date to the desired format.

For this, we will create a function called CustomizedAxisTick. You can take a look at the site of moment.js to find the format you prefer. I choose the ll format because I like the shorthand notation of the month with the day. After formatting, I use slice to cut off the year since we don’t need that in this example.

After creating this function you can add another property to the XAxes called tick and add our new function to it as follows:

After adding this your chart should display the dates correctly like below. I have also changed the color to orange because it suits active cases more than the blue color.

Chart with correctly formatted dates
Chart with correctly formatted dates

Great now we have a working chart showing real data from an API. You can play around with this a little bit by changing the datakey for Area to see how it affects the chart. So now that we have your first working chart let’s add some interactivity to it.

So let’s first add a function to customize the styling and formatting of the tooltip.

Next to this, you will also need to add this styling to the style.scss to give the tooltip a clean look.

Now we can add the CustomTooltip function to the tooltip as follows. You can also add an animationDuration property to change the delay the tooltip has when you hover over to another value. I have set mine to zero for it to have no delay.

Now for some real interactivity, we are also going to add a brush component. This component allows the user to select only a certain range of the data with the help of a slider. To add this we first need to add another custom function to format the date correctly. Even though we are formatting the same date we used for the Tooltip we have to create another one because the Brush component doesn’t accept the HTML elements which are returned in the CustomizedAxisTick function.

Now let’s add the brush component to our chart. You can put it in the AreaChart.

After adding the Tooltip and the brush correctly your chart should work like below.

Chart with correctly the Brush component to zoom in on a specific data set of the chart
Chart with correctly the Brush component to zoom in on a specific data set of the chart

Your chart is now much more interesting because the brush allows the user to zoom in on a specific range of dates to make new observations. Next to this the updated tooltip now shows you the number of cases per day making it more obvious for the user to track the highlighted data. So now that we have a fully working chart with some interactivity let’s try to create a dashboard. We first want to create three columns above the charts showing the Covid-19 numbers for each type of case (active, recovered, and death).

Covid-19 numbers for each type of cases(active, recovered and death)
Covid-19 numbers for each type of cases(active, recovered and death)

Let’s first get the latest value of each case type. we can use the spread operator for this with Math.max for each value like below. next to that we can also get the country name with data.map and slice it to 1 so we only get a single value.

If you console.log() this you should get the latest values for each case type and the country name from the Covid-19 API. Next, we also need to add useState variables so we can let react know which button is pressed and what styling it should get accordingly.

Now we can set up our BUTTONS array.

After adding this we can use this array to loop over this array in our HTML.

also change the className“app” to ”dashboard” and add the dashboard.scss as an import to style.scss and remove the styling for the .App class. If you now take a look at the dashboard.scss you can see I have already added the styling for the columns and charts. I`ve already added the styling so we canfocus on Recharts for this tutorial instead of css.

Your dashboard should now look like the one below showing the latest figures for each case type.

Dashboard that changes the chart through buttons
Dashboard that changes the chart through buttons

As you can see whenever we click on the buttons it gets highlighted with a color and a box-shadow.

What we want to do next is to change the chart data by selecting a specific case type by pressing a button. You can see that we already added a setType to the onClick on the button which stores the value into the type variable. So all we need to do now is add this value to the AreaChart.

We can easily now add the type variable to the datakey property instead of a string. next to this, we can also add color to the fill and stroke value to also make the color of the chart dynamic.

You also now have a cool animation whenever you change the case type.

Added colors to the chart whenever a specific case type is selected
Added colors to the chart whenever a specific case type is selected

For the sake of keeping this tutorial simple, we will end it here and instead give you some homework to improve this dashboard. First of all, you can try to add multiple charts next to the existing one to create a real dashboard. Try to experiment with different chart types such as bar-charts, line-charts, and circle charts. You can add these charts in the same way we added the AreaChart. Apart from this, you can also add a drop-down menu that allows the user to switch countries. For this, you will need to add a parameter to the API call to make it dynamic. It would look something like this.

If you want some inspiration on how to improve your dashboard you can take a look at mine that I build for this tutorial as an example.

Resources used in this article…

--

--

Uğur Ertaş
Webtips

Front-end developer ugur-ertas.com #techNerd #environmentalist #vegetarian #dataviz 🇹🇷🇳🇱