Introduction
Hi everyone, in this time of crisis let's do something for society. Let's make a COVID-19 tracking website which can show real-time data in the form of charts. Also, we will add filters as per the country.
Features of the website: Easy to use, Attractive UI, Charts speaks more than text, real-time data, and last but not least, it is a responsive website for both desktop & mobile view.
Tools & language used: VS Code, Node.js (has to be installed), React, Material.UI & Chart.js
This is what our final product will look like:
- 3 cards showing Infected, Recovered & Deaths count
- Line chart representing the global count, separating the infected & death count line over the last 3 months.
- Filter as per country, the selected country's results will be displayed by the bar chart.
- Mobile view: responsive to both Auto-Rotate.
- Cards in the mobile view.
- The bar chart in the mobile view.
Let's get into coding.
Step 1
Install VS-Code & Node.js to begin.
Step 2
Create an empty folder and name as you desire: e.g. covid19tracker
Step 3
Open VS code go to the terminal then navigate to the folder that you just created
Now type this command in PowerShell : npx create-react-app ./ (this will create an empty React app in into your folder).
After successful installation, delete everything inside the src folder. Let's build everything from scratch.
Step 4
Add index.js
- import React from 'react';
- import ReactDom from 'react-dom';
- import App from './App';
- //first component is App, second parameter is the element we want to render it on
- ReactDom.render(<App />, document.getElementById('root'));
Let's Add App.js in the src folder.
- import React from 'react';
- class App extends React.Component {
- render(){
- return(
- <H1>Covid-19</H1>
- )
- }
- }
- export default App;
Let's install all dependencies.
- we need Axios: this will make a get request to an API
- react-chartjs-2: libraries for making charts
- react-countup: Animation for counting numbers
After successful installation, use this command: npm start, It will run your application on localhost: port 3000
Step 7
Now create a Components folder: and create 3 new folders inside the component folder.
Cards, 2. Charts & 3.CountryPicker: each folder is responsible for its own style & component
Go inside 1st folder Cards and create Cards.jsx & Cards.module.css, then follow the same with both of the folders follow the proper naming conventions. It is very important to load CSS.
Also, create a folder name API & Add index.js inside the API folder, as shown.
Step 8
API/Index.js: it loads Axios, which is responsible for REST calls. As you can see, we are making 3 different calls for charts, cards & country picker.
- import axios from 'axios';
- const url ='https://covid19.mathdro.id/api';
- export const fetchData = async (country) => {
- let changableurl = url;
- if(country){
- changableurl = `${url}/countries/${country}`
- }
- try {
- const { data: {confirmed, recovered, deaths, lastUpdate} } = await axios.get(changableurl);
- return {confirmed, recovered, deaths, lastUpdate};
- }
- catch(error){
- console.log(error);
- }
- };
- export const fetchDailyData = async () =>{
- try{
- const {data} = await axios.get(`${url}/daily`);
- return data.map(({ confirmed, deaths, reportDate: date }) => ({ confirmed: confirmed.total, deaths: deaths.total, date }));
- }
- catch(error){
- return error
- }
- };
- export const fetchCountries = async () =>{
- try{
- const {data:{countries}} = await axios.get(`${url}/countries`);
- return countries.map((country)=> country.name);
- }
- catch(error){
- return error
- }
- };
src\Components\Cards\Cards.jsx : This js loads 3 cards : Infected, Recovered & deaths on the top of the website.
- import React from 'react';
- import styles from './Cards.module.css'
- import {Card, CardContent, Typography, Grid, StylesProvider} from '@material-ui/core';
- import CountUp from 'react-countup';
- import cx from 'classnames';
- import Button from '@material-ui/core/Button';
- const Cards = ({ data: { confirmed, recovered, deaths, lastUpdate } } ) => {
- if(!confirmed){
- return 'Please wait..';
- }
- console.log(lastUpdate);
- return (
- <div className={styles.container}>
- <Grid container spacing = {3} justify="center">
- <Grid style={{backgroundColor: 'rgba(208, 208, 241, 0.5)'}} item component={Card} xs={12} md={3} className={cx(styles.card, styles.infected)}>
- <CardContent >
- <Typography color="textSecondary" gutterBottom>Infected</Typography>
- <Typography variant="h5" >
- <CountUp start={0} end={confirmed.value} duration={3} separator="," />
- </Typography>
- <Typography color="textSecondary">{new Date(lastUpdate).toDateString()}</Typography>
- <Typography variant="body2">Number of active cases of Covid-19</Typography>
- </CardContent>
- </Grid>
- <Grid style={{backgroundColor: 'rgba(188, 253, 188, 0.5)'}} item component={Card} xs={12} md={3} className={cx(styles.card, styles.recovered)}>
- <CardContent>
- <Typography color="textSecondary" gutterBottom>Recovered</Typography>
- <Typography variant="h5" >
- <CountUp start={0} end={recovered.value} duration={3} separator="," />
- </Typography>
- <Typography color="textSecondary" >{new Date(lastUpdate).toDateString()}</Typography>
- <Typography variant="body2">Number of recoveries from Covid-19</Typography>
- </CardContent>
- </Grid>
- <Grid style={{backgroundColor: 'rgba(245, 192, 192, 0.5)'}} item component={Card} xs={12} md={3} className={cx(styles.card, styles.deaths)}>
- <CardContent>
- <Typography color="textSecondary" gutterBottom>Deaths</Typography>
- <Typography variant="h5" >
- <CountUp start={0} end={deaths.value} duration={3} separator="," />
- </Typography>
- <Typography color="textSecondary" >{new Date(lastUpdate).toDateString()}</Typography>
- <Typography variant="body2">Number of deaths caused by Covid-19</Typography>
- </CardContent>
- </Grid>
- <Grid className={styles.btnGrid} >
- <Button style={{backgroundColor: '#4A148C'}} className={styles.btnMyGov} variant="contained" color="primary" href="https://www.mohfw.gov.in/">
- mohfw
- </Button>
- <Button style={{backgroundColor: '#004D40'}} className={styles.btnMyGov} variant="contained" color="primary" href="https://www.mygov.in/covid-19">
- mygov
- </Button>
- <Button style={{backgroundColor: '#900C3F'}} className={styles.btnMyGov} variant="contained" color="primary" href="https://twitter.com/who?lang=en">
- WHO
- </Button>
- <Button style={{backgroundColor: '#581845'}} className={styles.btnMyGov} variant="contained" color="primary" href="https://twitter.com/PIB_India">
- PIB:IN
- </Button>
- </Grid>
- </Grid>
- </div>
- )
- }
- export default Cards;
src\Components\Cards\Cards.module.css : It is a CSS for cards, @media tag is responsible for responsive view.

Sumit KumarPosted Jul 9, 2020, 3:30 AM
Nice Rikam...thanks
kumar kashyap pandeyPosted Jul 8, 2020, 10:33 AM
Same code already available on Youtube: https://www.youtube.com/watch?v=khJlrj3Y6Ls