ZhangXianQiang
2024-04-28 d387848d15a40fb16c8a6eefb007d5f7411c5dbc
src/views/home/data-chart/index.vue
@@ -5,7 +5,7 @@
        <div class="title-container">
          <h1>运维监控报表</h1>
          <div class="select-container">
            <el-select v-model="company" placeholder="请选择运维公司">
            <el-select v-model="company" placeholder="请选择运维公司" @change="companyChange">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
@@ -27,6 +27,7 @@
<script>
import * as echarts from 'echarts';
let lineChart = null;
let observer = null;
export default {
  name: 'DataChart',
  data() {
@@ -45,6 +46,26 @@
          state: { '1月': 1000, '2月': 2131, '3月': 1233, '4月': 2132, '5月': 3211 },
          state2: { '1月': 123, '2月': 213, '3月': 1412, '4月': 23, '5月': 123 }
        },
        {
          name: 'XX运维公司2',
          state: { '1月': 213, '2月': 2131, '3月': 1233, '4月': 2132, '5月': 3211 },
          state2: { '1月': 123, '2月': 123, '3月': 1412, '4月': 23, '5月': 123 }
        },
        {
          name: 'XX运维公司3',
          state: { '1月': 1000, '2月': 2131, '3月': 1233, '4月': 2132, '5月': 3211, '6月': 1321 },
          state2: { '1月': 123, '2月': 213, '3月': 123, '4月': 23, '5月': 123 }
        },
        {
          name: 'XX运维公司4',
          state: { '1月': 1000, '2月': 2131, '3月': 1233, '4月': 2132, '5月': 3211 },
          state2: { '1月': 123, '2月': 613, '3月': 1412, '4月': 2336, '5月': 123 }
        },
        {
          name: 'XX运维公司5',
          state: { '1月': 1000, '2月': 433, '3月': 1233, '4月': 2132, '5月': 8886 },
          state2: { '1月': 123, '2月': 213, '3月': 1412, '4月': 23, '5月': 123 }
        },
      ],
    }
  },
@@ -52,12 +73,12 @@
    initChart() {
      const option = {
        legend: {
          right: '2%',
          top: '5%',
          right: 'right',
          top: 'top',
          orient: "vertical",
          icon: 'rect',
          data: [
            {
              name: '正常数',
              itemStyle: {
                color: 'rgba(62, 144, 247, 1)'
@@ -106,21 +127,45 @@
      lineChart.setOption(option, true);
    },
    companyChange() {
      this.acitveData = this.dataList.find((item) => {
        return item.name === this.company;
      });
      if (this.acitveData) {
        this.initChart();
      }
    },
    // 监听变化
    observe() {
      if (!observer) {
        observer = new ResizeObserver(entries => {
          this.handleResize();
        })
      }
      observer.observe(this.$refs.chartContent);
    },
    // 窗口变换
    handleResize() {
      if (lineChart) {
        lineChart.resize()
        lineChart.resize();
      }
    }
  },
  mounted() {
    this.acitveData = this.dataList[0];
    this.company = this.acitveData.name;
    lineChart = echarts.init(this.$refs.chartContent);
    this.initChart();
    window.addEventListener('resize', this.handleResize)
    this.observe();
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
    if (lineChart) {
      lineChart.dispose();
      observer.unobserve(this.$refs.chartContent);
    }
  },
}
</script>
@@ -148,7 +193,6 @@
  align-items: center;
  z-index: 2;
  .more-button {
    cursor: pointer;
    font-size: 16px;
@@ -171,4 +215,4 @@
  margin: 0 20px;
  width: 180px;
}
</style>
</style>