Newer
Older
Nanping_sponge_SJJC / src / components / Detailbox / bengzhan / baobiaoecharts.vue
@liyingjing liyingjing on 25 Oct 2023 4 KB 数据检测
<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,
  nextTick,
  onBeforeUnmount,
} from "vue";
export default {
  name: "qcswCharts",
  props: {
    data: Object,
    refresh: Number,
  },
  setup (props) {
    const state = reactive({
      id: guid(),
      chart: null,
    });
    const resizeTheChart = () => {
      if (state.chart) {
        state.chart.resize();
      }
    };
    const init = () => {
      console.log(props, "props");

      let chartDom = echarts.init(document.getElementById(state.id));
      var option;
      option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          backgroundColor: "#082e57",
          borderColor: "#00D2FF",
          textStyle: {
            fontSize: 14,
            fontWeight: 500,
            color: "#BFDDD5",
          },
        },
        grid: {
          top: "15%",
          left: "8%",
          right: "8%",
          bottom: "20%",
          // containLabel: true,
        },
        color: ["#2980E0", "#1FDAC5"],
        // color: ["red", "black", "yellow", "pink", "cyan"],
        legend: {
          textStyle: {
            color: "#E8F7FF",
          },

          itemHeight: 10,
          itemWidth: 10,
          icon: "circle",
          // left: "60%",
          // top: "5%",
          data: props.data.yinziName,
        },

        xAxis: {
          type: "category",
          // name: "小时",
          data: props.data.XAxis,
          boundaryGap: false,
          splitLine: {
            show: false,
          },
          axisLabel: {
            color: "#DADADA",
          },
        },
        yAxis: [
          {
            type: "value",
            name: "m",
            alignTicks: true,
            // min: 0,
            min: function (value) {
              return value.min;
            },
            nameTextStyle: {
              color: "#298AFF",
            },
            axisLine: {
              show: true,
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dotted",
                color: "#0E6BA5",
              },
            },
            axisLabel: {
              color: "#298AFF",
            },
          },
        ],
        series: [
          {
            name: props.data.yinziName[0],
            data: props.data.YAxis,
            type: "line",
            smooth: true,
            yAxisIndex: 0,
            showSymbol: false,
            itemStyle: {
              normal: {
                color: "#2980E0",
                lineStyle: {
                  color: "#2980E0",
                },
              },
            },
          },
          {
            name: props.data.yinziName[1],
            data: props.data.YAxis2,
            // data: [1, 1, 5, 6, 8, 9, 9, 4],
            type: "line",
            smooth: true,
            yAxisIndex: 0,
            showSymbol: false,
            itemStyle: {
              normal: {
                color: "#1FDAC5",
                lineStyle: {
                  color: "#1FDAC5",
                },
              },
            },
          },
        ],
      };
      option && chartDom.setOption(option, true);
      state.chart = chartDom;
    };
    watch(
      () => props.refresh,
      () => {
        if (state.chartDom) {
          state.chartDom.dispose();
          state.chartDom = null;
        }
        setTimeout(() => {
          init();
        }, 0);
      }
    );

    onMounted(() => {
      // setTimeout(() => {
      //   init();
      // }, 1000);
      nextTick(() => {
        init();
      });
      window.addEventListener("resize", resizeTheChart);
    });
    onBeforeUnmount(() => {
      if (state.chart) {
        state.chart.dispose();
      }
    });
    return {
      ...toRefs(state),
      resizeTheChart,
      init,
    };
  },
};
</script>

<style></style>