VUE3中echarts图表做成响应 echarts实现响应式布局

  • 单个图表
    
         
         
          
          
    1. //获取dom节点
    2. var myChart = echarts. init( document. getElementById( 'main'));
    3. //渲染dom
    4. myChart. setOption({...})
    5. // 响应式
    6. window. onresize = function( ) {
    7. myChart. resize();
    8. }

    注意:这种方法只能对一个图表有用,如果一个页面当中同时存在多个图表的话,只会对最后一个图表生效。而我们在vue中使用的时候往往会有多个图表,虽然我们把不同的图表分为多个组件,但是还是只能对一个图表生效。所以最有效的方法就是挂载在全局。

  • 多个图表(通用方法)

        在全局挂载:


   
   
    
    
  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import router from './router/index'
  4. //引入element-ui
  5. import ElementPlus from 'element-plus'
  6. import 'element-plus/dist/index.css'
  7. const app = createApp( App)
  8. app. use( ElementPlus)
  9. app. use(router)
  10. app. mount( '#app')
  11. //方法--全局挂载
  12. app. config. globalProperties. $echartsResize = function( ref:any){
  13. window. addEventListener( 'resize', function ( ) {
  14. ref. resize()
  15. })
  16. }
  17. //属性--全局挂载
  18. app. config. globalProperties. $axios = Axios;
  19. app. config. globalProperties. $Test = "我在全局";

在组件中使用:

        在组件实例中需要从vue中引入getCurrentInstance ,再通过 getCurrentInstance 获取proxy,进而可以获取全局挂载的实例进行使用。


   
   
    
    
  1. <script setup>
  2. import { onMounted } from 'vue';
  3. import * as echarts from 'echarts';
  4. //引入getCurrentInstance
  5. import { getCurrentInstance } from 'vue'
  6. const { proxy } = getCurrentInstance();
  7. const echartInit = ( data) => {
  8. // 基于准备好的dom,初始化echarts实例
  9. var myChart = echarts. init( document. getElementById( 'main'));
  10. // 绘制图表
  11. myChart. setOption({...});
  12. // 响应式--使用
  13. proxy.$echartsResize(myChart)
  14. }
  15. onMounted( async () => {
  16. echartInit()
  17. })
  18. </script>

  • 单个图表
    
         
         
        
        
    1. //获取dom节点
    2. var myChart = echarts. init( document. getElementById( 'main'));
    3. //渲染dom
    4. myChart. setOption({...})
    5. // 响应式
    6. window. onresize = function( ) {
    7. myChart. resize();
    8. }

    注意:这种方法只能对一个图表有用,如果一个页面当中同时存在多个图表的话,只会对最后一个图表生效。而我们在vue中使用的时候往往会有多个图表,虽然我们把不同的图表分为多个组件,但是还是只能对一个图表生效。所以最有效的方法就是挂载在全局。

  • 多个图表(通用方法)

        在全局挂载:


   
   
  
  
  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import router from './router/index'
  4. //引入element-ui
  5. import ElementPlus from 'element-plus'
  6. import 'element-plus/dist/index.css'
  7. const app = createApp( App)
  8. app. use( ElementPlus)
  9. app. use(router)
  10. app. mount( '#app')
  11. //方法--全局挂载
  12. app. config. globalProperties. $echartsResize = function( ref:any){
  13. window. addEventListener( 'resize', function ( ) {
  14. ref. resize()
  15. })
  16. }
  17. //属性--全局挂载
  18. app. config. globalProperties. $axios = Axios;
  19. app. config. globalProperties. $Test = "我在全局";

在组件中使用:

        在组件实例中需要从vue中引入getCurrentInstance ,再通过 getCurrentInstance 获取proxy,进而可以获取全局挂载的实例进行使用。


   
   
  
  
  1. <script setup>
  2. import { onMounted } from 'vue';
  3. import * as echarts from 'echarts';
  4. //引入getCurrentInstance
  5. import { getCurrentInstance } from 'vue'
  6. const { proxy } = getCurrentInstance();
  7. const echartInit = ( data) => {
  8. // 基于准备好的dom,初始化echarts实例
  9. var myChart = echarts. init( document. getElementById( 'main'));
  10. // 绘制图表
  11. myChart. setOption({...});
  12. // 响应式--使用
  13. proxy.$echartsResize(myChart)
  14. }
  15. onMounted( async () => {
  16. echartInit()
  17. })
  18. </script>

猜你喜欢

转载自blog.csdn.net/weixin_64310738/article/details/129034399