When using echart in angular, you only need to call the echart API in the corresponding component life cycle. Initialization of echartInitialize echarts in the ngOnInit event of component, configure option, and then the echarts chart is generated. app-base-chart componenthtml<div #chart [ngClass]="'chart-box ' + (!option ? 'empty-chart' : '')"></div> CSS// Basic chart style.chart-box{ font-weight: bold; border: 1px solid #dcdcdc; border-radius: 4px; } // Style when option is not available.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 = 'No data yet'; } } // When the component is destroyed, cancel the related subscription ngOnDestroy(): void { if (this.windowResize) { this.windowResize.unsubscribe(); } if (this.timer) { this.timer.unsubscribe(); } } // Initialize the size of the container boxStyleInit(): void { this.chart.nativeElement.style.width = this.width; this.chart.nativeElement.style.height = this.height; } // Set the window's resize event listener and redraw the size of echarts setListen(): void { this.windowResize = fromEvent(window, 'resize').pipe( debounceTime(200), tap(res => { this.aChart.resize(); }) ).subscribe(); } // Configure and generate echarts chart according to optionechartsInit(): void { this.aChart = init(this.chart.nativeElement); this.aChart.setOption(Object.assign({ grid: this.defaultGrid }, this.option)); // Resize echarts through the delayer this.timer = timer(400).subscribe(res => { this.aChart.resize(); }); } } Using the app-base-chart component<app-base-chart [option]="option" width="100%" height="300px" ></app-base-chart> Just use the above code in the component's HTML, and you can also configure the height and width. option is the option officially defined by echarts This is actually a simple encapsulation of a basic echarts generation component, all configuration items are echarts SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Summary of Common Mistakes in Web Design
>>: Talking about the use of CSS3 custom variables in projects from a project reconstruction
How to install Linux CentOS 7.7 system in Vmware,...
Overview This article begins to introduce content...
Preview: Code: Page Sections: <template> &l...
In the MySQL database, null is a common situation...
This article introduces a detailed explanation of...
1 Download and start Tomcat Go to the official we...
When an employer asks you whether an index will b...
Table of contents 1. Overview 1.1 Definition 1.2 ...
Configure tomcat 1. Click run configuration 2. Se...
Preface This article mainly introduces the releva...
1. Download the repository image docker pull regi...
Without further ado, let's take a look at the...
Uninstall the installed version on Ubuntu: sudo a...
The /etc/network/interfaces file in Linux is used...
1. Install less dependency: npm install less less...