<template>
  <div class="app-container-YHPC">
    <div id="YHPCChart" :style="{height:height,width:width}"></div>
  </div>
</template>

<script>
  import { homeData } from "@/api/production/accident";
  import resize from "./mixins/resize";

  export default {
    mixins: [resize],
    name: "YhpcChart",
    props: {
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '200px'
      }
    },
    data() {
      return {
        //显示表格数据
        chartsList: null,
        // 查询参数
        queryParams: {},
        // 获取当前年份
        searchFormField: {
          year: this.getNowTime(),
        },
        chart: null
      }
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    mounted(){
      this.$nextTick(() => {
        this.queryParams.year = 2020;
        homeData(this.queryParams).then(response => {
          this.chartsList = response;
          this.initChart()
        });
      })
    },
    methods: {
      /** 获取当前年份 */
      getNowTime() {
        var now = new Date();
        var year = now.getFullYear(); //得到年份
        var defaultDate = `${year}`;
        return defaultDate;
        this.$set(this.searchFormField, "year", defaultDate);
      },
      /** 图表 */
      initChart(){
        // 基于准备好的dom，初始化echarts实例
        this.chart = this.echarts.init(document.getElementById('YHPCChart'))
        // 绘制图表
        this.chart.setOption({
          color: ['#6be5ea', '#fff4f4'],
          /* 周围边距 */
          grid: {
            left: '3%',
            right: '3%',
            bottom: '1%',
            top: '10%',
            containLabel: true
          },
          /* 坐标轴显示 */
          tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
              type: 'cross',
              label: {
                backgroundColor: '#a0a0a0'
              }
            }
          },
          xAxis: {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
            axisLabel: {//x轴文字的配置
              show: true,
              textStyle: {
                color: '#ffffff',
              }
            },
            axisLine: {//x轴线的颜色以及宽度
              show: true,
              lineStyle: {
                fontSize: 10,
                color: '#ffffff',
                width: 0,
                type: "solid"
              }
            },
          },
          yAxis: {
            type: 'value',
            axisLabel: {
              formatter: '{value}',
              textStyle: {
                fontSize: 10,
                color: '#ffffffff',
                margin: 15
              },
            },
          },
          series: [{
            symbolSize: 8,   //折线点的大小
            itemStyle: {
              normal: {
                lineStyle: {
                  width: 2// 0.1的线条是非常细的了
                }
              }
            },
            data: [this.chartsList.jan, this.chartsList.feb, this.chartsList.mar, this.chartsList.apr, this.chartsList.may, this.chartsList.jun, this.chartsList.jul, this.chartsList.aug, this.chartsList.sep, this.chartsList.oct, this.chartsList.nov, this.chartsList.dec, this.chartsList.ytd, this.chartsList.target],
            type: 'line'
          }]
        });
      }
    }
  }
</script>

<style scoped>

</style>
