Gary Gu
10 days ago 16064d395121113d9a68fed1b83612b60385b282
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<template>
  <div
    :id="idName"
    :style="`margin-top:${top}px`"
    class="echarts w-full h-full"
  >
  </div>
</template>
 
<script>
import * as echarts from 'echarts'
export default {
  name: 'V2Echarts',
  props: {
    options: {
      type: Object,
      default: () => {},
      require: true
    },
    // 传入id名称
    idName: {
      type: String,
      default: () => {
        return ''
      },
      require: true
    },
    width: {
      type: String,
      default: () => {
        return '410'
      }
    },
    height: {
      type: String,
      default: () => {
        return '290'
      }
    },
    top: {
      type: String,
      default: () => {
        return '0'
      }
    }
  },
  data () {
    return {
      myChart: null,
      resizeObserver: null,
    }
  },
 
  watch: {
    options: {
      handler (newVal) {
        this.setChartEvents(newVal)
      },
      deep: true,
      immediate: true
    }
  },
  created() {
    this.$nextTick(this.initTheChart)
  },
  methods: {
    // 初始化图表
    initTheChart() {
      this.myChart = echarts.init(document.getElementById(this.idName))
      this.myChart.setOption(this.options, true)
      
      // 鼠标移入图表时,不为第一条时就取消第一条的高亮效果
      this.myChart.on('mouseover', v => {
        if (v.dataIndex !== 0) {
          this.myChart.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex: 0
          })
        }
        if (this.idName === 'jinzhanqushi' || this.idName === 'dailogjinzhanqushi') {
          this.myChart.setOption({
            tooltip: {
              // 鼠标在折线上,系列内横向比对,显示系列名称
              formatter: function(p) {
                const ynames = ['年度计划完成', '预计年度完成', '预计年度TBM', '预计年度钻爆']
                if (ynames.indexOf(p[v.seriesIndex].seriesName) !== -1) {
                  return p[v.seriesIndex].marker +
                  ' ' + p[v.seriesIndex].seriesName + ':' + p[v.seriesIndex].value + 'm'
                } else {
                  let dataStr = ''
                  const valMap = {};
                  p.forEach(element => {
                      const seriesName = element.seriesName; // 图例名称
                      const value = element.value; // y轴值
 
                      // 过滤无效值
                      if (value === '-') {
                          return
                      }
                      // 过滤重叠值
                      if (valMap[seriesName] === value) {
                          return
                      }
                      if (ynames.indexOf(seriesName) === -1) {
                        dataStr += `<span style="display:inline-block;margin-right:5px;width:10px;height:10px;background-color:${element.color};"></span>`
                        dataStr += `${element.seriesName}: <span style="font-size:16px;font-weight:600;">${element.data ? element.data : 0}m</span> <br>`
                        valMap[seriesName] = value;
                      }
                  });
                  return dataStr
                }
              }
            }
          })
        }
      })
      // 鼠标图表时默认显示第一条
      this.myChart.on('mouseout', v => {
        this.myChart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: 0
        })
      })
      // this.myChart.on('click', function (param) {
      //   console.log('param', param)
      // })
      // 监听窗口变化 - 只刷新最后一个图表
      window.onresize = () => {
        this.myChart.resize()
      }
      // 监听窗口变化 - 多个图表同时刷新
      window.addEventListener('resize', () => {
        this.myChart.resize()
      })
    },
    // 设置图表的事件
    setChartEvents (options) {
      // console.log(options, this.idName)
      if (!this.myChart) return
      this.myChart.setOption(options, true)
      // 刷新时默认显示第一条
      this.myChart.dispatchAction({
        type: 'highlight',
        seriesIndex: 0,
        dataIndex: 0
      })
    },
    setResize() {
      this.$nextTick(() => {
        // 使echarts尺寸重置
        if (this.myChart) {
          this.myChart.resize()
        }
      })
    }
  },
  mounted() {
    // 当屏幕改变,图表重新动态改变并渲染
    const self = this;
    const viewElem = document.body;
    // 调用实例对象的observe方法,监听整个body节点的变化,当改变窗口大小或者某个DOM节点出现或隐藏时时,就会触发回调
    this.resizeObserver = new ResizeObserver(() => {
      if (!self.myChart) return
      self.myChart.resize();
    });
    this.resizeObserver.observe(viewElem);
  },
  beforeDestroy() {
    if (this.resizeObserver) {
      this.resizeObserver.unobserve(document.body)
    }
  }
}
</script>