DESKTOP-GJP2N7J\lx
2023-08-11 49cd08187c9c49df199a954bd0a5a5bca40f8f1a
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
<!--********************************************************
 
    *FileName:      lineChart.vue
    *Description:   折线 图表
 
********************************************************-->
 
<template>
  <div :class="className" :style="{ width: width, height: height }"></div>
</template>
 
<script>
import * as echarts from 'echarts'
export default {
  name: 'LineChart',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      immediate: true,
      deep: true,
      handler(newVal, oldVal) {
        if (this.chart) {
          if (newVal) {
            this.setOptions(newVal)
          } else {
            this.setOptions(oldVal)
          }
        }
      }
    }
  },
  mounted() {
    this.initChart()
    if (this.chart) {
      window.addEventListener('resize', this.$_handleResizeChart)
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return false
    }
    this.chart.dispose()
    this.chart = null
    window.removeEventListener('resize', this.$_handleResizeChart)
  },
  methods: {
    $_handleResizeChart() {
      this.chart.resize()
    },
    setOptions({ time, Data, config } = {}) {
      const option = {
        color: config.color,
        legend: {
          top: '0',
          // itemGap: 50,
          data: config.name,
          textStyle: {
            color: config.textStyleColor || '#5470c6',
            fontSize: config.textStyleSize || 10
          },
          // 图例大小
          itemHeight: config.itemSize || 10
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        grid: {
          containLabel: true,
          left: '5%',
          top: '15%',
          right: '5%',
          bottom: '2%'
        },
        xAxis: {
          type: 'category',
          // boundaryGap: false,
          axisLine: {
            show: true,
            lineStyle: {
              color: config.XlineColor,
              width: 1,
              type: 'solid'
            }
          },
          axisLabel: { textStyle: { color: '#808EA3', fontWeight: 'blod' },rotate: config.xRotate },
          axisTick: {
            show: false
          },
          data: time
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              color: config.YlineColor,
              type: 'solid'
            }
          },
          axisLabel: { textStyle: { color: '#808EA3' } }
        },
        series: Data
      }
      this.chart.setOption(option)
    },
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    }
  }
}
</script>