ionic 封装Echarts,自定义组件方便开发调用

4 篇文章 0 订阅
3 篇文章 0 订阅

1. Ionic 常用命令


2.前期准备工作

(1) 将echarts 放在项目assets文件夹中

 

(2)在 index.html 引用相关js文件



(3)使用命令行创建名为echart的组件,我将它放在了这里。如下图


3.相关代码

(1)html代码

<div #containerchart class="myechart"></div>
(2)scss代码

echarts {
.myechart{
  width: 100%;
  height: 260px;
  margin-right: 5px;
}
}
(3) ts代码
import {Component, Input, Output, EventEmitter, ElementRef, ViewChild} from '@angular/core';
declare var echarts;
/**
 * @name echart 组件
 * @description  miao
 * @example   <echarts [config]="" [cheight]=""></echarts>
 */
@Component({
  selector: 'echarts',
  templateUrl: 'echarts.html',
})
export class Echart   {
  @ViewChild('containerchart') container: ElementRef;
  @Input() config:any;//此为 echart 组件的配置项
  @Input() cheight:any;//设置  图表的高度
  @Output() pwdResult = new EventEmitter<any>();
  defaltOption;
  chart: any;
  constructor() {
    this.defaltOption = {
      baseOption: {
        toolbox: {
          show: false, // 是否显示工具栏
          feature: {
            dataView: {
              readOnly: true // 数据只读
            }
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false
        }
      }
    };
  }
  ionViewDidEnter(){

  }
  ngOnChanges(changes) {
      this.refresh();
  }
  ngAfterContentInit() {
      this.refresh();
  }
  refresh(){
    let ctx = this.container.nativeElement;
    if(this.cheight){
      ctx.style.height=this.cheight+'px';
    }
    this.chart = echarts.init(ctx);
    this.chart.setOption(this.config);
  }
}
4.使用方法
	(1)引用组件

 
(2)html 使用组件 
	<echarts [config]="config" [cheight]="220"></echarts>
(3)ts文件初始化图表数据

this.config={
  color: ['#3398DB'],
  tooltip : {
    trigger: 'axis',
    axisPointer : {
      type : 'shadow'
    }
  },
  grid: {
    left: '2%',
    right: '3%',
    bottom: '3%',
    containLabel: true
  },
  xAxis : [
    {
      type : 'category',
      data : ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      axisTick: {
        alignWithLabel: true
      }
    }
  ],
  yAxis : [
    {
      type : 'value'
    }
  ],
  series : [
    {
      name:'直接访问',
      type:'bar',
      // barWidth: '60%',
      data:[10, 52, 200, 334, 390, 330, 212]
    }
  ]
};



5.效果图


我们可以发现在此基础上还可以扩展更多东西,这要靠大家继续努力了, 您的帮助是我莫大的鼓励!喜欢的话可以扫描左侧二维码随意打赏哈~支付宝微信都可以,欢迎看看我的其他文章~

这篇文章目前收到了不少朋友的支持,在这里谢谢你们了,打赏有价,支持与鼓励无价!



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值