Blame view

src/components/base/barChart.vue 5.99 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  <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: {
      setOptions({yData} = {}) {
        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
94
                    color:'#01AEFE',
95fc0103   liuqimichale   收入信息
95
96
97
                  }
                },
                {
a3e443aa   Andy   add
98
                  value:' 微  信 ',
95fc0103   liuqimichale   收入信息
99
                  textStyle:{
a3e443aa   Andy   add
100
                    color:'#06C406',
95fc0103   liuqimichale   收入信息
101
102
103
                  }
                },
                {
a3e443aa   Andy   add
104
                  value:' 其  它 ',
95fc0103   liuqimichale   收入信息
105
                  textStyle:{
a3e443aa   Andy   add
106
                    color:'#FFAB00',
95fc0103   liuqimichale   收入信息
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
                  }
                }
              ],//右侧Y轴值
              inverse: true,
              axisLine: {show: false},
              splitLine: {show: false},
              axisTick: {show: false },
              axisLabel: {
                //margin:-20,
                // nameTextStyle:{
                //   align:'left'
                // }
              }
            }, {
              show: true,
              inverse: true,
              data: [
                      {
                        value:1000,
                        textStyle:{
                          color:'#01AEFE'
                        }
                      },
                      {
                        value:200,
                        textStyle:{
                          color:'#06C406'
                        }
                      },
                      {
                        value:300,
                        textStyle:{
                          color:'#FFAB00'
                        }
                      }
                      ],//右侧Y轴值
              axisLabel: {
                textStyle: {fontSize: 12,},
              },
              axisLine: { show: false },
              splitLine: { show: false},
              axisTick: {show: false},
            }],
            series: [{
              name: '条',
              type: 'bar',
              yAxisIndex: 0,
d91b873f   liuqimichale   收入信息
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
              data: [1000,200,300],
              data:[
                    {
                      value:1000,
                      itemStyle: {
                        normal: {
                          color: {
                            type: 'linear',
                            x: 0,
                            x1: 1,
                            colorStops: [{
                              offset: 0,
                              color: '#2772F4'
                            }, {
                              offset: 1,
                              color: '#00CAFE'
                            }]
                          }
                        },
                      }
                    },
                    {
                      value:200,
                      itemStyle: {
                        normal: {
                          color: {
                            type: 'linear',
                            x: 0,
                            x1: 1,
                            colorStops: [{
                              offset: 0,
                              color: '#02C202'
                            }, {
                              offset: 1,
                              color: '#51EC51'
                            }]
                          }
                        },
                      }
                    },
                  {
                    value:300,
                    itemStyle: {
                      normal: {
                        color: {
                          type: 'linear',
                          x: 0,
                          x1: 1,
                          colorStops: [{
                            offset: 0,
                            color: '#FD8811'
                          }, {
                            offset: 1,
                            color: '#FFAB00'
                          }]
                        }
                      },
                    }
                  },
  
  
                ],
95fc0103   liuqimichale   收入信息
216
              barWidth: '40%',
d91b873f   liuqimichale   收入信息
217
  
95fc0103   liuqimichale   收入信息
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
              label: {
                normal: {show: false}
              },
            }, {
              name: '框',
              type: 'bar',
              yAxisIndex: 1,
              barGap: '-100%',
              data: [1500,1500,1500],//百分比
              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>