【Vue中使用Echarts】在Vue中优雅的使用Echarts——图表轮播图、Echarts图表组件封装、节流函数优化图表性能

news2024/9/22 15:49:24

文章目录

  • 一、为什么要优雅的使用echarts
  • 二、最初的表格组件
  • 三、初步的封装
  • 四、性能优化

一、为什么要优雅的使用echarts

为了提高代码的规范性、复用性,vue中最常用的就是将具有某些功能的代码封装到一个插件。如果没有对插件进行封装,在后期使用插件的时候效率会十分低下也不便于后期对程序的维护拓展,在优雅的使用echarts时,首先,我们考虑到多个地方需要用到echarts,就需要封装一个组件出来,由于每一个图中的属性均由配置项option进行控制,所以我们可以将option选项提取出来,在将来用到图表的时候从父组件传进来,父组件需要做的就是为图标提供一个有大小的盒子,以便于将图表放进盒子内。当然这只是粗略的想法,具体应如何做还要向下继续看。接下来的几个例子均是由App.vue组件承担父组件角色,Mycharts.vue承担子组件的角色。
在这里插入图片描述

二、最初的表格组件

这种方法也是我们在上一篇博客中介绍到的,目的就是为了让大家画出来一个表格体验到画表格的喜悦感。在实际使用过程中我们并不这么干,这样只能一个组件画出一个表格,并且后期维护非常麻烦。我们首先考虑的就是将option选项提取出来,在父组件用到图表时将其传进来然后渲染出表格。

MyCharts.vue

<template>
  <div class="hello">
  </div>
</template>

<script>
import * as echarts from "echarts"
export default {
  name: 'HelloWorld',
  mounted() {
    this.initOneEcharts();
  },
  methods: {
    initOneEcharts() {
      const option = {
        color: ["#80FFA5", "#00DDFF", "#37A2FF", "#FF0087", "#FFBF00"],
        title: {
          text: "Gradient Stacked Area Chart",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985",
            },
          },
        },
        legend: {
          data: ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5"],
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "Line 1",
            type: "line",
            stack: "Total",
            smooth: true,
            lineStyle: {
              width: 0,
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "rgb(128, 255, 165)",
                },
                {
                  offset: 1,
                  color: "rgb(1, 191, 236)",
                },
              ]),
            },
            emphasis: {
              focus: "series",
            },
            data: [140, 232, 101, 264, 90, 340, 250],
          },
          {
            name: "Line 2",
            type: "line",
            stack: "Total",
            smooth: true,
            lineStyle: {
              width: 0,
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "rgb(0, 221, 255)",
                },
                {
                  offset: 1,
                  color: "rgb(77, 119, 255)",
                },
              ]),
            },
            emphasis: {
              focus: "series",
            },
            data: [120, 282, 111, 234, 220, 340, 310],
          },
          {
            name: "Line 3",
            type: "line",
            stack: "Total",
            smooth: true,
            lineStyle: {
              width: 0,
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "rgb(55, 162, 255)",
                },
                {
                  offset: 1,
                  color: "rgb(116, 21, 219)",
                },
              ]),
            },
            emphasis: {
              focus: "series",
            },
            data: [320, 132, 201, 334, 190, 130, 220],
          },
          {
            name: "Line 4",
            type: "line",
            stack: "Total",
            smooth: true,
            lineStyle: {
              width: 0,
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "rgb(255, 0, 135)",
                },
                {
                  offset: 1,
                  color: "rgb(135, 0, 157)",
                },
              ]),
            },
            emphasis: {
              focus: "series",
            },
            data: [220, 402, 231, 134, 190, 230, 120],
          },
          {
            name: "Line 5",
            type: "line",
            stack: "Total",
            smooth: true,
            lineStyle: {
              width: 0,
            },
            showSymbol: false,
            label: {
              show: true,
              position: "top",
            },
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "rgb(255, 191, 0)",
                },
                {
                  offset: 1,
                  color: "rgb(224, 62, 76)",
                },
              ]),
            },
            emphasis: {
              focus: "series",
            },
            data: [220, 302, 181, 234, 210, 290, 150],
          },
        ],
      };
      echarts.init(document.getElementById("app")).setOption(option);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#hello{
  width: 1000px;
  height: 600px;
}
</style>

