<template>
  <div class="chart" ref="chart"></div>
</template>

<script>
import * as echarts from 'echarts'
import { fontSize } from '@/views/groupPage/util'
export default {
  name: 'riskTypeChart', //隐患创建信息图表
  props: {
    chartData: {}
  },
  data() {
    return { myChart: null }
  },
  watch: {
    chartData(val) {
      this.$nextTick(() => {
        this.initialChart()
      })
    }
  },
  mounted() {
    //图表大小自适应
    window.addEventListener('resize', this.onResize)
  },
  methods: {
    destroyChart() {
      if (this.myChart != null) {
        this.myChart.dispose()
        this.myChart = null
      }
    },
    onResize() {
      if (this.myChart) {
        this.myChart.resize()
      }
    },
    initialChart() {
      this.destroyChart()
      let option = {
        grid: {
          left: '3%',
          right: '3%',
          top: '20%',
          bottom: '0%',
          containLabel: true
        },
        tooltip: {
          show: true,
          confine: true,
          borderWidth: 0,
          textStyle: {
            color: '#fff'
          },
          backgroundColor: 'rgba(50,50,50,0.7)',
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              type: 'solid',
              color: 'rgba(255, 255, 255, 0.3)'
            }
          }
        },
        legend: {
          show: true,
          x: 'right',
          y: '10',
          itemWidth: 12,
          itemHeight: 8,
          icon: 'inherit',
          textStyle: {
            color: '#FFFFFF'
          },
          data: [
            { name: '轻微', itemStyle: { color: '#13E3F0' } },
            { name: '重要', itemStyle: { color: '#E6C14D' } },
            { name: '重大', itemStyle: { color: '#FF2828' } }
          ]
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: true,
            axisLabel: {
              color: '#FFFFFF'
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            },
            data: this.chartData.xData
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '单位：个',
            nameTextStyle: {
              color: '#FFFFFF'
            },
            axisLabel: {
              color: '#FFFFFF'
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: 'rgba(255, 255, 255, 0.2)'
              }
            }
          }
        ],
        series: [
          {
            type: 'bar',
            stack: 'total',
            name: '重大',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#FF2828'
                },
                {
                  offset: 1,
                  color: 'rgba(151,149,70,0)'
                }
              ])
            },
            barWidth: 12,
            data: this.chartData.levelThree
          },
          {
            type: 'bar',
            stack: 'total',
            name: '重要',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#E6C14D'
                },
                {
                  offset: 1,
                  color: 'rgba(151,149,70,0)'
                }
              ])
            },
            barWidth: 12,
            data: this.chartData.levelTwo
          },
          {
            type: 'bar',
            stack: 'total',
            name: '轻微',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#13E3F0'
                },
                {
                  offset: 1,
                  color: 'rgba(151,149,70,0)'
                }
              ])
            },
            barWidth: 12,
            data: this.chartData.levelOne
          }
        ]
      }
      this.creatChart(option, this.$refs.chart)
    },
    creatChart(option, ref) {
      this.myChart = echarts.init(ref) //this.$refs.chart
      this.myChart.resize()
      this.myChart.setOption(option, {
        notMerge: true
      })
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.onResize)
    this.destroyChart()
  }
}
</script>

<style lang='scss' scoped>
.chart {
  height: 100%;
  width: 100%;
}
</style>