每天學習一點點,每天記錄一點點、每天進步一點點——————《伊索寓言》
效果圖
程式碼
<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>
總結
橫屏的時候,即為豎屏旋轉90度,加上監聽控制,然後改一下樣式,最後別忘了重繪一下Echarts
A good memory is better than a bad pen. Write it down ???
If this article helped you, don't forget to jump to my GitHub and give a star ⭐⭐⭐ Thanks a lot...