詳解angular中使用echarts地圖
在angular中使用echart的時候,只需要在對應(yīng)的組件生命周期中調(diào)用echart的api就可以了
echart的初始化
在component的ngOnInit事件中進(jìn)行echarts的初始化,配置option,然后echarts圖表就生成了
app-base-chart組件
html
<div #chart [ngClass]="'chart-box ' + (!option ? 'empty-chart' : '')"></div>
css
// 基本的圖表樣式
.chart-box{
font-weight: bold;
border: 1px solid #dcdcdc;
border-radius: 4px;
}
// option暫無的時候的樣式
.empty-chart{
display: flex;
justify-content: center;
align-items: center;
font-size: 18px;
}
import { Component, ElementRef, Input, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { fromEvent, Subscription, timer } from 'rxjs';
import { debounceTime, tap } from 'rxjs/operators';
import { ECharts, EChartsOption, init } from 'echarts';
@Component({
selector: 'app-base-chart',
templateUrl: './base-chart.component.html',
styleUrls: ['./base-chart.component.scss']
})
export class BaseChartComponent implements OnInit, OnDestroy {
@Input() option: EChartsOption;
@Input() height = '300px';
@Input() width = '100%';
@ViewChild('chart', { static: true }) chart: ElementRef;
aChart: ECharts;
windowResize: Subscription;
timer: Subscription;
defaultGrid = {
top: 10,
right: 10,
bottom: 30,
left: 30,
};
constructor() { }
ngOnInit(): void {
this.setListen();
this.boxStyleInit();
if (!!this.option) {
this.echartsInit();
}else{
this.chart.nativeElement.innerText = '暫無數(shù)據(jù)';
}
}
// 當(dāng)組件銷毀的時候,取消相關(guān)訂閱
ngOnDestroy(): void {
if (this.windowResize) {
this.windowResize.unsubscribe();
}
if (this.timer) {
this.timer.unsubscribe();
}
}
// 初始化容器的大小size
boxStyleInit(): void {
this.chart.nativeElement.style.width = this.width;
this.chart.nativeElement.style.height = this.height;
}
// 設(shè)置window的resize事件監(jiān)聽,并重繪echarts的大小
setListen(): void {
this.windowResize = fromEvent(window, 'resize').pipe(
debounceTime(200),
tap(res => {
this.aChart.resize();
})
).subscribe();
}
// 根據(jù)option配置和生成echarts圖表
echartsInit(): void {
this.aChart = init(this.chart.nativeElement);
this.aChart.setOption(Object.assign({ grid: this.defaultGrid }, this.option));
// 通過延時器進(jìn)行echarts的大小重繪
this.timer = timer(400).subscribe(res => {
this.aChart.resize();
});
}
}
使用app-base-chart組件
<app-base-chart [option]="option" width="100%" height="300px" ></app-base-chart>
只需要在組件的html中像上面代碼運(yùn)用就可以,同時還可以配置height和width。option為echarts官方定義的option
這樣其實(shí)就是簡單封裝了一個基本的echarts生成組件,所有的配置項都是echarts的
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
詳解angular2采用自定義指令(Directive)方式加載jquery插件
本篇文章主要介紹了詳解angular2采用自定義指令(Directive)方式加載jquery插件 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
AngularJS ng-blur 指令詳解及簡單實(shí)例
本文主要介紹AngularJS ng-blur 指令,這里對ng-blur 指令的知識詳細(xì)介紹給大家,并給大家一個實(shí)例作為參考,有需要的小伙伴可以參考下2016-07-07
Angular 4.x中表單Reactive Forms詳解
這篇文章主要介紹了Angular 4.x中表單Reactive Forms的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04
Angular outlet實(shí)現(xiàn)頁面布局示例詳解
這篇文章主要為大家介紹了Angular outlet實(shí)現(xiàn)頁面布局示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
分享Angular http interceptors 攔截器使用(推薦)
AngularJS 是一個 JavaScript 框架。它可通過 <script> 標(biāo)簽添加到 HTML 頁面。這篇文章主要介紹了分享Angular http interceptors 攔截器使用(推薦),需要的朋友可以參考下2019-11-11

