diff --git a/package.json b/package.json index c794bb2..d9a549b 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,8 @@ "pinia": "^2.0.21", "pinia-plugin-persistedstate": "^2.1.1", "print-js": "^1.6.0", + "raf": "^3.4.1", + "resize-observer-polyfill": "^1.5.1", "sass": "^1.54.0", "splitpanes": "^3.1.1", "svg-sprite-loader": "^6.0.11", diff --git a/src/components/DataScreen/migrationEcharts/index.vue b/src/components/DataScreen/migrationEcharts/index.vue index 1148d23..55c5f9c 100644 --- a/src/components/DataScreen/migrationEcharts/index.vue +++ b/src/components/DataScreen/migrationEcharts/index.vue @@ -5,7 +5,7 @@ import { geoJson } from './map.js' import * as echarts from 'echarts' import { EChartsType } from 'echarts/core' - import { onMounted } from 'vue' + import { onMounted, onUnmounted } from "vue"; import { cityIconData } from './data.js' import logo from '@/assets/image/logo.png' const props = defineProps({ @@ -318,6 +318,11 @@ chart.setOption(options) return chart } + + onUnmounted(()=>{ + chart&&chart.dispose() + }) + onMounted(() => { chart = initChart() window.addEventListener('resize', function () { diff --git a/src/hooks/useResizeElement.ts b/src/hooks/useResizeElement.ts new file mode 100644 index 0000000..78576e5 --- /dev/null +++ b/src/hooks/useResizeElement.ts @@ -0,0 +1,39 @@ +import ResizeObserver from "resize-observer-polyfill"; +import { onBeforeUnmount } from "vue"; +import requestAnimationFrameThrottle from "@/utils/requestAnimationFrameThrottle" +export const useResizeElement = (chart, chartsRef)=>{ + let observer = null ; + let widthW = 0; + let heightW = 0; + const handleResize = (entries )=>{ + const { contentRect } = entries[0]; + let {width,height} = contentRect + width = Math.floor(width); + height = Math.floor(height); + if(widthW!==width|| heightW !== height){ + widthW = width + heightW = height + chart && chart.resize() + } + } + const addObserver = ()=>{ + observer = new ResizeObserver(requestAnimationFrameThrottle(handleResize)) + observer.observe(chartsRef) + } + + const removeObserver = ()=>{ + if (observer) { + observer.disconnect(); + observer = null; + } + chart&&chart.dispose() + } + + onBeforeUnmount(()=>{ + removeObserver() + }) + + return{ + addObserver + } +} diff --git a/src/layout/hooks/useResizeHandler.ts b/src/hooks/useResizeHandler.ts similarity index 78% rename from src/layout/hooks/useResizeHandler.ts rename to src/hooks/useResizeHandler.ts index afbfd8c..8eaa5c7 100644 --- a/src/layout/hooks/useResizeHandler.ts +++ b/src/hooks/useResizeHandler.ts @@ -4,27 +4,46 @@ import {useRoute} from "vue-router"; const { body } = document -const WIDTH = 992 // refer to Bootstrap's responsive design +const WIDTH = 800 // refer to Bootstrap's responsive design +const MAX_WIDTH = 1200 export const useResizeHandler = ()=>{ const SettingStore = useSettingStore() const route = useRoute() const device = computed(()=>{ - return SettingStore.device }) function $_isMobile(){ const rect = body.getBoundingClientRect() return rect.width - 1 < WIDTH } + + function collapse(){ + const rect = body.getBoundingClientRect() + if(rect.width - 1 > MAX_WIDTH){ + return true + }else { + return false + } + + } + function $_resizeHandler(){ if (!document.hidden) { // bool型,表示页面是否处于隐藏状态。页面隐藏包括页面在后台标签页或者浏览器最小化 const isMobile = $_isMobile() + const isCollapse = collapse() SettingStore.toggleDevice(isMobile ? 'mobile' : 'desktop') + if (isMobile) { SettingStore.closeSideBar({ withoutAnimation: true }) } + + if(!isMobile){ + SettingStore.setCollapse(isCollapse) + } + + } } onMounted(()=>{ diff --git a/src/layout/components/Mobile/index.vue b/src/layout/components/Mobile/index.vue index 85bc13f..b38bfea 100644 --- a/src/layout/components/Mobile/index.vue +++ b/src/layout/components/Mobile/index.vue @@ -4,7 +4,7 @@ diff --git a/src/views/echarts/graphEcharts/index.vue b/src/views/echarts/graphEcharts/index.vue index 9af3eb6..1436940 100644 --- a/src/views/echarts/graphEcharts/index.vue +++ b/src/views/echarts/graphEcharts/index.vue @@ -6,12 +6,11 @@ diff --git a/src/views/echarts/pieEcharts/index.vue b/src/views/echarts/pieEcharts/index.vue index 2ae4b68..967ba61 100644 --- a/src/views/echarts/pieEcharts/index.vue +++ b/src/views/echarts/pieEcharts/index.vue @@ -6,7 +6,7 @@