vue中处理echarts因v-if切换后图形显示异常+实现echarts监听窗口变化而改变大小

一、处理echarts因v-if切换后图形显示异常

有时候我们需要在一个页面中使用v-if来显示不同的两个图表。
视觉效果上好像是从一个页面点击链接跳转到另一个页面,但其实原理是通过销毁和重建两个不同dom容器来实现这个效果。
可能会出现的问题:
在切换到另一个图表显示的时候,改变了窗口宽度高度,那么点击返回按钮时看到原先的echarts图形就会有一部分消失显示不完整了。
解决办法:
我们需要在返回这个按钮上加个定时器延迟,来主动触发窗口发生变化(前提是代码也有做监听窗口变化改变图形大小的操作,下面标题二会讲解)。这样图形能正确自动渲染变化一次。

methods: {
	// 关闭监控ip执行详情页
    closePerfExe () {      
      this.isShowPerfExe = false // 控制当前dom容器的显示
      // 当在监控ip详情页点击回性能分析页的时候,加个延迟主动触发窗口变化,这样窗口改变性能分析页就不会发生图表显示不完整的情况了
      // 这里的代码是关键!!!
      setTimeout( () => {
        let triggerResize = new Event('resize')
        window.dispatchEvent(triggerResize)
      },0)
    }
}
二、vue实现echarts监听窗口变化而改变大小

监听窗口的变化,echarts图形大小跟着变化。
注意:在组件销毁时记得也要移除监听。

  data () {
	return {
		myChartPerformance: '', // echarts的dom容器
		performanceOption: '' // echarts配置项option
	}	
  },
  mounted () {
  		// 一般我为了防止出现一些切换问题,都是先清除echarts再初始化
        if(this.myChartPerformance){
          this.myChartPerformance.clear()
        }
        this.myChartPerformance = echarts.init(document.getElementById('myChartPerformance'))

	   // 图表数据配置
       this.performanceOption = {
        title: {
          text: chartOptions.titleName
        },
        tooltip: {
          trigger: 'axis'
        },
        //.........
        //.........
      }
      // 设置图表数据配置
      this.myChartPerformance.setOption(this.performanceOption)
 	  //  监听窗口大小改变图表大小(先移除再监听,防止出错)
      window.removeEventListener('resize', this.resizePerformanceFun)
      window.addEventListener('resize', this.resizePerformanceFun)
  },
  beforeDestroy () {
    // 组件销毁前移除监听
	window.removeEventListener('resize', this.resizePerformanceFun)
  },
  methods : {
	resizePerformanceFun () {
       if(this.myChartPerformance){
        // console.log('窗口改变了,重新渲染图形')
        this.myChartPerformance.resize()
      }
    }
}
  
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Vue项目使用EchartsVue-Awesome-Swiper,你需要先安装它们。你可以使用npm或者yarn安装它们: ``` npm install echarts vue-awesome-swiper ``` 或者 ``` yarn add echarts vue-awesome-swiper ``` 接下来,你需要在Vue组件导入并使用它们。下面是一个例子: ```html <template> <div> <swiper :options="swiperOptions"> <swiper-slide> <div ref="chart1" class="chart"></div> </swiper-slide> <swiper-slide> <div ref="chart2" class="chart"></div> </swiper-slide> <swiper-slide> <div ref="chart3" class="chart"></div> </swiper-slide> </swiper> </div> </template> <script> import Swiper from 'vue-awesome-swiper'; import 'swiper/dist/css/swiper.css'; import echarts from 'echarts'; export default { components: { Swiper, }, data() { return { swiperOptions: { loop: true, }, }; }, mounted() { this.renderChart(this.$refs.chart1, 'chart1'); this.renderChart(this.$refs.chart2, 'chart2'); this.renderChart(this.$refs.chart3, 'chart3'); }, methods: { renderChart(container, chartId) { const chart = echarts.init(container); chart.setOption(this.getChartOption(chartId)); }, getChartOption(chartId) { // 这里根据不同的chartId生成不同的Echarts配置 // 省略具体实现代码 }, }, }; </script> ``` 这里我们使用了Vue-Awesome-Swiper来实现轮播图,每个swiper-slide里面都包含了一个Echarts图表。在mounted函数,我们通过ref获取每个chart的DOM元素,并使用Echarts渲染图表。你可以根据自己的需求修改getChartOption函数,生成不同的Echarts配置。 注意:你需要在组件引入EchartsVue-Awesome-Swiper,并且在样式引入Swiper的CSS文件。如果你使用了Vue CLI创建项目,可以在main.js全局引入echarts和swiper.css: ```javascript import Vue from 'vue'; import App from './App.vue'; import echarts from 'echarts'; import 'swiper/dist/css/swiper.css'; Vue.prototype.$echarts = echarts; new Vue({ render: (h) => h(App), }).$mount('#app'); ``` 这样,在整个应用都可以使用echarts和swiper了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值