BarChart.vue 4.47 KB
<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    option: {
      type: Object,
      default: () => {}
    },
    optionData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      chart: null,
      colorList: [
        '#ff7e50', '#97d3f9', '#dd70d9', '#34cf34',
        '#6497ef', '#85802b', '#D7504B', '#C6E579',
        '#F4E001', '#F0805A', '#26C0C0'
      ]
    }
  },
  watch: {
    option: {
      handler(newVal) {
        // console.log('获取配置', newVal)
        if (this.chart) {
          this.setOption()
        }
      },
      immediate: true,
      deep: true
    },
    optionData: {
      handler(newVal) {
        console.log('获取柱状数据', newVal, this.chart)
        if (this.chart) {
          this.initData()
        }
      },
      immediate: true,
      deep: true
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      const option = {
        title: {
          show: false,
          right: 'center',
          textStyle: {
            fontSize: 14
          }
        },
        tooltip: {
          trigger: 'axis'
          // formatter: function(a, b, c, d) {
          //   console.log(a, b, c, d)
          // }
        },
        grid: {
          top: '10%',
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          // axisLabel: {
          //   formatter: function(index, value) {
          //     console.log(index, value)
          //   }
          // },
          boundaryGap: [0, 0.01],
          splitNumber: 5,
          max: 'auto',
          data: [],
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          }
        },
        yAxis: {
          type: 'category',
          axisLine: {
            show: false
          },
          axisTick: {
            show: false,
            alignWithLabel: true
          },
          splitLine: {
            show: false
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        series: [
          {
            name: '标签人数分布',
            type: 'bar',
            data: [],
            barWidth: 60,
            label: {
              show: true,
              position: 'insideLeft'
            },
            itemStyle: {
              color: (params) => {
                return this.colorList[params.dataIndex]
              }
            }
          }
        ]
      }
      this.chart.setOption(option)
      if (Object.keys(this.option).length > 0) {
        this.setOption()
      }
      console.log(this.optionData)
      if (Object.keys(this.optionData).length > 0) {
        this.initData()
      }
    },
    initData() {
      console.log(123)
      console.log(this.optionData)
      const keyArray = Object.keys(this.optionData)
      const yAxis = []
      const xAxis = []
      if (typeof this.optionData[keyArray[0]] !== 'object') {
        keyArray.forEach(item => {
          yAxis.push(item)
          xAxis.push(this.optionData[item])
        })
        console.log(yAxis, xAxis)
      } else {
        keyArray.forEach(item => {
          const array = Object.keys(this.optionData[item])
          array.forEach(itemArray => {
            yAxis.push(item + ':' + itemArray)
            xAxis.push(this.optionData[item][itemArray])
          })
        })
      }
      const option = {
        yAxis: {
          data: yAxis
        },
        series: [
          {
            data: xAxis
          }
        ]
      }
      console.log(option)
      this.chart.setOption(option)
    },
    setOption() {
      this.chart.setOption(this.option)
    }
  }
}
</script>