Echarts圖表移動端橫屏進入退出的實現(xiàn)
更新時間:2023年05月09日 10:07:14 作者:水冗水孚
本文主要介紹了Echarts圖表移動端橫屏進入退出的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
效果圖

代碼
<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: {
// 當(dāng)橫屏進入退出要重繪一下echarts
isHorizontalScreen(newVal) {
console.log("橫屏切換", newVal);
this.myChart.setOption(this.option, true);
this.$nextTick(() => {
this.resize();
});
},
},
mounted() {
// 添加自適應(yīng)監(jiān)聽
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(); // 窗口大小發(fā)生變化的時候重置
},
// 切換 水平垂直~全屏默認屏
switchFn() {
this.isHorizontalScreen = !this.isHorizontalScreen;
this.isFull = !this.isFull;
},
},
// 移除自適應(yīng)監(jiān)聽
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畫布定位用,橫向就旋轉(zhuǎn)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>總結(jié)
橫屏的時候,即為豎屏旋轉(zhuǎn)90度,加上監(jiān)聽控制,然后改一下樣式,最后別忘了重繪一下Echarts
到此這篇關(guān)于Echarts圖表移動端橫屏進入退出的實現(xiàn)的文章就介紹到這了,更多相關(guān)Echarts移動端橫屏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Express框架中_router?對象數(shù)據(jù)結(jié)構(gòu)使用詳解
這篇文章主要為大家介紹了Express框架中_router的對象數(shù)據(jù)結(jié)構(gòu)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
使用JS監(jiān)聽鍵盤按下事件(keydown event)
這篇文章主要介紹了使用JS監(jiān)聽鍵盤按下事件(keydown event),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
淺談Javascript中Object與Function對象
JavaScript的面向?qū)ο笫腔谠蔚?,所有對象都有一條屬于自己的原型鏈。Object與Function可能很多看Object instanceof Function , Function instanceof Object都為true而迷惑,所以首先看下對象的實例2015-09-09

