echarts图表大小随屏幕的大小改变自适应
当页面有多个图表时
window.addEventListener("resize", function () {
myChart.resize();
});
点击查看代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>第一个 ECharts 实例</title>
<!-- 引入 echarts.js -->
<script src="https://cdn.staticfile.org/echarts/5.0.1/echarts.min.js"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="width: 100%;height:500px;"></div>
<div id="mainA" style="width: 100%;height:500px;"></div>
<div id="mainB" style="width: 100%;height:500px;"></div>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('main'));
var myChartA = echarts.init(document.getElementById('mainA'));
var myChartB = echarts.init(document.getElementById('mainB'));
// 指定图表的配置项和数据
var option = { title: { text: 'ECharts 入门示例' }, tooltip: {}, legend: { data: ['销量'] }, xAxis: { data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] };
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
myChartA.setOption(option);
myChartB.setOption(option);
window.addEventListener("resize", function () {
myChart.resize();
myChartA.resize();
myChartB.resize();
});
/* 或者
* window.onresize = function(){
* myChart.resize();
* myChartA.resize();
* myChartB.resize();
* }
/
</script>
</body>
</html>
当页面只有一个图表的时候直接用,window.onresize = myChart.resize