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

<script>
import * as echarts from 'echarts'
import { fontSize } from '@/views/groupPage/util'
export default {
  name: 'riskLevelChart', //上报统计图表
  props: {
    chartData: {}
  },
  data() {
    return { myChart: null }
  },
  watch: {
    chartData(val) {
      this.$nextTick(() => {
        this.initialChart()
      })
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initialChart()
    })
    //图表大小自适应
    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() {
      let option = {
        grid: {
          left: '5%',
          right: '5%',
          top: '20%',
          bottom: '5%',
          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',
          icon: 'inherit',
          itemWidth: 15,
          itemHeight: 10,
          textStyle: {
            color: '#FFFFFF'
          },
          data: ['高风险作业']
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: true,
            axisLabel: {
              color: '#FFFFFF'
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            },
            data: ['09/15', '09/16', '09/17', '09/18', '09/19', '09/20', '09/21']
          }
        ],
        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: [
          {
            name: '高风险作业',
            type: 'line',
            symbol: 'circle',
            symbolSize: 8,
            showSymbol: false,
            smooth: true,
            itemStyle: {
              color: '#0EA7FF'
            },
            lineStyle: {
              color: '#0EA7FF',
              width: 2
            },
            data: [20, 32, 10, 34, 90, 30, 20]
          },
          {
            type: 'bar',
            stack: 'total',
            name: '一级',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#ED2727'
                },
                {
                  offset: 1,
                  color: 'rgba(151,149,70,0)'
                }
              ])
            },
            barWidth: 12,
            data: [20, 82, 91, 34, 20, 30, 30]
          },
          {
            type: 'bar',
            stack: 'total',
            name: '二级',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#D6791F'
                },
                {
                  offset: 1,
                  color: 'rgba(151,149,70,0)'
                }
              ])
            },
            barWidth: 12,
            data: [22, 12, 11, 24, 29, 30, 10]
          },
          {
            type: 'bar',
            stack: 'total',
            name: '三级',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#EFA82D'
                },
                {
                  offset: 1,
                  color: 'rgba(151,149,70,0)'
                }
              ])
            },
            barWidth: 12,
            data: [30, 18, 19, 34, 29, 33, 31]
          },
          {
            type: 'bar',
            stack: 'total',
            name: '四级',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#979546'
                },
                {
                  offset: 1,
                  color: 'rgba(151,149,70,0)'
                }
              ])
            },
            barWidth: 12,
            data: [42, 82, 11, 34, 20, 30, 10]
          }
        ]
      }
      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>