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
mysql full backup 1. Enable binary log and separa...
Preface Before we begin, we should briefly unders...
1. Environmental Preparation 1.1 Basic Environmen...
What is a Viewport Mobile browsers place web page...
Create a table CREATE TABLE `map` ( `id` int(11) ...
After the previous two chapters, do you have a ne...
The rewrite module is the ngx_http_rewrite_module...
This article shares with you the installation of ...
Many people have encountered this error when star...
This article mainly introduces the solution to th...
Table of contents Overview Global hook function R...
Software version and platform: MySQL-5.7.17-winx6...
1. Introduction to Flex Layout Flex is the abbrev...
Recently, when I was learning docker, I found tha...
html4: Copy code The code is as follows: <form...