最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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í)的用法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue中的組件及路由使用實(shí)例代碼詳解

    Vue中的組件及路由使用實(shí)例代碼詳解

    組件系統(tǒng)是 Vue 的一個(gè)重要概念,因?yàn)樗且环N抽象,允許我們使用小型、獨(dú)立和通常可復(fù)用的組件構(gòu)建大型應(yīng)用。這篇文章主要介紹了Vue中的組件及路由使用 ,需要的朋友可以參考下
    2019-05-05
  • vue實(shí)現(xiàn)會(huì)議室拖拽布局排座功能

    vue實(shí)現(xiàn)會(huì)議室拖拽布局排座功能

    vue-draggable-resizable-gorkys是一更強(qiáng)大的拖拽組件,可以隨意拖拽,有點(diǎn)坐標(biāo),會(huì)議室拖拽布局排座是vue-draggable結(jié)合vue-draggable-resizable-gorkys進(jìn)行開發(fā)的,本文重點(diǎn)給大家介紹vue實(shí)現(xiàn)會(huì)議室拖拽布局排座,感興趣的朋友一起看看吧
    2023-11-11
  • 前端Vue學(xué)習(xí)之購物車項(xiàng)目實(shí)戰(zhàn)記錄

    前端Vue學(xué)習(xí)之購物車項(xiàng)目實(shí)戰(zhàn)記錄

    購物車是電商必備的功能,可以讓用戶一次性購買多個(gè)商品,下面這篇文章主要給大家介紹了關(guān)于前端Vue學(xué)習(xí)之購物車項(xiàng)目的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • vue如何從后臺(tái)下載.zip壓縮包文件

    vue如何從后臺(tái)下載.zip壓縮包文件

    這篇文章主要介紹了vue如何從后臺(tái)下載.zip壓縮包文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue+element-ui添加自定義右鍵菜單的方法示例

    Vue+element-ui添加自定義右鍵菜單的方法示例

    這篇文章主要給大家介紹了關(guān)于Vue+element-ui添加自定義右鍵菜單的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • vue中sync語法糖的用法詳解

    vue中sync語法糖的用法詳解

    Vue的.sync語法糖是一個(gè)用于雙向數(shù)據(jù)綁定的指令,可以在子組件中用來監(jiān)聽父組件傳遞下來的props的變化,本文給大家介紹了在Vue中,.sync語法糖的使用方法,感興趣的朋友跟著小編一起來學(xué)習(xí)吧
    2024-01-01
  • Vue3的vue-router超詳細(xì)使用示例教程

    Vue3的vue-router超詳細(xì)使用示例教程

    這篇文章主要介紹了Vue3的vue-router超詳細(xì)使用,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • 解決Vue-cli無法編譯es6的問題

    解決Vue-cli無法編譯es6的問題

    這篇文章主要介紹了解決Vue-cli無法編譯es6的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • vue中的計(jì)算屬性的使用和vue實(shí)例的方法示例

    vue中的計(jì)算屬性的使用和vue實(shí)例的方法示例

    本篇文章主要介紹了vue計(jì)算屬性的使用和vue實(shí)例的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • Vue實(shí)現(xiàn)路由跳轉(zhuǎn)和嵌套

    Vue實(shí)現(xiàn)路由跳轉(zhuǎn)和嵌套

    本篇文章主要介紹了Vue實(shí)現(xiàn)路由跳轉(zhuǎn)和嵌套,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06

最新評(píng)論

汉沽区| 和政县| 武夷山市| 如皋市| 乌海市| 晋中市| 丰城市| 府谷县| 治多县| 博乐市| 邢台市| 扬中市| 临清市| 余干县| 咸宁市| 仪陇县| 永川市| 宜春市| 长汀县| 将乐县| 高邮市| 左权县| 定日县| 新化县| 海门市| 罗平县| 桂东县| 历史| 巴东县| 霍林郭勒市| 永胜县| 衡阳县| 屯门区| 郧西县| 噶尔县| 裕民县| 四会市| 南江县| 陕西省| 西畴县| 碌曲县|