App.vue

<template>
  <div id="app"><HelloWorld/></div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue"
export default {
  name: "app",
  components:{
    HelloWorld
  }
};
</script>

<style scoped>
#app{
  width: 1000px;
  height: 600px;
}
</style>

三、初步的封装

初步的封装并没有考虑太多性能方面的东西,而是将组件提取出来,能够再父组件中使用起来,以下代码是通过简单的算法实现了一个echarts图例轮播图

当然其中有许多不足,大家只需要体会以下将其封装的感觉就好。

请添加图片描述

请添加图片描述
请添加图片描述
请添加图片描述

MyCharts.vue 封装好的图表组件

<template>
  <div id="mycharts"></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  mounted() {
    if (this.option) {
      this.initOneEcharts();
    }
  },
  data() {
    return {
      mycharts: null,
    };
  },
  props: {
    option: {
      Object,
    },
  },
  methods: {
    initOneEcharts() {
      this.mycharts = echarts.init(document.getElementById("mycharts"));
      this.mycharts.setOption(this.option);
    },
  },
  watch: {
    option() {
      // 不为空的话先销毁,然后再初始化新的
      if (this.mycharts) {
        this.mycharts.dispose();
      }
      this.initOneEcharts();
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#mycharts {
  width: 100%;
  height: 100%;
}
</style>

App.vue 将数据传向图表组件,并渲染

<template>
  <div id="app">
    <div class="charts">
      <my-charts :option="option"></my-charts>
    </div>
    <div class="footer">
      <button class="b1" @click="b1">上一个</button>
      <button class="b2" @click="b2">下一个</button>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import MyCharts from "./components/MyEcharts.vue";
export default {
  name: "app",
  components: {
    MyCharts,
  },
  data() {
    return {
      // 指向目前指向的是哪个图表
      pointer: 0,
      option: null,
      chartsList: [这里存放option对象],
    };
  },
  mounted() {
    this.option = this.chartsList[0];
  },
  methods: {
    b1() {
      console.log("后退!!");
      if (this.pointer == 0) {
        this.pointer = this.chartsList.length;
      }
      this.pointer--;
      this.option = this.chartsList[this.pointer];
    },
    b2() {
      console.log("前进!!", this.chartsList.length + 1, this.pointer);
      this.pointer = (this.pointer + 1) % this.chartsList.length;
      this.option = this.chartsList[this.pointer];
    },
  },
};
</script>

<style scoped>
#app {
  background-color: #ddd;
  text-align: center;
  width: 100%;
  height: 800px;
}
.charts {
  width: 100%;
  height: 90%;
  /* background-color: blue; */
}
.footer {
  margin-top: 20px;
  /* background-color: red; */
}
.b1 {
  margin: 0 50px;
}
</style>

四、性能优化

截止目前,我们的mycharts插件具备的以下功能:

  • 数据源与展示的模板进行了分离
  • mycharts插件可以在多个插件中使用(只需传入option配置即可)
  • 数据源发生改变时插件可以感知到然后重新渲染模板(如果是内部数据改变可以在watch中加入deep:true)

看似已经齐活了,但是从性能方面来说还有很大的差距:

  • 没有进行全局注册(哪里用到哪里引入,使用频繁就需要进行全局注册)
  • 窗口大小适配能力差,传统适配方案效率低下
  • option内容丰富,声明在data中性能差需要声明在外部

于是可以做以下调整

将封装好的插件注册为全局插件

import MyEcharts from "./components/MyEcharts"
Vue.component("MyEcharts",MyEcharts)

添加窗口改变监听事件

this.mychart.__resize = function(){
    chart.resize();
};
setTimeout(() => {
    window.addEventListener('resize',this.chart.__resize);
}, 200);

移除窗口改变监听事件

beforeDestroy() {
    // 移除窗口改变监听
    window.removeEventListener('resize',this.chart.__resize);
}

频繁的窗口改变会降低效率(使用节流函数)

