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

<script>
import * as echarts from 'echarts'
import { fontSize } from '@/views/groupPage/util'
export default {
  name: 'designAndConsChart', //设计施工综合图表
  props: {
    chartData: { default: () => {} }
  },
  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() {
      this.destroyChart()
      let legendData = this.chartData.legendData
      let xData = this.chartData.xData
      let spvData = this.chartData.spvData
      let consData = this.chartData.consData
      let yAxisName = this.chartData.yAxisName
      let seriesName = this.chartData.seriesName || null
      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: legendData
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: true,
            axisLabel: {
              color: '#FFFFFF',
              interval: 0
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            },
            data: xData
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: yAxisName,
            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: seriesName ? seriesName[0] : '监理',
            type: 'line',
            symbol: 'circle',
            symbolSize: 6,
            showSymbol: false,
            smooth: true,
            itemStyle: {
              color: '#FFD553'
            },
            lineStyle: {
              color: '#FFD553',
              width: 1
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: 'rgba(7,44,90,0.3)'
                },
                {
                  offset: 1,
                  color: 'rgba(255, 213, 83, 1)'
                }
              ])
            },
            data: spvData
          },
          {
            name: seriesName ? seriesName[1] : '施工',
            type: 'line',
            symbol: 'circle',
            symbolSize: 6,
            showSymbol: false,
            smooth: true,
            itemStyle: {
              color: '#0EA7FF'
            },
            lineStyle: {
              color: '#0EA7FF',
              width: 1
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: 'rgba(7,44,90,0.3)'
                },
                {
                  offset: 1,
                  color: 'rgba(14, 167, 255, 1)'
                }
              ])
            },
            data: consData
          }
        ]
      }
      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>