关于响应式eachart(随父级宽高改变而改变,可单页面多个eachart一起适配)

关于响应式eachart(随父级宽高改变而改变,可单页面多个eachart一起适配)

效果展示

在这里插入图片描述

一、ts代码

  import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-eachart-text',
  templateUrl: './eachart-text.component.html',
  styleUrls: ['./eachart-text.component.less']
})
export class EachartTextComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
    this.eachartfunttion()
    this.eachartBarfunttion()
  }
  resizeFun
  eachartfunttion() {
    let chartDom = <HTMLInputElement>document.getElementById('main');
    let myChart = echarts.init(chartDom, null, {
      width: document.getElementById('main').clientWidth,//获取父级的宽//初始化默认的宽
      height: document.getElementById('main').clientHeight//获取父级的高//初始化默认的高
    });
    //监听页面的 window.onresize 事件获取浏览器大小改变的事件,
    //然后调用 echartsInstance.resize 改变图表的大小
    this.resizeFun = () => {
      console.log("1111111111111111111111111111")
      myChart.resize({
        width: document.getElementById('main').clientWidth,//获取父级的宽
        height: document.getElementById('main').clientHeight//获取父级的高
      });
    }
    window.addEventListener('resize', this.resizeFun);
    let option;
    option = {
      xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: [150, 230, 224, 218, 135, 147, 260],
          type: 'line'
        }
      ]
    };
    option && myChart.setOption(option);
  }
  resizeBarFun
  eachartBarfunttion() {
    let chartDom = <HTMLInputElement>document.getElementById('mainbar');
    let myChart = echarts.init(chartDom, null, {
      width: document.getElementById('mainbar').clientWidth,//获取父级的宽//初始化默认的宽
      height: document.getElementById('mainbar').clientHeight//获取父级的高//初始化默认的高
    });
    //监听页面的 window.onresize 事件获取浏览器大小改变的事件,
    //然后调用 echartsInstance.resize 改变图表的大小
    this.resizeBarFun = () => {
      console.log("222222222222222222222222222222")
      myChart.resize({
        width: document.getElementById('mainbar').clientWidth,//获取父级的宽
        height: document.getElementById('mainbar').clientHeight//获取父级的高
      })
    }
    window.addEventListener('resize', this.resizeBarFun);

    let option;
    option = {
      xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: [120, 200, 150, 80, 70, 110, 130],
          type: 'bar',
          showBackground: true,
          backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)'
          }
        }
      ]
    };

    option && myChart.setOption(option);
  }
  //生命周期销毁调用
  ngOnDestroy() {
    window.removeEventListener('resize', this.resizeBarFun);//去除事件监听
    window.removeEventListener('resize', this.resizeFun);//去除事件监听
  }

}


二、html代码

<div class="Qbody">
  <!-- 折线图 -->
  <div id="main" class="main"></div>
  <!-- 柱状图 -->
  <div id="mainbar" class="mainbar"></div>
</div>

三、css代码

.Qbody{
  width: 100%;
  height:calc(100vh - 64px);//64px 为多余的部分,没有则直接用100vh
  padding: 20px;
  background: blue;
  //折线图
  .main{
    height: 50%;
    width: 100%;
    background: red;
  }
  //柱状图
  .mainbar{
    height: 50%;
    width: 100%;
    background: greenyellow;
  }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值