mounted(){
    this.chart = echarts.init(document.getElementById(this.id));
    this.chart.setOption(this.option);
    // 节流函数,来自Lodash,这里可以自己写一个简单点的
    // 如果有多个地方用到,也可以使用引入的方式
    function throttle(func, wait, options) {
        let time, context, args, result;
        let previous = 0;
        if (!options) options = {};

        let later = function() {
            previous = options.leading === false ? 0 : new Date().getTime();
            time = null;
            func.apply(context, args);
            if (!time) context = args = null;
        };

        let throttled = function() {
            let now = new Date().getTime();
            if (!previous && options.leading === false) previous = now;
            let remaining = wait - (now - previous);
            context = this;
            args = arguments;
            if (remaining <= 0 || remaining > wait) {
                if (time) {
                    clearTimeout(time);
                    time = null;
                }
                previous = now;
                func.apply(context, args);
                if (!time) context = args = null;
            } else if (!time && options.trailing !== false) {
                time = setTimeout(later, remaining);
            }
        };
        return throttled;
    };

    var chart = this.chart;
    this.chart.__resize = throttle(function(){
        chart.resize();
    },200);

    setTimeout(() => {
        window.addEventListener('resize',this.chart.__resize);
    }, 200);
},

将数据源提出去(与vue对象分离)

<template>
  <div id="mycharts"></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: "MyEcharts",
  beforeDestroy() {
    // 移除窗口改变监听
    window.removeEventListener("resize", this.mycharts.resize);
  },
  data() {
    return {
      mycharts: null,
    };
  },
  methods: {
      //当数据变化的时候调用这个接口即可
    setOption(option) {
      if (this.mycharts){
        this.mycharts.dispose()
      }
      this.mycharts = echarts.init(document.getElementById("mycharts"));
      this.mycharts.setOption(option);
      window.addEventListener("resize", this.mycharts.resize);
    },
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#mycharts {
  width: 100%;
  height: 100%;
}
</style>

修改后完整的代码如下:

main.js

import Vue from 'vue'
import App from './App.vue'
import * as echarts from "echarts"
// 绑定在vue的原型对象上
Vue.prototype.$echarts=echarts
import MyEcharts from "./components/MyEcharts"
Vue.component("MyEcharts",MyEcharts)
// 关闭生产提示
Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#root')

App.vue

<template>
  <div id="app">
    <div class="charts">
      <my-echarts ref="chart1" :option="option"></my-echarts>     
    </div>
    <div class="footer">
      <button class="b1" @click="b1">上一个</button>
      <button class="b2" @click="b2">下一个</button>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
let chartsList=[这里存放图标配置项option]
export default {
  name: "app",
  components:{},
  data() {
    return {
      // 指向目前指向的是哪个图表
      pointer: 0,
      option: null,
    };
  },
  mounted() {
    this.$refs.chart1.setOption(chartsList[0])
  },
  methods: {
    b1() {
      console.log("后退!!");
      if (this.pointer == 0) {
        this.pointer = chartsList.length;
      }
      this.pointer--;
      // this.option = chartsList[this.pointer];
      this.$refs.chart1.setOption(chartsList[this.pointer])

    },
    b2() {
      console.log("前进!!", chartsList.length + 1, this.pointer);
      this.pointer = (this.pointer + 1) % chartsList.length;
      // this.option = chartsList[this.pointer];
      this.$refs.chart1.setOption(chartsList[this.pointer])
    },
  },
};
</script>

<style scoped>
#app {
  background-color: #ddd;
  text-align: center;
  width: 100%;
  height: 800px;
}
.charts {
  width: 100%;
  height: 90%;
  /* background-color: blue; */
}
.footer {
  margin-top: 20px;
  /* background-color: red; */
}
.b1 {
  margin: 0 50px;
}
</style>

MyEcharts.vue

<template>
  <div id="mycharts"></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: "MyEcharts",
  beforeDestroy() {
    // 移除窗口改变监听
    window.removeEventListener("resize", this.mycharts.resize);
  },
  data() {
    return {
      mycharts: null,
    };
  },
  methods: {
    setOption(option) {
      if (this.mycharts){
        this.mycharts.dispose()
      }
      this.mycharts = echarts.init(document.getElementById("mycharts"));
      this.mycharts.setOption(option);
      window.addEventListener("resize", this.mycharts.resize);
    },
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#mycharts {
  width: 100%;
  height: 100%;
}
</style>

程序到目前为止已经该有的都有了但是还是不够完善,如果大家有什么好的介意或者意见欢迎共同探讨。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/138348.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

