国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

VUE 單頁面使用 echart 窗口變化時的用法

瀏覽:74日期:2022-12-22 08:06:32

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

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

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

resixeFu 就是圖表變化時的方法

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

但里面有一個問題就是:每次進來當前頁面都會執行 window.addEventListener

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

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

補充知識:vue+echart圖表自適應屏幕大小、點擊側邊欄展開收縮圖表自適應大小resize

開發中用到了echart圖表,需要圖表自適應大小resize,一開始使用的方法是:

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

但是又遇到一個問題,點擊側邊欄的展開收起的時候,圖表的大小沒有自適應(因為窗口的大小沒有變化)

這里參考vue+element+admin的框架寫的自適應

VUE 單頁面使用 echart 窗口變化時的用法

一、index.vue的文件

引入chart圖表``

VUE 單頁面使用 echart 窗口變化時的用法

這里是數據

chartData: { title: { text: ’3-1(2)’, textStyle: { color: ’#979797’, fontSize: 14 } }, tooltip: { trigger: ’axis’ }, legend: { icon: ’rect’, itemWidth: 4, // 圖例標記的圖形寬度 itemHeight: 11, textStyle: { lineHeight: 65, fontSize: 14 }, data: [’郵件營銷’, ’聯盟廣告’, ’視頻廣告’, ’直接訪問’, ’搜索引擎’] }, 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: ’聯盟廣告’, 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 : : /></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() { // 據上一次觸發時間間隔 const last = +new Date() - timestamp // 上次被包裝函數被調用時間間隔 last 小于設定時間間隔 wait if (last < wait && last > 0) { timeout = setTimeout(later, wait - last) } else { timeout = null // 如果設定為immediate===true,因為開始邊界已經調用過了此處無需調用 if (!immediate) { result = func.apply(context, args) if (!timeout) context = args = null } } } return function(...args) { context = this timestamp = +new Date() const callNow = immediate && !timeout // 如果延時不存在,重新設定延時 if (!timeout) timeout = setTimeout(later, wait) if (callNow) { result = func.apply(context, args) context = args = null } return result }}

以上這篇VUE 單頁面使用 echart 窗口變化時的用法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 草草影院ccyy国产日本欧美 | 狠狠色狠狠色综合日日32 | 久久公开视频 | 久久久久久免费观看 | 欧美成人午夜视频免看 | 亚洲美女网址 | 九九九九热精品免费视频 | 国产三级精品在线观看 | 国产成人免费全部网站 | 中国人免费观看高清在线观看二区 | 国产美女精品在线 | 亚洲日韩精品欧美一区二区 | 中文国产成人精品久久一 | 久久永久免费 | 日韩午夜免费视频 | 国产一级不卡毛片 | 亚洲精品成人一区二区 | 国产一级做a爰片... | 欧美精品成人 | 亚洲成人天堂 | 九九精品免视频国产成人 | 亚洲天堂在线观看视频 | 欧美大片a一级毛片视频 | 亚洲综合综合在线 | 国产成人精品免费视频网页大全 | 俄罗斯一级成人毛片 | 亚洲国产成人久久精品图片 | 怡红院色视频在线 | 亚洲精品推荐 | 日本伊人精品一区二区三区 | 99精品久久久久久久免费看蜜月 | 91久久香蕉国产线看观看软件 | 久久亚洲视频 | 日本久久久久久 | 国产成人ay手机在线观看 | 国产真实乱子伦精品视 | 日韩免费毛片全部不收费 | 久久99精品久久久久久久不卡 | 97久久精品午夜一区二区 | 国产一区二区三区手机在线观看 | 国产精品免费_区二区三区观看 |