1.首先创建echarts指令

 //echart.directive.ts
important { Directive,ElementRef,Input,Ouput,Onchanges,OnInit,OnDestroy,SimpleChanges,EventEmitter} from '@angular/core';
important * as echarts from 'echarts'; @Directive({
selector: 'echart'
}) export class EchartOptiononDirective implements OnInin,OnChanges,OnDestroy {
@Input('chartType') chartType:any;
@Output() resizeEle = new EventEmitter<any>();
public Ele;
constructor(private el:ElementRef){} ngOnInit():void {}
ngOnChanges(changes:SimpleChanges) {
let params;
if(params != this.chartType){
params = this.chartType;
if(this.Ele){this.Ele.dispose()} //每次change改变echart实例,防止内存泄漏
this.Ele = echarts.init(this.el.nativeElement); //绘制图表
this.Ele.setOption(this.chartType);
this.resizeEle.emit(this.Ele); //resize图表
}
}
ngOnDestroy(){
if(this.Ele){this.Ele.dispose()}
}
}

2.组件模板中引入指令

//echart.module.ts
import {EchartOptionDirective} from './echart.directive' @NgModule({
declarations:[
EchartOptionDirective
]
})

3.组件中使用echart

//echart.component.ts
export class EchartComponent implements OnInit,OnDestroy{
public chartoption:any;
public echart:any;
constructor(){
window.onresize = () => { //改变窗口大小reseze图表
this.echart.resize();
}
}
ngOnInit(){
window.onresize = () => {
this.echart.resize();
}
}
ngOnDestroy() {
window.onresize = () => {}; //防止内存泄漏
}
resizeElement(e){
this.echart= e;
}
renderChart(){
this.chartoption = {
//此处为echart图表相关内容
backgtoundColor:'#fff',
title:{},
...
}
}
}

4.html中使用echart

//echart.component.html
<echart *ngIf = "chartoption" [chartType] = "chartoption" (resizeEle) = "resizeElement($event)"></echart>

最新文章

  1. Linux命令全称
  2. Selenium2入门(一)简介
  3. 升级ruby后再安装cocodPod
  4. C#参考:Linq 概述
  5. [Tool] WireShark基本使用
  6. 初识50个Linux命令
  7. 更改PATH后,别忘了及时重启或刷新
  8. EAPOL 协议
  9. java String 的方法 (01)
  10. cardsui-for-android
  11. 个人对maven pom.xml文件的理解
  12. 当很多连接到你的数据库时,报这种错误“已超过了锁请求超时时段” SqlServer数据库正在还原的解决办法
  13. SD卡FAT32文件系统格式
  14. 【Java基础】用LinkedList实现一个简单栈的功能
  15. 【原创】Linux 增加系统调用
  16. 【转】linux 原子整数操作详解
  17. Ubuntu离线安装VSCode(附带前期准备工作)
  18. 二、Windows Server 2016 AD 组织单位、组、用户的创建
  19. laravel5.4 导出 Excel 表格
  20. 亚马逊VE账号运营

热门文章

  1. centos7 mysql允许远程连接设置
  2. ABAP 内表
  3. Java并发之LinkedBlockingQueue
  4. foreign key
  5. gulp 打包报错:ReferenceError: internalBinding is not defined
  6. HDU - 1176 免费馅饼 【DP】
  7. POJ - 2195 Going Home 【KM】
  8. Data Structure Graph: prim
  9. 0521 HTML基础
  10. vRA Customizing error