欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VUE 單頁面使用 echart 窗口變化時(shí)的用法

 更新時(shí)間:2020年07月30日 15:01:46   作者:qq_25186543  
這篇文章主要介紹了VUE 單頁面使用 echart 窗口變化時(shí)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

在 VUE 項(xiàng)目中,為了使 echart 在窗口變化時(shí)能夠自適應(yīng),要用到 window.resize = function(){ .......};

但是我在項(xiàng)目剛開始的時(shí)間就有一個(gè)地方的高度變化使用了 window.resize ,在里面再次使用 會(huì)覆蓋掉原來的,所以在里面圖表使用時(shí)可以用

window.addEventListener('resize',this.resizeFu,false);

resixeFu 就是圖表變化時(shí)的方法

resizeFu(){
 let div = document.getElementById('changeData');
 if(div && this.changeData.DataTime.length>0){
 this.chartsDiv.changeData.resize();
 }
}

但里面有一個(gè)問題就是:每次進(jìn)來當(dāng)前頁面都會(huì)執(zhí)行 window.addEventListener

解決方法是在路由勾子函數(shù)中把它給去掉,方法是

beforeRouteLeave(to, from, next) {
 //頁面走掉把事件給清除掉
 window.removeEventListener("resize", this.resizeFu,false);
 next()
},

補(bǔ)充知識(shí):vue+echart圖表自適應(yīng)屏幕大小、點(diǎn)擊側(cè)邊欄展開收縮圖表自適應(yīng)大小resize

開發(fā)中用到了echart圖表,需要圖表自適應(yīng)大小resize,一開始使用的方法是:

window.onresize = function () {
    this.myChart.resize();
};

但是又遇到一個(gè)問題,點(diǎn)擊側(cè)邊欄的展開收起的時(shí)候,圖表的大小沒有自適應(yīng)(因?yàn)榇翱诘拇笮]有變化)

這里參考vue+element+admin的框架寫的自適應(yīng)

一、index.vue的文件

引入chart圖表``

這里是數(shù)據(jù)

chartData: {
    title: {
     text: '3-1(2)',
     textStyle: {
      color: '#979797',
      fontSize: 14
     }
    },
    tooltip: {
     trigger: 'axis'
    },
    legend: {
     icon: 'rect',
     itemWidth: 4, // 圖例標(biāo)記的圖形寬度
     itemHeight: 11,
     textStyle: {
      lineHeight: 65,
      fontSize: 14
     },
     data: ['郵件營銷', '聯(lián)盟廣告', '視頻廣告', '直接訪問', '搜索引擎']
    },
    grid: {
     left: '3%',
     right: '4%',
     bottom: '3%',
     containLabel: true
    },
    xAxis: {
     type: 'category',
     boundaryGap: false,
     data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: {
     type: 'value'
    },
    series: [
     {
      name: '郵件營銷',
      type: 'line',
      stack: '總量',
      data: [0, 132, 101, 134, 90, 230, 210]
     },
     {
      name: '聯(lián)盟廣告',
      type: 'line',
      stack: '總量',
      data: [220, 12, 191, 234, 20, 330, 10]
     },
     {
      name: '視頻廣告',
      type: 'line',
      stack: '總量',
      data: [15, 232, 201, 154, 190, 330, 110]
     },
     {
      name: '直接訪問',
      type: 'line',
      stack: '總量',
      data: [320, 420, 301, 334, 60, 330, 320]
     },
     {
      name: '搜索引擎',
      type: 'line',
      stack: '總量',
      data: [820, 932, 901, 934, 1290, 1330, 1320]
     }
    ]
 }

二、chart.vue

<template>
 <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
 mixins: [resize],
 props: {
  className: {
   type: String,
   default: 'chart'
  },
  width: {
   type: String,
   default: '100%'
  },
  height: {
   type: String,
   default: '300px'
  },
  autoResize: {
   type: Boolean,
   default: true
  },
  chartData: {
   type: Object,
   required: true
  }
 },
 data() {
  return {
   chart: null
  }
 },
 watch: {
  chartData: {
   deep: true,
   handler(val) {
    this.setOptions(val)
   }
  }
 },
 mounted() {
  this.$nextTick(() => {
   this.initChart()
  })
 },
 beforeDestroy() {
  if (!this.chart) {
   return
  }
  this.chart.dispose()
  this.chart = null
 },
 methods: {
  initChart() {
   this.chart = echarts.init(this.$el, 'macarons')
   this.setOptions(this.chartData)
  },
  setOptions(chartData) {
   this.chart.setOption(chartData)
  }
 }
}
</script>

三、resize.js

import { debounce } from './debounce'

