zhaowei
2 天以前 54d11f9a2f395e021486e6a31912616274b3feda
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
<template>
  <div id="openRate"></div>
</template>
 
<script>
  import * as echarts from 'echarts'
 
  export default {
    name: 'OpenRateChart',
    components: {},
    props: {
      dataSource: {
        type: Array,
      }
    },
    watch: {
      dataSource: {
        handler(value) {
          if (value) this.initChart()
        }
      }
    },
    data() {
      return {
        chartContainer: null
      }
    },
    mounted() {
      window.addEventListener('resize', this.handleWindowSizeChange)
    },
    methods: {
      initChart() {
        this.chartContainer = this.$echarts.init(document.getElementById('openRate'))
        const option = {
          tooltip: {
            trigger: 'axis',
            fontSize: this.fontSize(0.16)
          },
          xAxis: [
            {
              type: 'category',
              axisTick: {
                show: true
              },
              data: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
              axisLabel: {
                color: '#eee',
                fontSize: '80%',
                fontWeight: 'bold',
                interval: 0
              }
            },
            {
              axisLine: {
                lineStyle: {
                  color: '#eee'
                }
              }
            }
          ],
          yAxis: [
            {
              type: 'value',
              axisTick: {
                show: true
              },
              splitLine: {  //决定是否显示坐标中网格
                show: true
              },
              min: 0.0,
              position: 'left',
              axisLabel: {
                show: true,
                interval: 'auto',
                formatter: '{value}%',
                textStyle: {
                  color: '#eee',
                  fontSize: '80%'
                }
              },
              axisLine: {
                lineStyle: {
                  color: ['white'],
                  width: this.fontSize(0.01),
                  show: true
                }
              },
              show: true
            }
 
          ],
          grid: {
            left: '3%',
            right: '3%',
            bottom: '3%',
            top: '15%',
            containLabel: true
          },
          legend: {
            pageTextStyle: {
              color: '#eee',
              fontSize: this.fontSize(0.12)
            },
            top: '2%',
            right: '2%',
            data: [
              {
                name: '全厂',
                textStyle: {
                  color: '#eee',
                  fontSize: this.fontSize(0.16),
                  fontWeight: 'bold'
                }
              }
            ]
          },
          series: [
            {
              name: '全厂',
              type: 'bar',
              axisLabel: {
                show: true,
                interval: 'auto',
                formatter: '{value}%',
                textStyle: {
                  color: '#eee',
                  fontSize: this.fontSize(0.16),
                  fontWeight: 'bold'
                }
              },
              symbolSize: this.fontSize(0.16),
              data: this.dataSource,
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
                  offset: 0,
                  color: '#96ceff'
                },
                  {
                    offset: 0.6,
                    color: '#67c2ff'
                  },
                  {
                    offset: 1,
                    color: '#1195ff'
                  }], false)
              }
            }
          ]
        }
        this.chartContainer.setOption(option, true)
      },
 
      //自适应字体方法封装
      fontSize(res) {
        var docEl = document.documentElement,
          clientWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
        if (!clientWidth) return
        var fontSize = 100 * (clientWidth / 1920)
        return res * fontSize
      },
 
      handleWindowSizeChange() {
        if (this.chartContainer) this.chartContainer.resize()
      }
    }
  }
</script>
 
<style scoped>
 
</style>