Echarts图表移动端横屏进入退出的实现
作者:水冗水孚 时间:2024-05-11 09:06:45
效果图
代码
<template>
<div class="outWrap">
<div
:class="
isHorizontalScreen ? 'horizontalEchartsFather' : 'verticalEchartsFather'
"
>
<!-- 全屏进入退出按钮 -->
<h3
@click="switchFn"
:class="isHorizontalScreen ? 'horizontalIcon' : 'verticalIcon'"
>
{{ isHorizontalScreen ? "退出横屏" : "进入横屏" }}
</h3>
<!-- echarts部分 -->
<div
id="echartsId"
:class="isHorizontalScreen ? 'horizontal' : 'vertical'"
></div>
</div>
</div>
</template>
<script>
import Echarts from "echarts";
export default {
data() {
return {
myChart: null, // echarts的实例
isFull: false, // 是否全屏
isHorizontalScreen: false, // 是否是横向屏幕,默认false,默认是竖向屏幕
option: {
dataZoom: [
{
type: "inside",
},
],
xAxis: {
data: [
"4.1",
"4.2",
"4.3",
"4.4",
"4.5",
"4.6",
"4.7",
"4.8",
"4.9",
"4.10",
"4.11",
"4.12",
"4.13",
"4.14",
"4.15",
"4.16",
"4.17",
],
},
yAxis: {},
series: {
name: "股价",
type: "line",
data: [
51, 61, 71, 27, 19, 20, 15, 8, 21, 2, 19, 66, 88, 4, 21, 77, 6,
],
itemStyle: {
normal: {
color: "green", //改变折线点的颜色
lineStyle: {
color: "green", //改变折线颜色
},
},
},
},
},
};
},
watch: {
// 当横屏进入退出要重绘一下echarts
isHorizontalScreen(newVal) {
console.log("横屏切换", newVal);
this.myChart.setOption(this.option, true);
this.$nextTick(() => {
this.resize();
});
},
},
mounted() {
// 添加自适应监听
window.addEventListener("resize", this.resize);
this.echarts();
},
methods: {
// 初始化
echarts() {
this.myChart = Echarts.init(document.querySelector("#echartsId"));
this.myChart.setOption(this.option);
},
resize() {
this.myChart.resize(); // 窗口大小发生变化的时候重置
},
// 切换 水平垂直~全屏默认屏
switchFn() {
this.isHorizontalScreen = !this.isHorizontalScreen;
this.isFull = !this.isFull;
},
},
// 移除自适应监听
beforeDestroy() {
window.removeEventListener("resize", this.resize);
},
};
</script>
<style lang="less" scoped>
// 最外层满屏
.outWrap {
width: 100%;
height: 100vh;
background: #e9e9e9;
}
/* 用于给竖向echarts画布定位用 */
.verticalEchartsFather {
width: 100%;
height: 50%;
background-color: #fff;
position: relative;
}
// 竖向的正常百分比即可
.vertical {
width: 100%;
height: 100%;
}
/* 用于给横向echarts画布定位用,横向就旋转90度即可 */
.horizontalEchartsFather {
transform: rotate(90deg);
position: relative;
width: 100%;
}
// 因为横向了,所以颠倒一下
.horizontal {
width: 100vh;
height: 100vw;
}
/* 进入横屏和退出横屏两套样式,定位在不同的位置 */
.verticalIcon {
position: absolute;
top: 2px;
right: 6px;
z-index: 999;
user-select: none;
}
.horizontalIcon {
position: absolute;
top: 2px;
left: 6px;
z-index: 999;
user-select: none;
}
</style>
来源:https://juejin.cn/post/7220396447464243256
标签:Echarts,横屏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Oracle中的分析函数汇总
2024-01-20 05:59:38
![](https://img.aspxhome.com/file/2023/1/67031_0s.jpg)
CI框架整合smarty步骤详解
2023-11-14 11:18:11
Python实现批量识别图片文字并存为Excel
2021-07-28 06:34:23
![](https://img.aspxhome.com/file/2023/8/66838_0s.jpg)
Python中return用法案例详解
2022-09-09 10:35:40
利用 Monkey 命令操作屏幕快速滑动
2021-03-06 11:37:50
![](https://img.aspxhome.com/file/2023/6/89136_0s.png)
python用于url解码和中文解析的小脚本(python url decoder)
2023-01-28 06:19:00
Python中的zip函数使用示例
2021-05-15 01:10:53
优化mysql的limit offset的例子
2024-01-12 17:23:25
windows中安装Python3.8.0的实现方法
2022-11-22 00:18:09
![](https://img.aspxhome.com/file/2023/6/118336_0s.jpg)
点球小游戏python脚本
2022-07-17 23:28:03
MySQL索引类型Normal、Unique和Full Text的讲解
2024-01-20 09:56:19
Python logging日志模块 配置文件方式
2021-03-07 04:31:01
numpy增加维度、删除维度的方法
2023-12-07 22:29:01
sql表连接查询使用方法(sql多表连接查询)
2024-01-22 12:25:39
mysql使用mysqld_multi部署单机多实例的方法教程
2024-01-15 12:26:01
python3中替换python2中cmp函数的实现
2021-08-15 01:42:41
![](https://img.aspxhome.com/file/2023/0/101660_0s.png)
MySQL中union和order by同时使用的实现方法
2024-01-26 22:48:45
![](https://img.aspxhome.com/file/2023/9/124549_0s.png)
go程序员日常开发效率神器汇总
2024-02-16 23:04:40
Go interface{} 转切片类型的实现方法
2024-05-05 09:31:05
python爬虫之自动登录与验证码识别
2022-05-18 07:22:53