export default {
 data() {
  return {
   $_sidebarElm: null
  }
 },
 mounted() {
  this.$_initResizeEvent()
  this.$_initSidebarResizeEvent()
 },
 beforeDestroy() {
  this.$_destroyResizeEvent()
  this.$_destroySidebarResizeEvent()
 },
 // to fixed bug when cached by keep-alive
 // https://github.com/PanJiaChen/vue-element-admin/issues/2116
 activated() {
  this.$_initResizeEvent()
  this.$_initSidebarResizeEvent()
 },
 deactivated() {
  this.$_destroyResizeEvent()
  this.$_destroySidebarResizeEvent()
 },
 methods: {
  // use $_ for mixins properties
  // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
  $_resizeHandler() {
   return debounce(() => {
    if (this.chart) {
     this.chart.resize()
    }
   }, 100)()
  },
  $_initResizeEvent() {
   window.addEventListener('resize', this.$_resizeHandler)
  },
  $_destroyResizeEvent() {
   window.removeEventListener('resize', this.$_resizeHandler)
  },
  $_sidebarResizeHandler(e) {
   if (e.propertyName === 'width') {
    this.$_resizeHandler()
   }
  },
  $_initSidebarResizeEvent() {
   this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]
   this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)
  },
  $_destroySidebarResizeEvent() {
   this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)
  }
 }
}

四、debounce.js

/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
 let timeout, args, context, timestamp, result

 const later = function() {
  // 據(jù)上一次觸發(fā)時(shí)間間隔
  const last = +new Date() - timestamp

  // 上次被包裝函數(shù)被調(diào)用時(shí)間間隔 last 小于設(shè)定時(shí)間間隔 wait
  if (last < wait && last > 0) {
   timeout = setTimeout(later, wait - last)
  } else {
   timeout = null
   // 如果設(shè)定為immediate===true,因?yàn)殚_始邊界已經(jīng)調(diào)用過了此處無需調(diào)用
   if (!immediate) {
    result = func.apply(context, args)
    if (!timeout) context = args = null
   }
  }
 }

 return function(...args) {
  context = this
  timestamp = +new Date()
  const callNow = immediate && !timeout
  // 如果延時(shí)不存在,重新設(shè)定延時(shí)
  if (!timeout) timeout = setTimeout(later, wait)
  if (callNow) {
   result = func.apply(context, args)
   context = args = null
  }

  return result
 }
}

以上這篇VUE 單頁面使用 echart 窗口變化時(shí)的用法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue3如何定義變量及ref、reactive、toRefs特性說明

    vue3如何定義變量及ref、reactive、toRefs特性說明

    這篇文章主要介紹了vue3如何定義變量及ref、reactive、toRefs特性說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Vue中props報(bào)錯(cuò)問題解決方案

    Vue中props報(bào)錯(cuò)問題解決方案

    這篇文章主要介紹了Vue中props報(bào)錯(cuò)問題解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Vue路由前后端設(shè)計(jì)總結(jié)

    Vue路由前后端設(shè)計(jì)總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于Vue路由前后端設(shè)計(jì)的知識(shí)點(diǎn)總結(jié)內(nèi)容,需要的朋友們參考下。
    2019-08-08
  • 利用SpringMVC過濾器解決vue跨域請(qǐng)求的問題

    利用SpringMVC過濾器解決vue跨域請(qǐng)求的問題

    下面小編就為大家分享一篇利用SpringMVC過濾器解決vue跨域請(qǐng)求的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • Vue熱更新出現(xiàn)內(nèi)存溢出的解決方法

    Vue熱更新出現(xiàn)內(nèi)存溢出的解決方法

    開發(fā)項(xiàng)目有一段時(shí)間了,隨著項(xiàng)目越來越大,打包的時(shí)間也相應(yīng)的變長(zhǎng)了,打包時(shí)的內(nèi)存也增多了,這時(shí)候產(chǎn)生了一個(gè)問題,在發(fā)布項(xiàng)目的時(shí)候,會(huì)出現(xiàn)Vue熱更新出現(xiàn)內(nèi)存溢出的問題,所以本文給大家介紹了Vue熱更新出現(xiàn)內(nèi)存溢出的解決方法,需要的朋友可以參考下
    2024-05-05
  • vue?demi支持sfc方式的vue2vue3通用庫開發(fā)詳解

    vue?demi支持sfc方式的vue2vue3通用庫開發(fā)詳解

    這篇文章主要為大家介紹了vue?demi支持sfc方式的vue2vue3通用庫開發(fā)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Vue數(shù)據(jù)劫持詳情介紹

    Vue數(shù)據(jù)劫持詳情介紹

    這篇文章主要介紹了Vue數(shù)據(jù)劫持詳情介紹,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助
    2022-08-08
  • 關(guān)于vue-treeselect綁值、回顯等常見問題的總結(jié)

    關(guān)于vue-treeselect綁值、回顯等常見問題的總結(jié)

    這篇文章主要介紹了關(guān)于vue-treeselect綁值、回顯等常見問題的總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 使用Vue實(shí)現(xiàn)帶拖動(dòng)和播放功能的時(shí)間軸

    使用Vue實(shí)現(xiàn)帶拖動(dòng)和播放功能的時(shí)間軸

    這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)帶拖動(dòng)和播放功能的時(shí)間軸,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • Vue中路由守衛(wèi)的具體使用

    Vue中路由守衛(wèi)的具體使用

    導(dǎo)航守衛(wèi)就是路由跳轉(zhuǎn)前、中、后過程中的一些鉤子函數(shù),本文詳細(xì)的介紹了Vue中路由守衛(wèi)的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-12-12

最新評(píng)論