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

Basic operation tutorial of files and permissions in centos

Preface Before we begin, we should briefly unders...

Methods and steps for Etcd distributed deployment based on Docker

1. Environmental Preparation 1.1 Basic Environmen...

HTML meta viewport attribute description

What is a Viewport Mobile browsers place web page...

Pure CSS3 to achieve mouse over button animation Part 2

After the previous two chapters, do you have a ne...

Detailed explanation of Nginx's rewrite module

The rewrite module is the ngx_http_rewrite_module...

How to install and modify the initial password of mysql5.7.18 under Centos7.3

This article shares with you the installation of ...

Solve the compatibility issue between MySQL 8.0 driver and Alibaba Druid version

This article mainly introduces the solution to th...

vue-router hook function implements routing guard

Table of contents Overview Global hook function R...

Mysql 5.7.17 winx64 installation tutorial on win7

Software version and platform: MySQL-5.7.17-winx6...

A brief discussion on Flex layout and scaling calculation

1. Introduction to Flex Layout Flex is the abbrev...

Solve the problem of ifconfig being unavailable in docker

Recently, when I was learning docker, I found tha...