vue期望值与实际值比较:折线图

2023-10-27

效果图

点击上方对应按钮,下方相应的数据图可隐藏/显示

代码:

一、

下载echarts包:终端运行

npm install echarts

二、components/HelloWorld.vue

<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>
  
  <script>
import * as echarts from "echarts";
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "1400px",
    },
    height: {
      type: String,
      default: "700px",
    },
  },
  data() {
    return {
      date: [],
      chart: null,
    };
  },
  mounted() {
      this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      var that = this;
      // x轴坐标当前日期前七天
      for (var i = 6; i >= 0; i--) {
        this.date.push(that.ShowDate(i));
      }
      this.chart = echarts.init(document.getElementById(this.id));
      this.chart.setOption({
        xAxis: {
          data: this.date,
          boundaryGap: false,
          axisTick: {
            show: false,
          },
        },
        grid: {
          left: 10,
          right: 100,
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
        },
        legend: {
          data: ["expected", "actual"],
        },
        series: [
          {
            name: "expected",
            itemStyle: {
              normal: {
                color: "#FF005A",
                lineStyle: {
                  color: "#FF005A",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            data: ["500", "1200", "800", "534", "585", "860", "700"],
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
          {
            name: "actual",
            smooth: true,
            type: "line",
            itemStyle: {
              normal: {
                color: "#3888fa",
                lineStyle: {
                  color: "#3888fa",
                  width: 2,
                },
                areaStyle: {
                  color: "#f3f8ff",
                },
              },
            },
            data: ["900", "600", "800", "1204", "585", "500", "800"],
            animationDuration: 2800,
            animationEasing: "quadraticOut",
          },
        ],
      });
    },
    // 获取当天日期的前n天
    ShowDate(date) {
      var num = date;
      let n = num;
      let d = new Date();
      let year = d.getFullYear();
      let mon = d.getMonth() + 1;
      let day = d.getDate();
      if (day <= n) {
        if (mon > 1) {
          mon = mon - 1;
        } else {
          year = year - 1;
          mon = 12;
        }
      }
      d.setDate(d.getDate() - n);
      year = d.getFullYear();
      mon = d.getMonth() + 1;
      day = d.getDate();
      let s =
        year +
        "-" +
        (mon < 10 ? "0" + mon : mon) +
        "-" +
        (day < 10 ? "0" + day : day);
      console.log(s);
      return s;
    },
  },
};
</script>
  

三、

在 components文件夹里新建mixins文件夹——>mixins文件夹里新建resize.js文件,代码如下:

import { debounce } from '@/utils'

export default {
  data() {
    return {
      $_sidebarElm: null,
      $_resizeHandler: null
    }
  },
  mounted() {
    this.$_resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    this.$_initResizeEvent()
    this.$_initSidebarResizeEvent()
  },
  beforeDestroy() {
    this.$_destroyResizeEvent()
    this.$_destroySidebarResizeEvent()
  },
  // to fixed bug when cached by keep-alive
  // https://github.com/PanJiaChen/vue-element-admin/issues/2116
  activated() {
    this.$_initResizeEvent()
    this.$_initSidebarResizeEvent()
  },
  deactivated() {
    this.$_destroyResizeEvent()
    this.$_destroySidebarResizeEvent()
  },
  methods: {
    // use $_ for mixins properties
    // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
    $_initResizeEvent() {
      window.addEventListener('resize', this.$_resizeHandler)
    },
    $_destroyResizeEvent() {
      window.removeEventListener('resize', this.$_resizeHandler)
    },
    $_sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.$_resizeHandler()
      }
    },
    $_initSidebarResizeEvent() {
      this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]
      this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)
    },
    $_destroySidebarResizeEvent() {
      this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)
    }
  }
}

四、

与components同级新建utils文件夹——>utils文件夹里新建index.js文件,代码如下:


/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
 export function debounce(func, wait, immediate) {
    let timeout, args, context, timestamp, result
  
    const later = function () {
      // 据上一次触发时间间隔
      const last = +new Date() - timestamp
  
      // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
      if (last < wait && last > 0) {
        timeout = setTimeout(later, wait - last)
      } else {
        timeout = null
        // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
        if (!immediate) {
          result = func.apply(context, args)
          if (!timeout) context = args = null
        }
      }
    }
  
    return function (...args) {
      context = this
      timestamp = +new Date()
      const callNow = immediate && !timeout
      // 如果延时不存在,重新设定延时
      if (!timeout) timeout = setTimeout(later, wait)
      if (callNow) {
        result = func.apply(context, args)
        context = args = null
      }
  
      return result
    }
  }

五、运行

npm run dev

注:

1、本案例可直接使用,本人是在HelloWorld.vue中写的应用代码,新加页面运行记得配置路由,其他文件位置放在与之相对应的位置

2、HelloWorld.vue文件里面的数据、样式可根据自己的要求改动,其余文件里的内容无需改动

部分代码详解:

1、legend中的数据为上方控制折线显示隐藏的图标,与series中的name相对应,若改变数据名称需要两处同时改

2、series中的data为对应name下的数据

3、xAxis为x轴,data为x轴展示的数据

4、yAxis为y轴

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vue期望值与实际值比较:折线图 的相关文章

