Blame view

src/components/barChart.vue 4.92 KB
813cafa3   liuqimichale   bar echarts
1
2
3
4
5
6
  <template>
    <div :class="className" :style="{height:height,width:width}"></div>
  </template>
  
  <script>
  import echarts from 'echarts'
813cafa3   liuqimichale   bar echarts
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  import {debounce} from '../utils/debounce'
  
  export default {
    name: 'barChart',
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
9092f766   liuqimichale   pda
22
        default: '100%'
813cafa3   liuqimichale   bar echarts
23
24
25
26
27
28
29
30
31
32
33
34
      },
      autoResize: {
        type: Boolean,
        default: true
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
974ee116   liuqimichale   道闸
35
        chart: null
813cafa3   liuqimichale   bar echarts
36
37
38
      }
    },
    watch: {
adf82b69   liuqimichale   调取接口
39
40
41
42
      chartData:function () {
        this.$nextTick(()=>{
          this.initChart()
        })
813cafa3   liuqimichale   bar echarts
43
44
45
      }
    },
    mounted() {
adf82b69   liuqimichale   调取接口
46
      // this.initChart()
813cafa3   liuqimichale   bar echarts
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
      if (this.autoResize) {
        this.__resizeHandler = debounce(() => {
          if (this.chart) {
            this.chart.resize()
          }
        }, 100)
        window.addEventListener('resize', this.__resizeHandler)
      }
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      if (this.autoResize) {
        window.removeEventListener('resize', this.__resizeHandler)
      }
  
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      setOptions({xData, yData} = {}) {
        this.chart.setOption({
          xAxis: {
35615334   liuqimichale   道闸 诱导屏
71
            boundaryGap: true,
9092f766   liuqimichale   pda
72
73
74
75
76
77
78
79
80
81
82
            axisLabel : {
              formatter: '{value}',
              textStyle: {
                color: '#59AAF7'
              }
            },
            axisLine:{
              lineStyle:{
                color:'rgba(89,170,247,.3)'
              }
            },
813cafa3   liuqimichale   bar echarts
83
84
            axisTick: {
              show: false
9092f766   liuqimichale   pda
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
            },
            splitLine: {
              show: false
            },
            data: [
              {
                value: xData[0],
                textStyle: {
                  fontSize: 12,
                  color: '#59AAF7'
                }
              },
              {
                value: xData[1],
                textStyle: {
                  fontSize: 12,
                  color: '#FFB900'
                }
              },
              {
                value: xData[2],
                textStyle: {
                  fontSize: 12,
                  color: '#FD4B15'
                }
              }
            ]
813cafa3   liuqimichale   bar echarts
112
113
114
115
          },
          grid: {
            left: 10,
            right: 10,
9092f766   liuqimichale   pda
116
            bottom: 10,
adf82b69   liuqimichale   调取接口
117
            top: 20,
813cafa3   liuqimichale   bar echarts
118
119
            containLabel: true
          },
974ee116   liuqimichale   道闸
120
121
122
          tooltip: {
  
          },
813cafa3   liuqimichale   bar echarts
123
          yAxis: {
9092f766   liuqimichale   pda
124
125
126
127
128
129
130
131
132
133
134
            axisLabel : {
              formatter: '{value}',
              textStyle: {
                color: '#fff'
              }
            },
            axisLine:{
              lineStyle:{
                color:'rgba(89,170,247,.3)'
              }
            },
813cafa3   liuqimichale   bar echarts
135
136
            axisTick: {
              show: false
9092f766   liuqimichale   pda
137
138
139
140
141
            },
            splitLine: {
              show: false
            },
            splitNumber:3
813cafa3   liuqimichale   bar echarts
142
143
          },
          series: [{
35615334   liuqimichale   道闸 诱导屏
144
            type: 'bar',
9092f766   liuqimichale   pda
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
            barWidth:25,
            data: [
              {
                name: xData[0],
                value: yData[0],
                label: {
                  show: true,
                  position: 'top',
                  textStyle: {
                    fontSize: 14,
                    color: '#01AEFE'
                  }
                },
                itemStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(
                      0, 0, 0, 1,
                      [
                        { offset: 0, color: '#635BE6' },
                        { offset: 1, color: '#6DB7EF' }
                      ]
                    )
                  }
                }
              },
              {
                name: xData[1],
                value: yData[1],
                label: {
                  show: true,
                  position: 'top',
                  textStyle: {
                    fontSize: 14,
                    color: '#FFB700'
                  }
                },
                itemStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(
                      0, 0, 0, 1,
                      [
                        { offset: 0, color: '#F9B205' },
                        { offset: 1, color: '#F2E34A' }
                      ]
                    )
                  }
                }
              },
              {
                name: xData[2],
                value: yData[2],
                label: {
                  show: true,
                  position: 'top',
                  textStyle: {
                    fontSize: 14,
                    color: '#FD4B15'
                  }
                },
                itemStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(
                      0, 0, 0, 1,
                      [
                        { offset: 0, color: '#F36C00' },
                        { offset: 1, color: '#F4C5A8' }
                      ]
                    )
                  }
                }
              }
            ]
813cafa3   liuqimichale   bar echarts
217
218
219
220
          }]
        })
      },
      initChart() {
9092f766   liuqimichale   pda
221
        this.chart = echarts.init(this.$el)
813cafa3   liuqimichale   bar echarts
222
223
224
225
226
        this.setOptions(this.chartData)
      }
    }
  }
  </script>