baotian
2024-06-04 b959135a1139fb66646523d92e5bd20c5910f283
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
<template>
  <div>
    <h3><icon-font type="icon-title_tubiao" />{{ title }}</h3>
    <div>
      <div class="echarts" ref="echart"></div>
    </div>
  </div>
</template>
 
<script>
import { Icon } from 'ant-design-vue'
import * as echarts from 'echarts'
const IconFont = Icon.createFromIconfontCN({
  scriptUrl: '//at.alicdn.com/t/c/font_3968332_p3kji0rhne.js'
})
export default {
  name: 'Echarts',
  components: { IconFont },
  props: ['title', 'data'],
  data() {
    return {
      opt: {},
      echarts: {}
    }
  },
  mounted() {
    this.initEcharts()
  },
  methods: {
    initEcharts() {
      this.myChart = echarts.init(this.$refs.echart)
      this.opt = {
        tooltip: {
          trigger: 'axis',
          formatter:
            '<a style="display:inline-block;width:10px;height:10px;background:linear-gradient(to bottom,#53F76E,#4DAC46);border-radius:50%"></a> {b}: {c}小时',
          backgroundColor: 'rgba(0,0,0,0.7)',
          textStyle: {
            color: '#fff'
          }
        },
        grid: {
          top: '10%',
          bottom: '35%'
        },
        legend: {
          icon: 'circle',
          right: 100
        },
        xAxis: {
          type: 'category',
          axisLabel: {
            color: '#000',
            interval: 0,
            rotate: 60
          },
          data: []
        },
        yAxis: {
          type: 'value',
          // name: '(h)',
          // nameTextStyle: { align: 'left', padding: [3, 30] },
          axisLine: {
            show: true,
            lineStyle: {
              color: '88879E'
            }
          },
          axisLabel: {
            margin: 30,
            color: '#000',
            formatter: function (value, index) {
              if (value != 0) return value + '小时'
              else return value
            }
          },
          splitLine: {
            lineStyle: {
              color: '#88879E'
            }
          }
        },
        series: [
          {
            name: '持续时长',
            type: 'line',
            smooth: true,
            data: [],
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                {
                  offset: 0,
                  color: '#53F76E' // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: '#4DAC46' // 100% 处的颜色
                }
              ]
            },
            symbol: 'circle',
            symbolSize: [10, 10]
          }
        ]
      }
      this.myChart.setOption(this.opt)
    }
  },
  watch: {
    data(val) {
      const xAxisData = []
      const yAxisData = []
      val.forEach((item) => {
        xAxisData.push(item.name)
        yAxisData.push(item.mtbf)
      })
      this.opt.xAxis.data = xAxisData
      this.opt.series[0].data = yAxisData
      this.myChart.setOption(this.opt)
    }
  }
}
</script>
 
<style lang="less" scoped>
.echarts {
  width: 100%;
  height: 350px;
}
</style>