In this article, we will learn how to create charts in a React application using the Chart.js library. Chart.js is an open-source JavaScript library for creating charts. Chart.js makes it easier to draw different types of charts like line, bar, doughnut, and area charts. In this article, we will create a line chart, bar chart, pie chart, and polar area using React and Web API. See chart.js’ docs for more information.

Prerequisites

  • Basic knowledge of ReactJS and Web API.
  • Visual Studio and Visual Studio Code can be installed.
  • SQL Server Management Studio.

This article covers:

  • Creating a database and table.
  • Creating an ASP.NET Web API Project.
  • Creating a ReactJS Project.
  • Installing the Chart.js library
  • Installing Bootstrap and Axios.
  • Adding routes in ReactJS.

Create a Table in the Database

Open SQL Server Management Studio, create a database named, “DemoTest,” and in this database, create a table. Give that table a name like “Ipltopscorer.”

Add some demo data into this table.

You may also like:
Cube.js: Ultimate Guide to the Open-Source Dashboard Framework.

Create a New Asp.net Web API Project

Open Visual Studio and create a new project.

Creating a new project

Change the name to “ChartDemo” and click on Ok.

Changing project name to "ChartDemo"

Select Web API as its template.

Selecting Web API as a template

Right-click the Models folder from Solution Explorer and go to Add >> New Item >> data.

Adding new item

Click on the ADO.NET Entity Data Model option and click Add.

Selecting ADO.NET Entity Data Model

Select EF Designer from the database and click the Next button.

Add the connection properties and select the database name on the next page and click OK.

Check the Table checkbox. The internal options will be selected by default. Now, click the Finish button.

Selecting Table checkbox

Our data model is successfully created now. Right-click on the Controllers folder and add a new controller. Name it “Chartcontroller.”

Add the following namespace in the Charts Controller.

Add a new method to fetch data from the database.

Complete the Chartcontroller code.

Now, let’s enable Cors. Go to Tools, open NuGet Package Manager, search for Cors, and install the Microsoft.Asp.Net.WebApi.Cors package.

Create a ReactJS Project

Now create a new React.js project by using the following command:

Open the newly created project in Visual Studio code.

Install the Chart.js Library

Now, install the Chart.js library in the React project by typing the following command:

Install Bootstrap in this project by using the following command:

Now, open the index.js file and import Bootstrap.

Now, install the Axios library by using the following command. Learn more about Axios fromt their docs.

Now go to src folder and add 5 new components.

  1. Linechart.js.
  2. Barchart.js.
  3. Doughnut.js.
  4. Piechart.js.
  5. Polararea.js.

Now, open Linechart.js, import Line chart from the Chart.js library, and add the following code.

Now, open Barchart.js and add following code:

Now, open Doughnut.js and add the following code:

Now, open Piechart.js and add the following code:

Now, open Polararea.js and add the following code:

Add Routing in ReactJS

Install the react-router-dom package by using the following command:

Now, open app.js file and import all five components and the React router component. Add the following code in the app.js file:

Now, run the project by using npm start command and check the result. Click on the buttons to see each chart.

Line chart

Pie chart

Doughnut chart

Bar graph

Polar chart

Summary

In this article, we learned about Chart.js and how to add it in a ReactJS applications to create charts. In this article, we discussed the line chart, bar chart, pie chart, doughnut chart, and polar area chart. We can also use other Chart libraries in ReactJS to create more visualizations. 

Further Reading



Source link

Write A Comment