Newer
Older
urbanLifeline_YanAn / src / views / oneMap / Echarts / AssessmentjsEcharts.vue
@zhangqy zhangqy 9 days ago 5 KB echarts 清除定时器
<template>
  <div :id="id" style="width: 100%; height: 100%"></div>
</template>
<script>
import * as echarts from "echarts";
import { guid } from "@/utils/ruoyi";
import { reactive, toRefs, onMounted, watch, onUnmounted } from "vue";
export default {
  name: "line-chart",
  props: {
    data: Object,
    refresh: Number,
  },
  setup(props) {
    const allData = reactive({
      series: [],
      legend: [],
      id: guid(),
      chart: null,
      timer: null,
    });
    const resizeTheChart = () => {
      nextTick(() => {
        if (allData.chart) {
          allData.chart.resize();
        }
      });
    };

    const init = () => {
      let chartDom = echarts.init(document.getElementById(allData.id));

      var option;
      option = {
        color: ["#3FFFC2", "#FFF21C"],
        tooltip: {
          trigger: "axis",
        },
        grid: {
          top: 50,
          bottom: 30,
          right: 20,
        },

        xAxis: {
          data: ["11/7", "11/8", "11/9", "11/10", "11/11", "11/12"],
          axisPointer: {
            type: "shadow",
          },
          textStyle: {
            color: "#EBFEFF",
          },
          axisLine: {
            lineStyle: {
              color: "#EBFEFF",
            },
          },
        },
        yAxis: [
          {
            type: "value",
            name: "单位(个)",
            nameTextStyle: {
              color: "#eee",
            },

            axisLine: {
              lineStyle: {
                color: "#AAC1CF",
              },
            },
            splitLine: {
              lineStyle: {
                // 使用深浅的间隔色
                color: ["#184E5A"],
              },
            },
          },
          {
            type: "value",
            name: "",
            nameTextStyle: {
              color: "#ebf8ac",
            },
            position: "right",
            splitLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisLabel: {
              show: false,
              formatter: "{value} %", //右侧Y轴文字显示
              color: "#ebf8ac",
            },
          },
          {
            type: "value",
            gridIndex: 0,
            min: 0,
            max: 40,
            splitNumber: 8,
            interval: 5,
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            splitArea: {
              show: true,
              areaStyle: {
                color: ["rgba(250,250,250,0.0)", "rgba(250,250,250,0.05)"],
              },
            },
            splitLine: {
              lineStyle: {
                // 使用深浅的间隔色
                color: ["#5a9d9f"],
              },
            },
          },
        ],
        series: [
          {
            name: "处置数量",
            type: "line",
            yAxisIndex: 1, //使用的 y 轴的 index,在单个图表实例中存在多个 y轴的时候有用
            smooth: true, //平滑曲线显示
            showAllSymbol: true, //显示所有图形。
            symbol: "circle", //标记的图形为实心圆
            symbolSize: 2, //标记的大小
            itemStyle: {
              //折线拐点标志的样式
              color: "#058cff",
            },
            lineStyle: {
              color: "#2BE7ABFF",
            },

            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#1AF7AA",
                },
                {
                  offset: 1,
                  color: "#1AF7AA00",
                },
              ]),
            },
            data: [12, 12, 15, 18, 21, 8],
          },
          {
            name: "预警数量",
            type: "bar",
            barWidth: 10,
            showBackground: true,
            itemStyle: {
              // normal: {
              borderRadius: [10, 10, 0, 0], //(顺时针左上,右上,右下,左下)
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#00FFE3",
                },
                {
                  offset: 1,
                  color: "#4693EC",
                },
              ]),
              // },
            },
            data: [30, 18, 10, 25, 38, 12],
          },
        ],
      };
      option && chartDom.setOption(option, true);
      allData.chart = chartDom;
      animateChart();
    };
    watch(
      () => props.refresh,
      () => {
        if (allData.chart) {
          allData.chart.dispose();
          allData.chart = null;
        }
        setTimeout(() => {
          init();
        }, 0);
      }
    );
    // echarts动画
    function animateChart() {
      if (allData.timer) {
        clearInterval(allData.timer);
      }
      let length = props.data.xAxis.length;
      let i = 0;
      allData.timer = setInterval(() => {
        i++;
        if (i == length) i = 0;
        allData.chart.dispatchAction({
          type: "showTip",
          seriesIndex: 0,
          dataIndex: i,
        });
      }, 1500);
    }
    onMounted(() => {
      init();
      window.addEventListener("resize", resizeTheChart);
    });
    onUnmounted(() => {
      if (allData.timer) clearInterval(allData.timer);
    });
    return {
      ...toRefs(allData),
      resizeTheChart,
      init,
    };
  },
};
</script>