詳解angular中使用echarts地圖
在angular中使用echart的時(shí)候,只需要在對(duì)應(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暫無的時(shí)候的樣式 .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)組件銷毀的時(shí)候,取消相關(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)); // 通過延時(shí)器進(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)用就可以,同時(shí)還可以配置height和width。option為echarts官方定義的option
這樣其實(shí)就是簡(jiǎn)單封裝了一個(gè)基本的echarts生成組件,所有的配置項(xiàng)都是echarts的
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
AngularJS自定義指令實(shí)現(xiàn)面包屑功能完整實(shí)例
這篇文章主要介紹了AngularJS自定義指令實(shí)現(xiàn)面包屑功能,結(jié)合完整實(shí)例形式分析了AngularJS自定義指令的定義、調(diào)用及面包屑功能的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05AngularJS 驗(yàn)證碼60秒倒計(jì)時(shí)功能的實(shí)現(xiàn)
最近在做AngularJS 項(xiàng)目,這是寫的一個(gè)60秒倒計(jì)時(shí)功能,下面小編給大家介紹AngularJS 驗(yàn)證碼60秒倒計(jì)時(shí)功能的實(shí)現(xiàn),需要的朋友參考下吧2017-06-06AngularJS入門教程之AngularJS表達(dá)式
AngularJS應(yīng)用表達(dá)式是純javascript表達(dá)式,并輸出它們被使用的數(shù)據(jù)在那里。本文給大家介紹AngularJS入門教程之AngularJS表達(dá)式,對(duì)angularjs表達(dá)式相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-04-04Angularjs中的事件廣播 —全面解析$broadcast,$emit,$on
下面小編就為大家?guī)硪黄狝ngularjs中的事件廣播 —全面解析$broadcast,$emit,$on。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過來看看吧2016-05-05AngularJS中監(jiān)視Scope變量以及外部調(diào)用Scope方法
在AngularJS中,有時(shí)候需要監(jiān)視Scope中的某個(gè)變量,因?yàn)樽兞康母淖儠?huì)影響一些界面元素的顯示,接下來通過本文給大家介紹AngularJS中監(jiān)視Scope變量以及外部調(diào)用Scope方法,需要的朋友參考下吧2016-01-01Angular 4.X開發(fā)實(shí)踐中的踩坑小結(jié)
這篇文章主要給大家介紹了關(guān)于Angular 4.X開發(fā)實(shí)踐中的一些踩坑經(jīng)驗(yàn),文中主要介紹的是使用ngIf或者ngSwitch出錯(cuò)以及多級(jí)依賴注入器的相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07angular中實(shí)現(xiàn)li或者某個(gè)元素點(diǎn)擊變色的兩種方法
本篇文章主要介紹了angular中實(shí)現(xiàn)li或者某個(gè)元素點(diǎn)擊變色的兩種方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-07-07