quanyawei
2023-09-19 7c5c458225e1e6ab32bba352c5efd33c6a78f023
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>
 
<script>
// import echarts from 'echarts'
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
 
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '350px',
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    interval: {
      type: Number,
      default: 1,
    },
    // xdata: {
    //   type: Array,
    //   required: true
    // }
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      },
    },
    // xdata: {
    //   deep: true,
    //   handler(val) {
    //     console.log(val)
    //     this.setOptions(val)
    //     console.log('执行成功')
    //   }
    // }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(val) {
      function fontSize(res) {
        let clientWidth =
          window.innerWidth ||
          document.documentElement.clientWidth ||
          document.body.clientWidth
        if (!clientWidth) return
        let fontSize = 100 * (clientWidth / 1920)
        return res * fontSize
      }
      let that = this
      this.chart.setOption(
        {
          title: {
            text: val.text,
            textStyle: {
              color: '#000000',
            },
          },
          xAxis: {
            data: val.xAxis,
            boundaryGap: false,
            axisTick: {
              show: true,
            },
          },
          grid: {
            left: '5%',
            right: '5%',
            top: '5%',
            containLabel: true,
            height: fontSize(4.3),
          },
          toolbox: {
            feature: {
              dataZoom: {
                yAxisIndex: 'none',
              },
              restore: {},
              saveAsImage: {},
            },
          },
          tooltip: {
            backgroundColor: 'rgba(50,50,50,0.5)',
            borderWidth: '0',
            trigger: 'axis',
            formatter: function (a) {
              let list = []
              let listItem = ''
              for (var i = 0; i < a.length; i++) {
                list.push(
                  '<span style="display: inline-block;padding: 0px 0;" >' +
                  '<i style="display: inline-block;width: 10px;height: 10px;background: ' +
                  a[i].color +
                  ';border-radius: 50%;}"></i><span style="width:15px; display:inline-block;">' +
                  '</span>' +
                  a[i].seriesName +
                  '&nbsp&nbsp&nbsp&nbsp' +
                  a[i].value +
                  '</span>'
                )
              }
              listItem = list.join('<br/>')
              return ' <div div style = "width:15px; display:inline-block;" > ' + a[0].name +
                '</div>' + '<div style="padding:0px;">' + listItem + '</div>'
            },
            position: function (point, params, dom, rect, size) {
              //其中point为当前鼠标的位置,size中有两个属性:viewSize和contentSize,分别为外层div和tooltip提示框的大小
              let x = point[0];//
              let y = point[1];
              let viewWidth = size.viewSize[0];
              let viewHeight = size.viewSize[1];
              let boxWidth = size.contentSize[0];
              let boxHeight = size.contentSize[1];
              let posX = 0;//x坐标位置
              let posY = 0;//y坐标位置
 
              if (x < boxWidth) {//左边放不开
                posX = 5;
              } else {//左边放的下
                posX = x - boxWidth;
              }
 
              if (y < boxHeight) {//上边放不开
                posY = 5;
              } else {//上边放得下
                posY = y - boxHeight;
              }
              if (params && params.length > 20) {
                posX = point[1],
                  posY = '-10%';
              }
              return [posX, posY];
 
            },
            axisPointer: {
              type: 'cross',
              label: {
                backgroundColor: '#6a7985'
              }
            },
            textStyle: {
              color: 'rgb(255, 255, 255);',
              fontSize: fontSize(0.117), // 字体大小
              lineHeight: 0
            }
          },
 
          yAxis: {
            axisTick: {
              show: false, // 轴线刻度
            },
            // axisLine: {
            //   lineStyle: {
            //     color: '#000000'
            //   }
            // }
          },
          legend: {
            data: val.title,
            tooltip: {
              show: true,
            },
            textStyle: {
              fontSize: fontSize(0.15),
            },
            widht: 'auto',
            height: 'auto',
            top: fontSize(5.5),
          },
          dataZoom: [
            {
              type: 'inside',
              start: 0,
              end: 100,
            },
            {
              start: 0,
              end: 20,
              top: fontSize(4.8),
              height: fontSize(0.5),
            },
          ],
          series: val.series,
        },
        true
      )
      window.onresize = this.chart.resize
    },
  },
}
</script>