随机推荐

  • Sqlilabs-16

    相较于第 15 关 单引号变成了双引号 括号 查列 uname admin and if ascii substr select group concat table name from information schema tables
  • bash: /root/.bashrc: 行 102: 语法错误: 未预期的文件结尾

    问题描述 解决方案 在添加内容的末尾加上fi
  • idea使用sonarlint插件

    JDH 邹老板 一 插件安装 由于是内网环境 根据自己安装的idea版本 去官网下载离线插件包进行离线安装 我的idea是IntelliJ IDEA 2020 2 3 安装包如下 二 sonarlint服务器配置 插件安装完成之后 在设置里
  • YaRN: Efficient Context Window Extension of Large Language Models

    本文是LLM系列文章 针对 YaRN Efficient Context Window Extension of Large Language Models 的翻译 YaRN 大型语言模型的有效上下文窗口扩展 摘要 1 引言 2 背景和相关
  • zookeeper版本选择与配置参数调优

    一 zookeeper 发布策略 Apache ZooKeeper 社区一次支持两个发布分支 stable和current ZooKeeper的稳定版本是 3 7 x 当前版本是 3 8 x 一旦发布新的次要版本 稳定版本预计将很快退役 大
  • 使用开散列实现对字典的查找插入删除(C++实现)

    一 需求分析 问题描述 实现对字典的查找 基本要求 在分块查找 AVL树 哈希查找 B树或者B 树查找中选择一种你认为最高效的动态查找方法对字典 单词 词性加释义 在内存中的动态查找结构或者在外存的字典文件的构造 查找 插入 删除 逻辑操作
  • JVM常见命令之JPS

    1 JPS Java Virtual Machine Process Status JDK1 5提供的显示当前所有进程pid的命令 2 jps q 只输出pid 3 jps l 输出应用程序main class 的完整package名或者应
  • Spring底层组件xxxAware家族

    搞懂xxxAware家族对理解Spring源码和提高代码能力也有帮助 Spring中常见xxxAware接口列举如下 ApplicationContextAware BeanNameAware EmbeddedValueResolverAw
  • 实时数仓实践以及架构

    前言 数据智能 Data Intelligence 有一个必须且基础的环节 就是数据仓库的建设 同时 数据仓库也是公司数据发展到一定规模后必然会提供的一种基础服务 从智能商业的角度来讲 数据的结果代表了用户的反馈 获取结果的及时性就显得尤为
  • IDEA在Debug模式下 对象转JSON

    如果文章对你有帮助欢迎 关注 点赞 收藏 一键三连 一起努力 IDEA在debug模式下面是不能直接复制被调试的Object为Json字符串的 但是在工作中经常会用到json入参 这里用两种方式实现这个操作 方便进行其他操作和工作交流 一
  • eds能谱图分析实例_EDS那些事儿

    什么是EDS 我们通常所说的EDS全称为能量色散X射线谱仪 简称能谱仪 可同时记录所有X射线谱 用以测量X射线强度与X射线能量的函数关系 是一种不损坏试样的快速微区成分分析方法 通过测量材料被激发的特征X射线能量进行元素的定性分析 测量特征
  • fake-useragent,python爬虫伪装请求头

    在编写爬虫进行网页数据的时候 大多数情况下 需要在请求是增加请求头 下面介绍一个python下非常好用的伪装请求头的库 fake useragent 具体使用说明如下 安装fake useragent库 pip install fake u
  • 复杂美区块链溯源系统架构

    从功能架构上 复杂美将区块链存证溯源系统按照功能划分为区块链核心层 接口层 运维管理层 溯源平台层和用户端层 1 区块链基础层 面向整个存证溯源平台提供基础信息服务 主要是为上层架构组件提供基础设施 保证上层服务可靠运行 源数据从IOT设备
  • 数据结构-链式存储

    数据结构 一 数据结构的定义 一组用来保存一种或者多种特定关系的数据集合 二 数据与数据之间的关系 lt 1 gt 数据的逻辑结构 数据元素与元素之间的关系 集合 关系平等 线性结构 元素之间一对一的关系 表 队列 栈 树形结构 元素之间一
  • 模式分类识别

    模式分类识别 DBN深度置信网络数据多特征分类预测 Matlab完整程序 目录 模式分类识别 DBN深度置信网络数据多特征分类预测 Matlab完整程序 分类结果 基本介绍 程序设计 参考资料 分类结果
  • Host文件

    linux中 etc目录 配置文件 etc目录包含了系统特有的配置文件 所谓配置文件 就是用于控制程序运行的本地文件 它绝大多情况下都说 只读 的私有文件 而且是可编辑的 这里的可编辑是指能直接看懂的 所以那些二进制可执行文件是不能作为配置
  • springboot多数据源---2事务

    一 多数据源事务控制 在多数据源下 由于涉及到数据库的多个读写 一旦发生异常就可能会导致数据不一致的情况 在这种情况希望使用事务 进行回退 但是Spring的声明式事务在一次请求线程中只能使用一个数据源进行控制 但是对于多源数据库 1 单一
  • 在webstorm 中直接运行ts文件

    原文链接 在webstorm 中直接运行ts文件 上一篇 ubuntu 使用 Apache Bench 进行并发测试 下一篇 使用js解数独难题 安装插件后重启IDE Run Configuration for TypeScript
  • Notice: Use of undefined constant submit - assumed 'submit'

    Notice Use of undefined constant submit assumed submit in D wamp www ECMS insert monitors php on line 66 Notice Undefine
  • vue期望值与实际值比较:折线图

    效果图 点击上方对应按钮 下方相应的数据图可隐藏 显示 代码 一 下载echarts包 终端运行 npm install echarts 二 components HelloWorld vue