关于响应式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
});
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
});
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;
}
}