- 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.
Change the name to “ChartDemo” and click on Ok.
Select Web API as its template.
Right-click the Models folder from Solution Explorer and go to Add >> New Item >> data.
Click on the ADO.NET Entity Data Model option and click Add.
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.
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.
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.
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.