<template>
  <div shadow="never" class="homeBoxCard" v-loading="loading">
    <div class="height400" ref="lineChartBanlance" />
  </div>
</template>
<script>
import { computed, defineComponent, onMounted, ref,watch } from 'vue'
import { useStore } from 'vuex'
import * as echarts from 'echarts'
// import { ChartDataType } from "../../data";

export default defineComponent({
  name: 'VoltageChart2',
  props: {
    echartsData: Object,
  },
  setup(props) {
    const store = useStore()
    let lineChartBanlance = ref(null)
    const voltageChart2 = ref({})

    // 总数
    const total = computed(() => store.state.Home.worksChartData.total)
    // num
    const num = computed(() => store.state.Home.worksChartData.num)

    // 读取数据 func
    const loading = ref(true)

    
    const getData = async () => {
      var arrOld = props.echartsData
      
      let dataTime = arrOld.map((item) => {
        return item.dataTime.substring(0, 10);
      })
      let vtBalun = arrOld.map((item) => {
        return item.vtBalun*100
      })
      voltageChart2.value.dataTime = dataTime
      voltageChart2.value.vtBalun = vtBalun
    
      loading.value = false
    }
    function echarts2() {
      let myChart = echarts.init(lineChartBanlance.value)
      // 绘制图表
      myChart.setOption({
        color: ['#1187FF'],
        legend: {
          bottom: '0',
          data: ['电压不平衡度'],
        },

        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'cross' },
        },
        grid: {
          left: '20',
          right: '40',
          top: '40',
          bottom: '30',
          containLabel: true,
        },
        xAxis: {
          axisLabel: {
            color: '#444',
            fontSize: 14,
          },
          /*改变xy轴颜色*/
          axisLine: {
            lineStyle: {
              color: '#444',
              width: 1, //这里是为了突出显示加上的
            },
          },
          boundaryGap: false,
          data: voltageChart2.value.dataTime,
        },
        yAxis: {
          name: '%',
          nameTextStyle: {
            color: 'black',
            fontSize: 10,
          },
          type: 'value',
          axisTick: {
            show: true, //去除刻度线
          },
          axisLabel: {
            color: 'black', // 文本颜色
          },
          axisLine: {
            show: true, // 去除轴线
            lineStyle: {
              color: 'black',
            },
          },
          splitNumber: 5,
          splitLine: {
            show: true,
            lineStyle: {
              color: '#9d9d9d',
            },
          },
        },

        series: [
          {
            name: '电压不平衡度',
            type: 'line',
            symbolSize: 7,
            smooth: false,
            data: voltageChart2.value.vtBalun,
            markLine: {
              //最大值和最小值
              data: [
                {
                  yAxis: 30,
                  label: {
                    position: 'middle',
                    formatter: '严重三项不平衡',
                  },
                  lineStyle: {
                    width: 2,
                    color: '#FF5D1D',
                  },
                },
                {
                  yAxis: 14,
                  label: {
                    position: 'middle',
                    formatter: '不平衡度',
                  },
                  lineStyle: {
                    width: 2,
                    color: '#f2e251',
                  },
                },
              ],
            },
          },
        ],
      })
      window.addEventListener('resize', () => {
        myChart.resize()
      })
    }

    const writeValue = (val) => {
      val
      getData()
      echarts2()
    }

    //监听变化
    watch(
      () => props.echartsData,
      (newVal, oldVal, clear) => {
        // 执行异步任务，并得到关闭异步任务的 id
        // console.log(newVal)
        let id = writeValue(newVal, oldVal)
        // 如果 watch 监听被重复执行了，则会先清除上次未完成的异步任务
        clear(() => clearTimeout(id))
      },
      { lazy: true }
    )
    onMounted(() => {
      getData()
      echarts2()
    })

    return {
      lineChartBanlance,
      loading,
      total,
      num,
    }
  },
})
</script>
<style lang="scss" scoped>
.homeBoxCard {
  margin-bottom: 24px;
  ::v-deep(.el-card__header) {
    padding-left: 12px;
    padding-right: 12px;
  }
  ::v-deep(.el-card__body) {
    padding: 12px;
    font-size: 14px;
    line-height: 1.5715;
  }
  ::v-deep(.el-divider) {
    margin: 8px 0;
  }
  .num {
    font-size: 30px;
    color: #515a6e;
  }
  .height400 {
    height: 400px;
  }
}
</style>