美文网首页
vue中将Echarts封装成自定义指令v-echarts

vue中将Echarts封装成自定义指令v-echarts

作者: 回到唐朝做IT | 来源:发表于2023-06-19 13:36 被阅读0次

当项目中出现大量echarts图表展示时,每个图表都需要进行echarts初始化,引入配置项,会造成大量冗余的重复代码,因此将echarts封装成自定义指定,方便使用;

示例:直接在vue中使用 v-echarts="option"
<template>
    <div v-echarts="option" style="width:100%;height:400px" />
<template>
开始封装
1、创建自定义指令文件 utils/directives/echarts.js
//utils/directives/echarts.js
import * as echarts from 'echarts';
const options = {
  deep: true,
  // 插入父节点时调用
  inserted: function(el, binding) {
    const myChart = echarts.init(el);
    const option = binding.value;
    myChart.showLoading();
    myChart.setOption(option, true);
    myChart.hideLoading();
    window.addEventListener('resize', function() {
      myChart.resize();
    });
  },
  update: function(el, binding) {
    const myChart = echarts.getInstanceByDom(el);
    const option = binding.value;
    myChart.showLoading();
    myChart.setOption(option, true);
    myChart.hideLoading();
    window.addEventListener('resize', function() {
      myChart.resize();
    });
  }

};
export {
  options
};
2、在main.js注册成全局使用
//main.js
import { options } from '@utils/directives/echarts';
Vue.directive('echarts', options);
3、组件中使用
<template>
  <div class="box">
    <div v-echarts="option1" style="width:100%;height:400px" />
  </div>
</template>
<script>
import option from './baseOption';
export default {
  data() {
    return {
      option1: {},
      option: option
    };
  },
  mounted() {
    this.getData1();
  },
  methods: {
    getData1() {
      const option1 = {
        title: { ...this.option.title },
        tooltip: { ...this.option.tooltip },
        legend: { ...this.option.legend, orient: 'horizontal', width: '100%', top: 0, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] },
        xAxis: { ...this.option.xAxis, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] },
        yAxis: { ...this.option.yAxis },
        series: [
          {
            name: '未处理',
            type: 'bar',
            bottom: '10%',
            barWidth: 32,
            color: '#8DCBE3',
            label: { show: true, formatter: '{c}', color: 'inherit', position: 'top' },
            data: [120, 200, 150, 80, 70, 110, 130]
          }
        ]
      };
      this.option1 = option1;
    }
  }
};
</script>
4、抽离出公共的配置项baseOption.js供组件页面中引用
//baseOption.js
export default {
  title: {
    text: '',
    left: 'left',
    textStyle: {
      color: '#666666',
      fontSize: 14,
      fontWeight: 'normal'
    },
    subtext: '',
    subtextStyle: {
      color: '#CCCCCC',
      fontSize: 14,
      fontWeight: 'normal'
    }
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    itemWidth: 12,
    itemHeight: 12,
    itemGap: 16,
    icon: 'circle',
    orient: 'vertical',
    right: 'right',
    top: '10%',
    textStyle: {
      fontSize: 14,
      color: '#333'
    },
    data: []
  },
  xAxis:
  {
    type: 'category',
    axisTick: { show: false },
    axisLine: {
      lineStyle: {
        color: '#CCCCCC',
        type: 'dashed'
      }
    },
    axisLabel: {
      color: '#666666'
    },
    data: []
  },
  yAxis:
  {
    type: 'value',
    splitLine: {
      lineStyle: {
        color: '#CCCCCC',
        type: 'dashed'
      }
    }
  }
};


相关文章

  • vue的v-echarts怎么看API

    未来更高效得利用vue开发echarts相关组件,我经常会使用到v-echarts,刚开始真的摸不着头脑,这个v-...

  • season2-全局API

    第1节:Vue.directive 自定义指令 Vue.directive自定义指令 自定义的指令:changec...

  • Vue div节点滚动事件-加载更多

    使用Vue.directive注册全局指令 绑定事件 对于Vue自定义指令不明白的同学请移步: Vue自定义指令 ...

  • vue echarts 自定义指令

    组件内写法如下

  • VUE-2:自定义指令、事件

    directive自定义指令 我们还可以通过`Vue`提供的directive方法来自定义指令 注册指令 `vue...

  • vue入门6---vue基本指令、自定义指令、插件

    一、vue常用指令概览 二、vue自定义指令 注册全局指令Vue.directive('my-directive'...

  • vue自定义指令初探

    vue自定义指令初探 一、什么是自定义指令 自定义指令是用来操作DOM的。尽管Vue推崇数据驱动视图的理念,但并非...

  • vue 有自定义指令

    vue 的自定义指令,分为全局自定义指令和局部自定义指令,局部自定义指令等价于局部组件。 自定义指令可以对DOM进...

  • Vue指令钩子函数

    Vue指令上篇文章有讲过了,现在分析Vue指令钩子函数。 自定义指令 全局定义:Vue.directive( ' ...

  • vue自定义指令

    除了内置的指令外,Vue 也允许注册自定义指令。 vue用Vue.directive(id,definition)...

网友评论

      本文标题:vue中将Echarts封装成自定义指令v-echarts

      本文链接:https://www.haomeiwen.com/subject/anpeedtx.html