Blame view

src/components/barChart.vue 4.9 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
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
      },
      autoResize: {
        type: Boolean,
        default: true
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null,
        sidebarElm: null
      }
    },
    watch: {
      chartData: {
        deep: true,
      }
    },
    mounted() {
      this.initChart()
      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   道闸 诱导屏
70
            boundaryGap: true,
9092f766   liuqimichale   pda
71
72
73
74
75
76
77
78
79
80
81
            axisLabel : {
              formatter: '{value}',
              textStyle: {
                color: '#59AAF7'
              }
            },
            axisLine:{
              lineStyle:{
                color:'rgba(89,170,247,.3)'
              }
            },
813cafa3   liuqimichale   bar echarts
82
83
            axisTick: {
              show: false
9092f766   liuqimichale   pda
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
            },
            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
111
112
113
114
          },
          grid: {
            left: 10,
            right: 10,
9092f766   liuqimichale   pda
115
116
            bottom: 10,
            top: 10,
813cafa3   liuqimichale   bar echarts
117
118
            containLabel: true
          },
9092f766   liuqimichale   pda
119
120
121
          // tooltip: {
          //
          // },
813cafa3   liuqimichale   bar echarts
122
          yAxis: {
9092f766   liuqimichale   pda
123
124
125
126
127
128
129
130
131
132
133
            axisLabel : {
              formatter: '{value}',
              textStyle: {
                color: '#fff'
              }
            },
            axisLine:{
              lineStyle:{
                color:'rgba(89,170,247,.3)'
              }
            },
813cafa3   liuqimichale   bar echarts
134
135
            axisTick: {
              show: false
9092f766   liuqimichale   pda
136
137
138
139
140
            },
            splitLine: {
              show: false
            },
            splitNumber:3
813cafa3   liuqimichale   bar echarts
141
142
          },
          series: [{
35615334   liuqimichale   道闸 诱导屏
143
            type: 'bar',
9092f766   liuqimichale   pda
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
            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
216
217
218
219
          }]
        })
      },
      initChart() {
9092f766   liuqimichale   pda
220
        this.chart = echarts.init(this.$el)
813cafa3   liuqimichale   bar echarts
221
222
223
224
225
        this.setOptions(this.chartData)
      }
    }
  }
  </script>