Blame view

src/components/base/barChart.vue 6.08 KB
95fc0103   liuqimichale   收入信息
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
  <template>
    <div :class="className" :style="{height:height,width:width}"></div>
  </template>
  
  <script>
  import echarts from 'echarts'
  
  require('echarts/theme/macarons') // echarts theme
  import {debounce} from '../../utils/debounce'
  
  export default {
    name: 'barChart',
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '100%'
      },
      autoResize: {
        type: Boolean,
        default: true
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      chartData: function () {
        this.$nextTick(()=>{
          this.initChart()
        })
      }
    },
    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: {
633692cc   liuqimichale   axios
70
      setOptions({seriesData,total} = {}) {
e30f1328   liuqimichale   地图坐标
71
        //console.log(seriesData)
95fc0103   liuqimichale   收入信息
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        this.chart.setOption(
          {
  
            grid: {
              top: 0,
              left: 0,
              right: '20',
              bottom: -10,
              containLabel: true
            },
            title:{show:false},
            xAxis: {
              show: false,//尽管显示false,但仍然占用空间containLabel为true的话[echarts bug]
              nameGap:0
            },
            yAxis: [{
              show: true,
              nameLocation:'start',
              //data: ['支付宝', '微信', '其它'],
              data: [
                {
                  value:'支付宝',
                  textStyle:{
a3e443aa   Andy   add
95
                    color:'#01AEFE',
95fc0103   liuqimichale   收入信息
96
97
98
                  }
                },
                {
a3e443aa   Andy   add
99
                  value:' 微  信 ',
95fc0103   liuqimichale   收入信息
100
                  textStyle:{
a3e443aa   Andy   add
101
                    color:'#06C406',
95fc0103   liuqimichale   收入信息
102
103
104
                  }
                },
                {
a3e443aa   Andy   add
105
                  value:' 其  它 ',
95fc0103   liuqimichale   收入信息
106
                  textStyle:{
a3e443aa   Andy   add
107
                    color:'#FFAB00',
95fc0103   liuqimichale   收入信息
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
                  }
                }
              ],//右侧Y轴值
              inverse: true,
              axisLine: {show: false},
              splitLine: {show: false},
              axisTick: {show: false },
              axisLabel: {
                //margin:-20,
                // nameTextStyle:{
                //   align:'left'
                // }
              }
            }, {
              show: true,
              inverse: true,
              data: [
                      {
633692cc   liuqimichale   axios
126
                        value:seriesData[0],
95fc0103   liuqimichale   收入信息
127
128
129
130
131
                        textStyle:{
                          color:'#01AEFE'
                        }
                      },
                      {
633692cc   liuqimichale   axios
132
                        value:seriesData[1],
95fc0103   liuqimichale   收入信息
133
134
135
136
137
                        textStyle:{
                          color:'#06C406'
                        }
                      },
                      {
633692cc   liuqimichale   axios
138
                        value:seriesData[2],
95fc0103   liuqimichale   收入信息
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
                        textStyle:{
                          color:'#FFAB00'
                        }
                      }
                      ],//右侧Y轴值
              axisLabel: {
                textStyle: {fontSize: 12,},
              },
              axisLine: { show: false },
              splitLine: { show: false},
              axisTick: {show: false},
            }],
            series: [{
              name: '条',
              type: 'bar',
              yAxisIndex: 0,
633692cc   liuqimichale   axios
155
              data: seriesData,
d91b873f   liuqimichale   收入信息
156
157
              data:[
                    {
633692cc   liuqimichale   axios
158
                      value:seriesData[0],
d91b873f   liuqimichale   收入信息
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
                      itemStyle: {
                        normal: {
                          color: {
                            type: 'linear',
                            x: 0,
                            x1: 1,
                            colorStops: [{
                              offset: 0,
                              color: '#2772F4'
                            }, {
                              offset: 1,
                              color: '#00CAFE'
                            }]
                          }
                        },
                      }
                    },
                    {
633692cc   liuqimichale   axios
177
                      value:seriesData[1],
d91b873f   liuqimichale   收入信息
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
                      itemStyle: {
                        normal: {
                          color: {
                            type: 'linear',
                            x: 0,
                            x1: 1,
                            colorStops: [{
                              offset: 0,
                              color: '#02C202'
                            }, {
                              offset: 1,
                              color: '#51EC51'
                            }]
                          }
                        },
                      }
                    },
                  {
633692cc   liuqimichale   axios
196
                    value:seriesData[2],
d91b873f   liuqimichale   收入信息
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
                    itemStyle: {
                      normal: {
                        color: {
                          type: 'linear',
                          x: 0,
                          x1: 1,
                          colorStops: [{
                            offset: 0,
                            color: '#FD8811'
                          }, {
                            offset: 1,
                            color: '#FFAB00'
                          }]
                        }
                      },
                    }
                  },
  
  
                ],
95fc0103   liuqimichale   收入信息
217
              barWidth: '40%',
d91b873f   liuqimichale   收入信息
218
  
95fc0103   liuqimichale   收入信息
219
220
221
222
223
224
225
226
              label: {
                normal: {show: false}
              },
            }, {
              name: '框',
              type: 'bar',
              yAxisIndex: 1,
              barGap: '-100%',
633692cc   liuqimichale   axios
227
              data: [total,total,total],//百分比
95fc0103   liuqimichale   收入信息
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
              barWidth: '40%',
              itemStyle: {
                normal: {
                  color: 'none',
                  borderColor: '#157ADB',
                  borderWidth: 0.5,
                  //barBorderRadius: 15,
                }
              }
            }, ]
          }
        )
      },
      initChart() {
        this.chart = echarts.init(this.$el)
        this.setOptions(this.chartData)
      },
  
    }
  }
  </script>