2022年区块链白皮书详解及内容分享

目录 导 读 技术创新稳步推进 应用路径日益清晰 产业趋稳资本活跃 多地加快战略布局 白皮书具体内容如下 导 读 区块链是构建信任的基石&#xff0c;是实现信用传递&#xff0c;价值传递的可信网络。区块链聚焦构建安全、可靠的区块链基础设施&#xff0c;让企业、政府快速…

2023北京/上海/广州/深圳NPDP产品经理国际认证招生中

产品经理国际资格认证NPDP是国际公认的唯一的新产品开发专业认证&#xff0c;集理论、方法与实践为一体的全方位的知识体系&#xff0c;为公司组织层级进行规划、决策、执行提供良好的方法体系支撑。 【认证机构】 产品开发与管理协会&#xff08;PDMA&#xff09;成立于1979年…

【算法】动态规划(第五章习题解答)

5 动态规划 5.1 图书馆大门前有 nnn 级台阶, 你每次跨上 111 级或者 222 级, 请问等上 nnn 级台阶总共有多少种不同的方法? 设计一个算法求解上述问题, 尝试写出公式, 说明算法设计思想和时间复杂度. 算法设计&#xff1a;核心思路是函数的递归调用&#xff0c;当处理nnn级台…

drawImage 详解

背景&#xff1a; 在canvas的开发中 总会碰到drawImage这个API&#xff0c;但是里面有9个参数&#xff0c;具体代表什么意思有时候会把自己搞混乱了&#xff0c;特此记录一下&#xff0c;加深影响。 drawImage 方法有三种形态&#xff1a; drawImage(image, dx, dy) 在画布指…

【微服务】Nacos 配置管理模块

目录 一、Nacos 配置管理模块 1、配置⼀致性模型 2、Server 间的⼀致性协议 2.1、有 DB 模式&#xff08;读写分离架构&#xff09; 2.2、无 DB 模式 3、SDK 与 Server 的⼀致性协议 3.1、Nacos 1.X 3.2、Nacos 2.X 二、Nacos ⾼可⽤设计 1、全局高可用 2、同城容灾…

工信部部长金壮龙首次提到元宇宙,加快谋划布局未来产业

前言&#xff1a;继工业和信息化部、教育部、文化和旅游部、国家广播电视总局、国家体育总局等五大部门印发《虚拟现实与行业应用融合发展行动计划(2022—2026年)》之后&#xff0c;工信部部长金壮龙在接受新华社采访时提出&#xff0c;要加快人工智能、生物制造、物联网等战略…

质数判定,质因数分解,两种质数筛:埃氏筛、线性筛(欧拉筛)

质数判定 试除法&#xff0c;根据定义&#xff0c;枚举 [2,n−1][2,n-1][2,n−1] 中所有整数&#xff0c;看是否有能整除 nnn 的数 。 事实上&#xff0c;我们没有必要枚举出所有整数 abna\times bnabn&#xff0c;我们就说 aaa 和 bbb 是 nnn 的因数&#xff0c;所以因数都是…

【区块链-智能合约工程师】第四篇:Truffle框架安装和介绍(Windows)

文章目录第一次尝试安装&#xff08;fail&#xff09;简单介绍安装配置truffle命令报错&#xff1a;truffle 不是内部或外部命令truffle命令报错&#xff1a;Cannot find module fs/promises第二次尝试安装&#xff08;fail&#xff09;安装和配置npm安装ganache-cli节点仿真器…

Java高手速成│实战:应用数据库和GUI开发产品销售管理软件(2)

实战项目&#xff1a;应用数据库和GUI开发产品销售管理软件&#xff08;2&#xff09; 01、项目分析 改进实战项目——应用数据库和GUI开发产品销售管理软件&#xff08;1&#xff09;的设计和操作&#xff0c;并增加新的功能&#xff1b;利用按钮和文本框实现对数据库编程的…

荣耀破壁2022:蜕变的解法、蓄势的护法、进击的打法

“世上只有一种英雄主义&#xff0c;那就是在认清生活的真相后依然热爱生活”&#xff0c; 罗曼罗兰在《米开朗琪罗传》中写道。人如此&#xff0c;企业亦如此。12月30日&#xff0c;荣耀在新年致辞中提到&#xff1a;“真正的勇敢是在认清真相后依然热爱。”刚过去的2022年&am…

