barChart.vue 6.08 KB
<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({seriesData,total} = {}) {
      //console.log(seriesData)
      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:{
                  color:'#01AEFE',
                }
              },
              {
                value:' 微  信 ',
                textStyle:{
                  color:'#06C406',
                }
              },
              {
                value:' 其  它 ',
                textStyle:{
                  color:'#FFAB00',
                }
              }
            ],//右侧Y轴值
            inverse: true,
            axisLine: {show: false},
            splitLine: {show: false},
            axisTick: {show: false },
            axisLabel: {
              //margin:-20,
              // nameTextStyle:{
              //   align:'left'
              // }
            }
          }, {
            show: true,
            inverse: true,
            data: [
                    {
                      value:seriesData[0],
                      textStyle:{
                        color:'#01AEFE'
                      }
                    },
                    {
                      value:seriesData[1],
                      textStyle:{
                        color:'#06C406'
                      }
                    },
                    {
                      value:seriesData[2],
                      textStyle:{
                        color:'#FFAB00'
                      }
                    }
                    ],//右侧Y轴值
            axisLabel: {
              textStyle: {fontSize: 12,},
            },
            axisLine: { show: false },
            splitLine: { show: false},
            axisTick: {show: false},
          }],
          series: [{
            name: '条',
            type: 'bar',
            yAxisIndex: 0,
            data: seriesData,
            data:[
                  {
                    value:seriesData[0],
                    itemStyle: {
                      normal: {
                        color: {
                          type: 'linear',
                          x: 0,
                          x1: 1,
                          colorStops: [{
                            offset: 0,
                            color: '#2772F4'
                          }, {
                            offset: 1,
                            color: '#00CAFE'
                          }]
                        }
                      },
                    }
                  },
                  {
                    value:seriesData[1],
                    itemStyle: {
                      normal: {
                        color: {
                          type: 'linear',
                          x: 0,
                          x1: 1,
                          colorStops: [{
                            offset: 0,
                            color: '#02C202'
                          }, {
                            offset: 1,
                            color: '#51EC51'
                          }]
                        }
                      },
                    }
                  },
                {
                  value:seriesData[2],
                  itemStyle: {
                    normal: {
                      color: {
                        type: 'linear',
                        x: 0,
                        x1: 1,
                        colorStops: [{
                          offset: 0,
                          color: '#FD8811'
                        }, {
                          offset: 1,
                          color: '#FFAB00'
                        }]
                      }
                    },
                  }
                },


              ],
            barWidth: '40%',

            label: {
              normal: {show: false}
            },
          }, {
            name: '框',
            type: 'bar',
            yAxisIndex: 1,
            barGap: '-100%',
            data: [total,total,total],//百分比
            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>