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

export default defineComponent({
  name: 'ElectricChart',
  props: {
    echartsData: Object,
    electricChartData:Object
  },
  setup(props) {
    let lineChartBanlance = ref(null)
     const electricChart=ref({})

    // 读取数据 func
    const loading = ref(true)
    const getData = async () => {

      console.log('props.electricChartData.value')
      console.log(props.electricChartData)
     
      var arrOld = props.echartsData
      let dataTime = arrOld.map((item) => {
        return item.dataTime
      })
      let la = arrOld.map((item) => {
        return item.ia
      })
      let lb = arrOld.map((item) => {
        return item.ib
      })
      let lc = arrOld.map((item) => {
        return item.ic
      })

      electricChart.value.dataTime = dataTime
      electricChart.value.la = la
      electricChart.value.lb = lb
      electricChart.value.lc = lc
      loading.value = false
    }
    function echarts2(){
      let myChart = echarts.init(lineChartBanlance.value)
      // 绘制图表
      myChart.setOption({
        // backgroundColor: "pink",
        color: ['#f2e251', '#48C964', '#fe8161'],

        legend: {
          bottom: '0',
          data: ['A相电流', 'B相电流', 'C相电流'],
        },
        // toolbox: {
        //   show: false,
        // },
        tooltip: {
          // show: true
          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: electricChart.value.dataTime,
        },
        yAxis: {
          name: 'A',
          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: 'A相电流',
            type: 'line',
            symbolSize: 7,
            smooth: false,
            data:electricChart.value.la,
          },
          {
            name: 'B相电流',
            type: 'line',
            symbolSize: 7,
            smooth: false,
            data: electricChart.value.lb,
          },
          {
            name: 'C相电流',
            type: 'line',
            symbolSize: 7,
            smooth: false,
            data:electricChart.value.lc,
          },
        ],
      })
      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,
     
      
    }
  },
})
</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>