zmc
2023-10-12 f3acb8ce787f3df0eda633031473be4e6a9ff448
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!-- 异常情况的 折线图组件
子组件有基本的样式 
使用同一个图形实例,接受父组件传入的折线图option
**父组件
 <ExceptionTypeLineChart
        :option="option"
        :is-open-dialog="centerDialogVisible"
        v-loading="chartLoading"
      ></ExceptionTypeLineChart>
 -->
<template>
  <div  id="main" class="line-chart"></div>
</template>
 
<script>
import * as echarts from 'echarts';
 
export default {
  props: {
    option:{
      type:Object,
      default(){
        return {}
      }
    },
    isOpenDialog:{
      type:Boolean
    },
 
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
     // 获取页面宽度的一半
     
    this.initChart();
    this.chart.clear
    this.chart.setOption(this.option,true)
    window.addEventListener('resize', this.resizeChart);
  },
  watch: {
    option(){
      // this.chart.dispose;
      // this.initChart();
      // this.chart.clear
      // 不与之前的option进行合并
      this.chart.setOption(this.option,true)
    },
    isOpenDialog(){
      window.addEventListener('resize', this.resizeChart);
    },
  },
  beforeUnmount() {
    if (this.chart) {
      this.chart.dispose;
    }
  },
  methods: {
    initChart() {
      // 创建echarts实例
      this.chart = echarts.init(document.getElementById('main'));
      // 定义图表的配置项和数据
      const option = {
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        tooltip: {},
        toolbox: {
          // 工具栏
          feature: {
            // dataZoom: {
            //   // 区域缩放
            //   yAxisIndex: 'none'
            // },
            // 保存为图片
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'time',
          data: [],
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            name: '油烟浓度',
            type: 'line',
            data: []
          }
        ]
      };
      // 使用刚指定的配置项和数据显示图表
      this.chart.setOption(option, true);
    },
 
    // 跟页面响应式变化
    resizeChart() {
      this.$nextTick(() => {
        if (this.chart) {
          this.chart.resize();
        }
      });
      // this.chart.resize();
    }
  }
};
</script>
 
 
<style>
.line-chart {
  width:920px;
  height: 300px;
  margin-bottom: 20px;
  /* margin-left: 10px; */
  min-width: 600px;
}
</style>