Introduction
I have already published an article about simple COVID-19 tracker on C# Corner and also deployed the application in Google firebase. You can read the article from below link.
In this post, we will see how to create a COVID-19 tracker application in Angular 10. We can track the Indian state-wise and district-wise COVID-19 patient details using this application. We will use two public APIs for getting the latest patient data. We will use
https://api.covid19india.org/data.json for getting the entire state-wise patient data and use
https://api.covidindiatracker.com/state_data.json for getting district-wise patient details.
Create Angular 10 application using Angular CLI
We can use below command to create new Angular application.
ng new Covid19-India-Tracker
After few moments, new Angular application will be ready with required node modules. We can open the application in Visual Studio Code.
We need bootstrap package to add some styles in our application. We can install the latest bootstrap package using npm command
npm install bootstrap
We must import bootstrap package inside the style.css file. So that, this package will be available in entire application context.
style.css
- @import "~bootstrap/dist/css/bootstrap.css";
- body {
- overflow: hidden;
-
- }
We must register “HttpClientModule” and “FormsModule” inside app.module file.
app.module.ts
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
-
- import { AppComponent } from './app.component';
-
- import { HttpClientModule } from '@angular/common/http'
- import { FormsModule } from '@angular/forms'
-
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- HttpClientModule,
- FormsModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
We can create a service to get data from public APIs. Use below CLI command to create a new service.
ng generate service Data
Add below code inside the service to get data from public API.
data.service.ts
- import { Injectable } from '@angular/core';
- import { HttpClient } from '@angular/common/http'
- import { Observable } from 'rxjs';
-
- @Injectable({
- providedIn: 'root'
- })
- export class DataService {
-
- constructor(private http: HttpClient) { }
-
- getAllData(): Observable<any> {
- let url = 'https://api.covid19india.org/data.json'
- return this.http.get(url).
- pipe((response) => response
- )
- }
-
- getStateData(): Observable<any> {
- let url = 'https://api.covidindiatracker.com/state_data.json'
- return this.http.get(url).
- pipe((response) => response
- )
- }
- }
Create a model class file and add below classes inside the file.
models.ts
- export class Data {
- active: number;
- confirmed: number;
- deaths: number;
- deltaconfirmed: number;
- deltadeaths: number;
- deltarecovered: number;
- lastupdatedtime: string;
- migratedother: number;
- recovered: number;
- state: string;
- statecode: string;
- }
-
- export class ChildData {
- id: string;
- state: string;
- districtData: DistrictData[];
- }
-
- export class DistrictData {
- confirmed: number;
- id: string;
- name: string;
- }
We can modify app.component class file with below code.
app.component.ts
- import { Component, OnInit } from '@angular/core';
- import { DataService } from './data.service'
- import { Data, ChildData } from './models'
-
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent implements OnInit {
- title = 'Covid19-India-Tracker';
- allData: Data[];
- totalData: Data;
- selectedStateCode: string;
- selectedDistrictCode: string;
- stateData: Data;
- statewiseData: ChildData[];
- districtData: ChildData;
-
- constructor(private service: DataService) { }
-
- ngOnInit() {
- this.getAllData();
- }
-
- getAllData() {
- this.service.getAllData().subscribe(
- response => {
- this.allData = response.statewise;
- this.totalData = this.allData.find(x => x.statecode == 'TT');
- this.allData = this.allData.filter(x => x.statecode != 'TT' && x.statecode != 'UN');
- this.getStateData();
- }
- )
- }
-
- getStateData() {
- this.service.getStateData().subscribe(
- response => {
- this.statewiseData = response;
- }
- )
- }
-
- onStateSelected() {
- this.stateData = this.allData.find(x => x.statecode == this.selectedStateCode)
- let stateCode = (this.selectedStateCode == 'LA') ? 'LK' : this.selectedStateCode;
- this.districtData = this.statewiseData.find(x => x.id == `IN-${stateCode}`);
- }
- }
We can modify app.component html file with below code.
app.component.html
We need some common style in the application. We can modify the css class file with below code.
app.component.css
- tbody {
- height: 550px;
- overflow-y: auto;
- }
-
- thead>tr, tbody {
- display: block;
- }
-
- .margin-class{
- margin: 7px;
- }
-
- img {
- opacity: 0.3;
- }
We have completed the coding part. We can run the application now.
We can choose any of the state from drop down list.
If you choose any state, state-wise patient details will be shown along with district-wise confirmed patients count.
I am not claiming full accuracy for the patient data provided by above public APIs. Both APIs are maintained by separate organizations.
I have deployed the application in Google firebase. You can check the COVID-19 patient details using this
LIVE application.
Conclusion
In this post, we have seen how to create a COVID-19 tracker application with Angular 10. We have used two public APIs for getting patient data. We can easily track the state-wise and district-wise patient details using this simple application. Please give your valuable feedback about this application.