Detailed explanation of using echarts map in angular

Detailed explanation of using echarts map in angular

When using echart in angular, you only need to call the echart API in the corresponding component life cycle.

Initialization of echart

Initialize echarts in the ngOnInit event of component, configure option, and then the echarts chart is generated.

app-base-chart component

html

<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

Summarize

This 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:
  • Detailed explanation of Javascript Echarts air quality map effect
  • JavaScript data visualization: ECharts map making
  • vue+echarts realizes the flow effect of China map (detailed steps)
  • vue+echarts+datav large screen data display and implementation of China map province, city and county drill-down function
  • Case study of introducing Chinese map in echarts in vue

<<:  Summary of Common Mistakes in Web Design

>>:  Talking about the use of CSS3 custom variables in projects from a project reconstruction

Recommend

Detailed tutorial on VMware installation of Linux CentOS 7.7 system

How to install Linux CentOS 7.7 system in Vmware,...

Detailed explanation of CocosCreator message distribution mechanism

Overview This article begins to introduce content...

Detailed code for implementing 3D tag cloud in Vue

Preview: Code: Page Sections: <template> &l...

Summary of knowledge points about null in MySQL database

In the MySQL database, null is a common situation...

Will the index be used in the MySQL query condition?

When an employer asks you whether an index will b...

Use Docker Compose to quickly deploy ELK (tested and effective)

Table of contents 1. Overview 1.1 Definition 1.2 ...

Idea configures tomcat to start a web project graphic tutorial

Configure tomcat 1. Click run configuration 2. Se...

Detailed explanation of the construction and use of docker private warehouse

1. Download the repository image docker pull regi...

Sample code for implementing 3D book effect with CSS

Without further ado, let's take a look at the...

Problems installing TensorRT in docker container

Uninstall the installed version on Ubuntu: sudo a...

Linux /etc/network/interfaces configuration interface method

The /etc/network/interfaces file in Linux is used...

Solve the problem of using less in Vue

1. Install less dependency: npm install less less...