vue echarts 组件

姜冏
• 阅读 1602
<template>
  <div style="margin-bottom: 0.5rem">
    <div
      v-if="chartData && chartData.length > 0"
      style="height: 1.98rem"
      :id="chartId"
      v-on-echart-resize
    ></div>
    <div
      style="height: 1.98rem"
      v-if="!chartData || !(chartData.length > 0)"
      class="temp-data"
    >
      <img src="../assets/img/kong1.png" alt="" />
      <div>暂无此维度数据</div>
    </div>
  </div>
</template>

<script>
const sortNum = new Date().getTime();
export default {
  props: {
    name: "",
    chartData: {
      type: Array,
    },
    xData: {
      type: Array,
    },
  },
  data() {
    return {
      myChart: null,
      colorList: ["#3DFFCA", "#0EB5FF", "#FBBF00", "#FF6926", "#707070"],
    };
  },
  computed: {
    chartId: function () {
      return this.name + new Date().getTime();
    },
  },
  /**
   * 深度监听 图表生成之后 传定时刷新数据进来 由于数据不能动态传值,所以子组件使用深度监听来监控数据变化
   */
  watch: {
    chartData: {
      deep: true,
      handler(newVal, oldVal) {
        if (this.myChart) {
          this.myChart.dispose(); //数据变换 先销毁
          this.drawLine();
        } else {
          this.drawLine();
        }
      },
    },
  },
  mounted() {
    this.drawLine();
  },
  beforeDestroy() {
    if (this.myChart) {
      this.myChart.dispose();
      this.myChart = null;
    }
  },

  methods: {
    drawLine() {
      let _this = this;
      let dataZoomLength = 100;
      if (!this.xData.length || this.xData.length > 8) {
        dataZoomLength = 10;
      }
      //构建数据
      let serOpt = [];
      for (let i = 0; i < this.chartData.length; i++) {
        let target = this.chartData[i];
        let item = {
          name: target.name,
          type: target.type,
          data: target.data,
          itemStyle: {
            normal: {
              color: this.colorList[i],
              barBorderRadius: [2, 2, 2, 2],
              label: {
                show: false, //开启显示
                position: "top", //在上方显示
                textStyle: {
                  //数值样式
                  color: "#FBBF00",
                  fontSize: 8,
                },
              },
            },
          },
          showBackground: true,
          barWidth: 13,
          backgroundStyle: {
            color: "rgba(0,0,0,0.3)",
          },
        };

        serOpt.push(item);
      }
      //构建图表
      let option = {
        tooltip: {
          // 鼠标是否可以进入悬浮框
          // 触发方式 mousemove, click, none, mousemove|click
          triggerOn: `mousemove|click`,
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器,坐标轴触发有效
            type: "shadow", // 默认为直线,可选为:'line' | 'shadow'
          },

          position: function (point, params, dom, rect, size) {
            // 鼠标坐标和提示框位置的参考坐标系是:以外层div的左上角那一点为原点,x轴向右,y轴向下
            // 提示框位置
            var x = 0; // x坐标位置
            var y = 0; // y坐标位置

            // 当前鼠标位置
            var pointX = point[0];
            var pointY = point[1];

            // 外层div大小
            // var viewWidth = size.viewSize[0];
            // var viewHeight = size.viewSize[1];

            // 提示框大小
            var boxWidth = size.contentSize[0] / 3;
            var boxHeight = size.contentSize[1] / 3;

            // boxWidth > pointX 说明鼠标左边放不下提示框
            if (boxWidth > pointX) {
              x = 5;
            } else {
              // 左边放的下
              x = pointX - boxWidth;
            }

            // boxHeight > pointY 说明鼠标上边放不下提示框
            if (boxHeight > pointY) {
              y = 5;
            } else {
              // 上边放得下
              y = pointY - boxHeight;
            }

            return [x, y];
          },
        },
        legend: {
          // data: ["目标值", "实际值"],
          top: "-1%",
          left: "5%",
          itemWidth: 6,
          itemHeight: 6,
          textStyle: {
            color: "#78D4CE", //更改坐标轴文字颜色
            fontSize: 10, //更改坐标轴文字大小
          },
        },
        grid: {
          left: "3%",
          top: "20%",
          right: "5%",
          bottom: "0%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: this.xData,
          axisLine: {
            lineStyle: {
              color: "#0E635E", //更改坐标轴颜色 #0E635E #052626
            },
          },
          axisLabel: {
            interval: 0,
            rotate: 30, //倾斜
            textStyle: {
              color: "#78D4CE", //更改坐标轴文字颜色
              fontSize: 10, //更改坐标轴文字大小
            },
          },
        },
        dataZoom: [
          {
            type: "inside", //图表下方的伸缩条
            show: true, //是否显示
            realtime: true, //拖动时,是否实时更新系列的视图
            start: 0, //伸缩条开始位置(1-100),可以随时更改
            end: dataZoomLength, //伸缩条结束位置(1-100),可以随时更改
          },
        ],
        yAxis: [
          {
            // name: "",
            type: "value",
            // min: 0,
            // max: 0.14,
            nameGap: 35,
            splitNumber: 4,
            splitLine: {
              show: true,
              lineStyle: {
                type: "dotted",
                color: "#0E635E",
              },
            },
            axisLine: {
              lineStyle: {
                color: "rgba(6, 50, 49, 0.5)", //更改坐标轴颜色#052626
              },
            },
            axisLabel: {
              textStyle: {
                color: "#78D4CE", //更改坐标轴文字颜色
                fontSize: 10, //更改坐标轴文字大小
              },
            },
          },
        ],
        series: serOpt,
      };
      // 基于准备好的dom,初始化echarts实例
      if (this.chartId && document.getElementById(this.chartId)) {
        //动态存在 找不到问题。暂时使用判断
        this.myChart = this.$echarts.init(
          document.getElementById(this.chartId)
        );
        // 绘制图表
        this.myChart.setOption(option);
      }

      window.addEventListener("resize", function () {
        if (_this.myChart && _this.myChart.resize()) {
          _this.myChart.resize();
        }
      });
    },
  },
};
</script>

<style>
</style>
点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
美凌格栋栋酱 美凌格栋栋酱
10个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Easter79 Easter79
4年前
typeScript数据类型
//布尔类型letisDone:booleanfalse;//数字类型所有数字都是浮点数numberletdecLiteral:number6;lethexLiteral:number0xf00d;letbinaryLiteral:number0b101
Stella981 Stella981
4年前
ASMSupport教程4.7 生成关系运算符
<p在java中,关系运算符是很常用的,分别是&gt;,,&lt;,&gt;,&lt;,!这六种,我们按照惯例看看我们需要生成的代码:</p<divid"scid:9D7513F9C04C4721824A2B34F0212519:dfec0f1ca2ec4ebabc9b91c161fbfa47"class"wlWri
Wesley13 Wesley13
4年前
VBox 启动虚拟机失败
在Vbox(5.0.8版本)启动Ubuntu的虚拟机时,遇到错误信息:NtCreateFile(\\Device\\VBoxDrvStub)failed:0xc000000034STATUS\_OBJECT\_NAME\_NOT\_FOUND(0retries) (rc101)Makesurethekern
Wesley13 Wesley13
4年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
4年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
4年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
4年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Wesley13 Wesley13
4年前
MBR笔记
<bochs:100000000000e\WGUI\Simclientsize(0,0)!stretchedsize(640,480)!<bochs:2b0x7c00<bochs:3c00000003740i\BIOS\$Revision:1.166$$Date:2006/08/1117
姜冏
姜冏
Lv1
已知泉路近,欲别故乡难。
文章
3
粉丝
0
获赞
0