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>

ng2 中使用echart的更多相关文章

  1. ng2 中的全屏与退出全屏

    1.进入全屏 launchFullscreen(element) { if(element.requestFullscreen) { element.requestFullscreen(); } el ...

  2. Django框架中使用Echart进行统计的SQL语句

    最近想用Echart做数据统计的图形显示,数据来源是MySQL数据库,自然需要根据不同的搜索条件筛选出表中的数据,用比较多的就是时间的参数吧! 常用的mysql时间的条件进行检索的SQL语句: 数据表 ...

  3. 网站中使用echart

    在网站开发中,可能会使用折线图,圆饼图,等等 来丰富网页对数据的展示,可以使用echart http://echarts.baidu.com/tutorial.html#5%20%E5%88%86%E ...

  4. angular中使用echart遇到的获取容器高度异常的问题记录

    问题 在使用echart去创建图表时,发现图表只占了容器的一个角落,如图,并没有充满容器. 第一反应是容器元素的样式有问题,于是我把容器的宽高都改为px指定的(之前是百分比设定的,查询资料发现说ech ...

  5. vuejs中使用echart图表

    首先安装echart npm i echarts -S 加下来以使用这个图表为例 在vue组件中像这样使用: <template> <div :class="classNa ...

  6. 切换tab页时,tab页中的echart变形问题

    本文为博主原创,未经允许,不得转载: 在两个tab页中,分别展现了两个echart图表,同样的格式与写法,但只有在默认选中的tab页中的图表显示的是正常的, 但进入另一个tab页中时,图表则产生了变形 ...

  7. angular5中使用echart的方法

    注意两点安装的版本 安装好后可以参照echart的官网使用 1.实现package.json中安装这两个包 2.index.html中引入 3.在appModule中添加 然后再html中就可以这么使 ...

  8. 如何在vue中使用echart

    1.安装echarts依赖   npm install echarts --save 2.在main.js中全局中引用 import echarts from 'echarts' Vue.protot ...

  9. 在Vue中使用Echart图表库。【全网最简单】

    使用npm安装echart npm install echarts --save 然后在使用的页面上直接import import echarts from "echarts"; ...

随机推荐

  1. 丢失vcruntime140.dll

    我在php7安装yaf时报了标题所提示的错误信息. 解决方案是:下载vc++2015 并安装 链接如下:https://www.microsoft.com/zh-cn/download/confirm ...

  2. Java 线程的终止-interrupt

    Java线程的终止——interrupt 取消/关闭的场景 我们知道,通过线程的start方法启动一个线程后,线程开始执行run方法,run方法运行结束后线程退出,那为什么还需要结束一个线程呢?有多种 ...

  3. C#练习委托、事件、事件处理

    控制台应用程序效果: 代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  4. ubunbu退出nano

    无意中进入ubuntu GNU nano ctrl+x即可退出

  5. Java基础教程:反射基础

    Java基础教程:反射基础 引入反射 反射是什么 能够动态分析类能力的程序称为反射. 反射是一种很强大且复杂的机制. Class类 在程序运行期间,Java运行时系统始终为所有对象维护一个被称为运行时 ...

  6. 数据库基本表创建 完整性约束 foreign Key

    理解以下几张表的内容,根据实际情况设计属性名.数据类型.及各种完整性约束(primary key.foreign key.not null.unique.check),用数据定义语言实现,然后设计实验 ...

  7. runtime-分类为什么不生成setter和getter

    前言 前几天有人问我一个问题:为什么分类不能自动创建get set方法.老实说,笔者从来没有去思考过这个问题.于是这次通过代码实践跟runtime源码来探究这个问题. 准备工作 为了能减少输出类数据的 ...

  8. web前端框架之自定义form表单验证

    自定义form验证初试 .在后端创建一个类MainForm,并且在类中自定义host ip port phone等,然后写入方法,在post方法中创建MainForm对象,并且把post方法中的sel ...

  9. web框架详解之tornado 三 url和分页

    一.代码目录构建 controllers  :处理业务逻辑的 account:处理账户相关的 上面目录作用和内容 controllers 包 :处理业务逻辑的 account:处理账户相关的 home ...

  10. C#实现对外部程序的调用操作

    测试工具,首先也是一个C#的程序,它的主要目的是: 1:获取上文应用程序的窗口句柄,继而获取TextBox句柄及Button句柄: 2:为TextBox随机填入一些字符: 3:模拟点击Button: ...