#3. How to use Basic Area Line Echarts with Angular 10
Read time : 8-10 minutes
Step-1 : Create a Angular 10 Project .
If you haven't created an Angular 10 project yet then visit the following link otherwise skip this step.CLICK HERE TO VISIT.
Step-2 : Integrate of Echarts with Angular 10 .
If you haven't integrated Echarts with an Angular 10 project yet then visit the following link otherwise skip this step.
Step-3 : Create Component (Create html , scss and ts file).
Please find the component which I have created for basic area line chart with code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div fxLayoutGap="1rem" fxLayout="column"> | |
<mat-checkbox [(ngModel)]="_isDarkMode" (change)="ngOnInit()">Dark Mode</mat-checkbox> | |
<div echarts [options]="_chartOption" [theme]="_theme"> </div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NO CODE CHANGES. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, OnDestroy } from '@angular/core'; | |
import { Subscription } from 'rxjs'; | |
import { EchartService } from 'src/app/common/service/echart.service'; | |
import { BasicEchartLineModel } from 'src/app/common/model/echart.model'; | |
import { EChartOption } from 'echarts'; | |
@Component({ | |
selector: 'app-basic-area-echarts', | |
templateUrl: './basic-area-echarts.component.html', | |
styleUrls: ['./basic-area-echarts.component.scss'] | |
}) | |
export class BasicAreaEchartsComponent implements OnInit, OnDestroy { | |
_chartOption: EChartOption; | |
subscription: Subscription; | |
_isDarkMode: boolean = false; | |
_theme: string; | |
constructor(private echartSevrice: EchartService) { } | |
ngOnInit(): void { | |
this.subscription = this.echartSevrice.getbasicAreaEchartData().subscribe(data => { | |
this._initBasicAreaEcharts(data); | |
}) | |
} | |
ngOnDestroy() { | |
if (this.subscription) { | |
this.subscription.unsubscribe(); | |
} | |
} | |
private _initBasicAreaEcharts(chartData: BasicEchartLineModel[]) { | |
this._theme = this._isDarkMode ? 'dark' : ''; | |
this._chartOption = { | |
tooltip: { | |
show: true | |
}, | |
xAxis: { | |
type: 'category', | |
data: chartData.map(m => ({ | |
value: m.name | |
})) | |
}, | |
yAxis: { | |
type: 'value' | |
}, | |
series: [{ | |
data: chartData.map(m => ({ | |
value: m.value | |
})), | |
type: 'line', | |
areaStyle: {} | |
}] | |
} | |
} | |
} |
Step-4 : Create Service file to get data from service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpClient } from "@angular/common/http"; | |
import { Observable } from 'rxjs'; | |
import { BasicEchartLineModel, StackedLineEchartmodel } from '../model/echart.model'; | |
@Injectable() | |
export class EchartService { | |
constructor(private httpClient: HttpClient) { } | |
getbasicAreaEchartData() :Observable<BasicEchartLineModel[]>{ | |
return this.httpClient.get<BasicEchartLineModel[]>('assets/echart/basic-area-chart-data.json'); | |
} | |
} |
Step-5 : Create model class to map service value into model properties.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface BasicEchartLineModel { | |
name: string; | |
value: number; | |
} |
Step-6 : Create .json file under asset folder and also add some dummy json data required for echarts.
NOTE : We have called this file to get data using get service under echart.service.ts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "Monday", | |
"value": 500 | |
}, | |
{ | |
"name": "Tuesday", | |
"value": 100 | |
}, | |
{ | |
"name": "Wednesday", | |
"value": 1500 | |
}, | |
{ | |
"name": "Thrusday", | |
"value": 2000 | |
}, | |
{ | |
"name": "Friday", | |
"value": 300 | |
}, | |
{ | |
"name": "Saturday", | |
"value": 600 | |
}, | |
{ | |
"name": "Sunday", | |
"value": 50 | |
}, | |
{ | |
"name": "Sunday1", | |
"value": 5000 | |
} | |
] |
Step-7 : Add entries of components , services etc into module file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BrowserModule } from '@angular/platform-browser'; | |
import { NgModule } from '@angular/core'; | |
import { AppRoutingModule } from './app-routing.module'; | |
import { AppComponent } from './app.component'; | |
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | |
import { FormsModule } from '@angular/forms'; | |
import { HttpClientModule } from "@angular/common/http"; | |
import { BasicAreaEchartsComponent } from './echarts/line/basic-area-echarts/basic-area-echarts.component'; | |
import { NgxEchartsModule } from 'ngx-echarts'; | |
import { EchartService } from './echarts/echart.service'; | |
@NgModule({ | |
declarations: [ | |
AppComponent, | |
BasicAreaEchartsComponent | |
], | |
imports: [ | |
FormsModule, | |
HttpClientModule, | |
BrowserModule, | |
AppRoutingModule, | |
BrowserAnimationsModule, | |
NgxEchartsModule | |
], | |
providers: [EchartService], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { } |
Conclusion :
This is all about Basic area line echarts. But note this is not the full source code , this is just a major piece of code .To get the full source code please click on the following link.
Full Source Code
See Project Demo :
Find Source Code :
For more updates please follow us on :
Facebook : https://www.facebook.com/dorado2041
Instagram : https://www.instagram.com/dorado2041
Blogger : https://doradosolutions.blogspot.com
No comments: