Vue 结合Echarts 动态刷新,适配2.0

550 阅读1分钟

数据请求以后实现前端分页,实现图表间隔2秒更新一次,实现组件切换时,清除定时器,当鼠标移入到图表中的时候图表不会产生间隔性刷新数据,移开后继续实现间隔性更新图表:

一.柱状图功能实现的 完整版-具体代码如下:

<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      startValue: 0, // 区域缩放的起点
      endValue: 9, // 区域缩放的终点
      timerId: null,
    };
  },
  mounted() {
    this.initChart();
    this.getDate();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timerId);
  },
  methods: {
    // 1. 初始化数据
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, "chalk");
      const initOption = {
        title: {
          text: "▎地区销售排行",
          left: 20,
          top: 20,
        },

        grid: {
          // 坐标网格的配置
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },
        tooltip: {
          show: true,
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            type: "bar",
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });

      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },

    // 2. 获取图表的数据
    async getDate() {
      const res = await this.$http.get("接口地址");
      //自己接口数据调整方式
      this.allData = res.data;
      this.allData.sort((a, b) => {
        // 对元素进行排序
        return b.value - a.value;
      });
      this.updateChart();
      // 启用数据向左平移的定时器
      this.startInterval();
    },

    // 3. 图表需要处理的数据
    updateChart() {
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];
      // 所有省份的数组
      const provinceArr = this.allData.map((item) => {
        return item.name;
      });
      // console.log(provinceArr);

      // 所有省份对应的销售金额
      const valueArr = this.allData.map((item) => {
        return item.value;
      });
      const dataOption = {
        xAxis: {
          data: provinceArr,
        },
        dataZoom: {
          // 实现动态数据向左平移
          show: true, // 隐藏那个进度的柱子
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: (arg) => {
                let targetColorArr = null;
                if (arg.value > 300) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0],
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1],
                  },
                ]);
              },
            },
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },

    // 4. 当浏览器大小发生改变时需要的方法
    screenAdapter() {
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },

    // 定时器实现柱状图定时数据向左平移
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    },
  },
};
</script>

<style lang="less" scoped>
</style>

二.折线图实现的功能完整版代码--具体如下:

<template>
  <div class="com-container">
    <!-- 下拉切换的主题 -->
    <div class="title" :style="comStyle">
      <span>{{ '▎ ' +  showTitle }}</span>
      <span class="iconfont title-icon" @click="showChice = !showChice" :style="comStyle">
        &#xe6eb;
      </span>

      <div class="select-con" v-show="showChice" :style="marginStyle">
        <div
          class="select-item"
          style="cursor: pointer"
          v-for="item in selectTypes"
          :key="item.key"
          @click="handleSelect(item)"
        >
          {{ item.text }}
        </div>
      </div>
    </div>

    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInstane: null,
      allData: null, // // 从服务器中获取的所有数据
      showChice: false, // 是否显示可选项
      choiceType: "map", // 显示数据的类型
      titleFontSize: 0 // 指明标题的字体大小
    };
  },
  // 使用计算属性来获取到下拉选择框的值
  computed: {
    selectTypes() {
      // 下拉框中的数据类型
      return this.allData
        ? this.allData.type.filter((item) => item.key !== this.choiceType)
        : [];
    },

    showTitle() {
      return this.allData ? this.allData[this.choiceType].title : [];
    },

    // 设置给标题的样式
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px",
      };
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize + "px",
      };
    },
  },

  mounted() {
    // 4. 调用 初始化方法 和 数据的请求
    this.initChart();
    this.getData();

    // 5. 分辨率的适配问题
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter(); // 主动调用一下实现适配效果
  },

  // 6.1 当组件销毁的时候 取消适配的监听
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
  },

  methods: {
    // 7. 点击按钮实现下拉数据的切换
    handleSelect(currentValue) {
      this.choiceType = currentValue.key;
      this.showChice = false;
      this.updateChart();
    },
    // 5.1 适配问题
    screenAdapter() {
      // 5.2 定义一个适配的实例化对象
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
      };

      // 5.3 挂载好 需要数据的配置渲染页面
      this.chartInstane.setOption(adapterOption);

      // 5.4 手动调用当前图表的resize 方法 实现手动适配效果
      this.chartInstane.resize();
    },
    // 1. 初始化图表
    initChart() {
      // 1.1 拿到图表实例
      this.chartInstane = this.$echarts.init(this.$refs.trend_ref, "chalk");

      // 1.2 定义一个原始的option对象,定义一个原不需要数据的配置项,先进行页面配置
      const initOption = {
        grid: {
          // 坐标轴的大小设置
          left: "3%",
          top: "35%",
          right: "4%",
          bottom: "1%",
          containLabel: true,
        },
        // 鼠标经过图表时的提示
        tooltip: {
          trigger: "axis",
        },
        // 控制标题文字
        legend: {
          left: 20,
          top: "15%",
          icon: "circle",
        },
        xAxis: {
          type: "category",
          boundaryGap: false, // 紧挨边缘
        },

        yAxis: {
          type: "value",
        },
      };

      // 1.3 挂载到 echarts中
      this.chartInstane.setOption(initOption);
    },

    // 2. 获取服务器数据
    async getData() {
      const { data: res } = await this.$http.get("接口地址");
      this.allData = res;

      // 2.2 获取到数据以后进行图表更新
      this.updateChart();
    },

    // 3. 更新图表
    updateChart() {
      // 定义一个颜色数组
      // 半透明的颜色值
      const colorArr1 = [
        "rgba(11, 168, 44, 0.5)",
        "rgba(44, 110, 255, 0.5)",
        "rgba(22, 242, 217, 0.5)",
        "rgba(254, 33, 30, 0.5)",
        "rgba(250, 105, 0, 0.5)",
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11, 168, 44, 0)",
        "rgba(44, 110, 255, 0)",
        "rgba(22, 242, 217, 0)",
        "rgba(254, 33, 30, 0)",
        "rgba(250, 105, 0, 0)",
      ];
      // 3.1处理数据
      const timeArr = this.allData.common.month; // 类目轴的数据 得到月份
      // this.choiceType 是用来控制当前到底展示哪一类数据
      const valueArr = this.allData[this.choiceType].data; // 地区销量趋势数据
      const seriesArr = valueArr.map((item, index) => {
        return {
          name: item.name,
          type: "line",
          data: item.data,
          stack: this.choiceType,
          areaStyle: {
            // 区域阴影面积的设置
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index],
              }, // %0的颜色值
              {
                offset: 1,
                color: colorArr2[index],
              }, // 100%的颜色值
            ]),
          },
        };
      });
      // 图例数据的处理
      const legendArr = valueArr.map((item) => item.name);
      const dataOption = {
        xAxis: {
          data: timeArr,
        },
        legend: {
          data: legendArr,
        },
        series: seriesArr,
      };

      // 3.2 挂载好 需要数据的配置渲染页面
      this.chartInstane.setOption(dataOption);
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-con {
    background-color: #222733;
  }
}
</style>