這篇文章將為大家詳細講解有關angular中怎么使用echarts地圖,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在angular中使用echart的時候,只需要在對應的組件生命周期中調用echart的api就可以了
在component的ngOnInit事件中進行echarts的初始化,配置option,然后echarts圖表就生成了
<div #chart [ngClass]="'chart-box ' + (!option ? 'empty-chart' : '')"></div>
// 基本的圖表樣式 .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 = '暫無數據'; } } // 當組件銷毀的時候,取消相關訂閱 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; } // 設置window的resize事件監聽,并重繪echarts的大小 setListen(): void { this.windowResize = fromEvent(window, 'resize').pipe( debounceTime(200), tap(res => { this.aChart.resize(); }) ).subscribe(); } // 根據option配置和生成echarts圖表 echartsInit(): void { this.aChart = init(this.chart.nativeElement); this.aChart.setOption(Object.assign({ grid: this.defaultGrid }, this.option)); // 通過延時器進行echarts的大小重繪 this.timer = timer(400).subscribe(res => { this.aChart.resize(); }); } }
<app-base-chart [option]="option" width="100%" height="300px" ></app-base-chart>
只需要在組件的html中像上面代碼運用就可以,同時還可以配置height和width。option為echarts官方定義的option
這樣其實就是簡單封裝了一個基本的echarts生成組件,所有的配置項都是echarts的
關于“angular中怎么使用echarts地圖”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。