vue2.0中自適應(yīng)echarts圖表、全屏插件screenfull的使用
該案例是基于vue2.0腳手架,使用了elementUI、eCharts、screenfull插件
自適應(yīng)echarts圖表
- 第一:自適應(yīng)的echarts圖表,要配合著能夠自適應(yīng)的盒子來(lái)使用,首先就是盒子要能夠跟隨屏幕大小改變而改變,比如我們使用彈性盒子就可以實(shí)現(xiàn)。
- 第二:要想實(shí)現(xiàn)自適應(yīng)的echarts圖表,就是當(dāng)窗口發(fā)生改變時(shí),echarts圖表的尺寸重新定義一下,使用resize的方法就可以實(shí)現(xiàn)。
總體布局
<template>
<div class="aboutPage">
<div class="chartBoxWrap" ref="chartBox">
<div id="chartBox"></div>
</div>
</div>
</template>
<style lang="scss" scoped>
.chartBoxWrap {
display: flex;
height: 600px;
#chartBox {
flex: 1;
background-color: #f0faf0;
}
}
</style>
創(chuàng)建echarts圖表的方法
initChart(e){
let myChart = echarts.init(document.getElementById("chartBox"))
myChart.setOption({
title:{
text:"自適應(yīng)圖表"
},
legend:{
show:true,
icon:'circle'
},
xAxis:{
type:'category',
name:"月份",
data:e.xData
},
yAxis:{
type:'value',
axisLine:{
show:true
},
axisTick:{
show:true
}
},
series:[
{
type:"line",
smooth:"true",
data:e.data
}
]
})
window.onresize=function(){ // 當(dāng)屏幕尺寸發(fā)生變化時(shí),圖表尺寸同時(shí)發(fā)生改變
myChart.resize()
}
}使用
在mounted中調(diào)用這個(gè)方法,并且將數(shù)據(jù)傳入進(jìn)去,那么就可以實(shí)現(xiàn)自適應(yīng)echarts圖表了
后面會(huì)有整個(gè)的一個(gè)案例,可以后面一起復(fù)制過(guò)去
screenfull全屏插件
首先在項(xiàng)目中安裝這個(gè)插件,使用npm指令
npm install screenfull -S
在需要使用的組件,引入一下即可
import screenfull from "screenfull";
- 判斷是否支持全屏
isEnabled—screenfull.isEnabled,返回布爾值 - 判斷是否已進(jìn)入全屏模式
isFullscreen—screenfull.isFullscreen,返回布爾值 - 進(jìn)入全屏
request(this.$refs.refName)–screenfull.request(this.$refs.refName),里面的參數(shù)可以省略,如果省略,那么就默認(rèn)整個(gè)組件進(jìn)入全屏模式,如果想要某個(gè)部分進(jìn)入全屏,那么給這個(gè)元素定義一個(gè)ref,將其填入?yún)?shù)中,即可實(shí)現(xiàn) - 退出全屏
exit()—screenfull.exit() - 來(lái)回切換
toggle(this.$refs.refName)–screenfull.toggle(this.$refs.refName),切換全屏和非全屏,如果需要單獨(dú)切換某個(gè)部分,那么里面可以加入?yún)?shù)
使用的方法
toScreenfull(){
console.log(screenfull.isEnabled)
if(screenfull.isEnabled){ // 判斷是否支持全屏
screenfull.toggle(this.$refs.chartBox); // 使用toggle方法
}else{
this.$message.error('不支持全屏')
}
if(screenfull.isFullscreen){ // 判斷是否為全屏,如果是false就是沒有全屏
this.text="全屏"
this.iconType='el-icon-zoom-in'
}else{
this.text="退出全屏"
this.iconType='el-icon-zoom-out'
}
}自適應(yīng)圖表和screenfull案例
<template>
<div class="aboutPage">
<div class="chartBoxWrap" ref="chartBox">
<el-button type="primary" :icon="iconType" @click="toScreenfull">
{{text}}
</el-button>
<div id="chartBox"></div>
</div>
</div>
</template><script>
import * as echarts from 'echarts'
import screenfull from "screenfull";
export default {
data() {
return {
text:'全屏',
iconType:'el-icon-zoom-in',
chartData:{
xData:["一月份","二月份","三月份","四月份"],
data:[50,24,86,89]
}
}
},
methods: {
toScreenfull(){
console.log(screenfull.isEnabled)
if(screenfull.isEnabled){ // 判斷是否支持全屏
screenfull.toggle(this.$refs.chartBox); // 使用toggle方法
}else{
this.$message.error('不支持全屏')
}
if(screenfull.isFullscreen){ // 判斷是否為全屏,如果是false就是沒有全屏
this.text="全屏"
this.iconType='el-icon-zoom-in'
}else{
this.text="退出全屏"
this.iconType='el-icon-zoom-out'
}
},
initChart(e){
let myChart = echarts.init(document.getElementById("chartBox"))
myChart.setOption({
title:{
text:"自適應(yīng)圖表"
},
legend:{
show:true,
icon:'circle'
},
xAxis:{
type:'category',
name:"月份",
data:e.xData
},
yAxis:{
type:'value',
axisLine:{
show:true
},
axisTick:{
show:true
}
},
series:[
{
type:"line",
smooth:"true",
data:e.data
}
]
})
window.onresize=function(){ // 當(dāng)屏幕尺寸發(fā)生變化時(shí),圖表尺寸同時(shí)發(fā)生改變
myChart.resize()
}
}
},
mounted() {
this.$nextTick(()=>{
this.initChart(this.chartData)
})
},
}
</script><style lang="scss" scoped>
.chartBoxWrap {
display: flex;
height: 600px;
#chartBox {
flex: 1;
background-color: #f0faf0;
}
}
</style>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中解決異步交互數(shù)據(jù)出現(xiàn)延遲問(wèn)題
這篇文章主要介紹了vue中解決異步交互數(shù)據(jù)出現(xiàn)延遲問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
vue axios請(qǐng)求頻繁時(shí)取消上一次請(qǐng)求的方法
這篇文章主要介紹了vue axios請(qǐng)求頻繁時(shí)取消上一次請(qǐng)求的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
elementUI中el-dropdown的command實(shí)現(xiàn)傳遞多個(gè)參數(shù)
這篇文章主要介紹了elementUI中el-dropdown的command實(shí)現(xiàn)傳遞多個(gè)參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue3實(shí)現(xiàn)局部頁(yè)面刷新效果的示例詳解
這篇文章主要為大家詳細(xì)介紹了vue3如何采用 App.vue定義全局變量與方法并實(shí)現(xiàn)局部頁(yè)面刷新效果,文中的示例代碼講解詳細(xì),需要的可以參考一下2024-01-01
vue操作動(dòng)畫的記錄animate.css實(shí)例代碼
這篇文章主要介紹了vue操作動(dòng)畫的記錄animate.css的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04