zOffice新年新版本!多项实用功能上线

2022年发生了很多大事&#xff0c;你们在2022年做过最有意义的事情是什么呢&#xff1f;或许这一年我们有很多不足&#xff0c;或许我们依旧还需努力&#xff0c;但是我们都将继续寻找人生的新篇章&#xff0c;在2023年到来之际&#xff0c;联想Filez也祝大家新年快乐&#xff…

都2023年了,诸佬们肯定熟知RabbitMQ了吧

前言&#xff1a;大家好&#xff0c;我是小威&#xff0c;24届毕业生&#xff0c;曾经在某央企公司实习&#xff0c;目前入职某税务公司。本篇文章将记录和分享RabbitMQ相关的知识点。 本篇文章记录的基础知识&#xff0c;适合在学Java的小白&#xff0c;也适合复习中&#xff…

EXCEL的查找:如何按 行号+列号 进行查询, 可用indirect() + match() 或 index() + match()

0 首先用match()等取得行号&#xff0c;列号 如果想根据行号列号&#xff0c;精确查找&#xff0c;另外一个区域的数据&#xff0c;可以用如下方法 INDIRECT("Sheet2!r"&MATCH($C11,Sheet2!$A:$A,0)&"C"&MATCH(D$10,Sheet2!$1:$1,0),FALSE) …

ERR_UNSAFE_PORT浏览器安全问题导致无法访问的解决方案

前言 出发点是Java Agent内存马的自动分析与查杀&#xff0c;实际上其他内存马都可以通过这种方式查杀 本文主要的难点主要是以下三个&#xff0c;我会在文中逐个解答 1.如何dump出JVM中真正的当前的字节码 2.如何解决由于LAMBDA表达式导致非法字节码无法分析的问题 3.如何对…

短视频带货流程话术

现在短视频热度高涨&#xff0c;已经成为人们娱乐休闲的一种方式。短视频人流量大&#xff0c;已经衍生出了直播带货、橱窗链接带货等方式&#xff0c;也吸引了一批想要带货的用户。前言现在短视频热度高涨&#xff0c;已经成为人们娱乐休闲的一种方式。短视频人流量大&#xf…

JDBC(使用java语言操作数据库)

JDBC概念&#xff1a;使用Java语言操作关系型数据库的一套api&#xff08;可以用一套标准的jdbc操作所有类型的数据库&#xff0c;jdbc是接口&#xff0c;每一个数据库&#xff08;mysql、oracle、db2...&#xff09;都去实现jdbc的接口&#xff0c;每个数据库的实现类都不一样…

LeetCode:11. 盛最多水的容器

11. 盛最多水的容器1&#xff09;题目2&#xff09;思路3&#xff09;代码4&#xff09;结果1&#xff09;题目 给定一个长度为 n 的整数数组 height 。有 n 条垂线&#xff0c;第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 找出其中的两条线&#xff0c;使得它们与 x …

2023年1月4日:fastadminApi接口开发项目时遇到的问题

SelectPage selectpage(官方文档&#xff1a;动态下拉(SelectPage) - FastAdmin框架文档 - FastAdmin开发文档) 常规用法 下面介绍一个基础的动态下拉列表示例&#xff0c;如下 <input id"c-name" data-rule"required" data-source"category/sel…

AVS3变换之IST和ISTS

IST&#xff08;Implicit Selected Transform&#xff09;是AVS3中新增的针对intra块的变换工具&#xff0c;IST对intra块提供了两种可分离的变换核&#xff0c;编码器根据RDO选择最优的变换核&#xff0c;但是对于选中的变换核不在码流中传输其索引&#xff0c;而是将其索引隐…

day34【代码随想录】贪心算法之跳跃游戏、跳跃游戏||、K次取反后最大化的数组和

文章目录前言一、跳跃游戏&#xff08;力扣55&#xff09;二、跳跃游戏 II&#xff08;力扣45&#xff09;三、K次取反后最大化的数组和&#xff08;力扣1005&#xff09;前言 1、跳跃游戏 2、跳跃游戏|| 3、K次取反后最大化的数组和 一、跳跃游戏&#xff08;力扣55&#xff…