码迷,mamicode.com
首页 > 其他好文 > 详细

ng4中使用echart;升级脚手架到最新版本

时间:2017-06-29 17:27:52      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:title   窗口   改变   tco   struct   创建   ack   set   rom   

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>

 

 

 

升级脚手架到最新版本

//先卸载,后安装
npm uninstall -g @angular/cli
npm cache clean
npm install -g @angular/cli@latest

ng4中使用echart;升级脚手架到最新版本

标签:title   窗口   改变   tco   struct   创建   ack   set   rom   

原文地址:http://www.cnblogs.com/rushjs/p/7094636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!