
<template>
  <el-card shadow="always" class="common-card-container common-card-container-body-no-padding">
    <div slot="header">
      <span class="card-title">电池平均SOC</span>
    </div>
    <div style="height: 360px" id="dcpjsocChart"/>
  </el-card>
</template>

<style scoped lang="scss"></style>
<script>
import * as echarts from 'echarts'
import resize from '@/mixins/ems/resize'
import {formatDate} from "@/filters/ems";
import {batteryAveSoc} from '@/api/ems/dzjk'
export default {
  mixins: [resize],
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.chart = echarts.init(document.querySelector('#dcpjsocChart'))
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    init(siteId){
      this.chart.showLoading()
      const x = []
      const data =[]
      batteryAveSoc(siteId).then(response => {
        const source = response?.data?.batteryAveSOCList || []
        source.forEach(item=>{
          x.push(formatDate(item.createDate,false,true))
          data.push(item.batterySOC)
        })
        this.setOption(x,data)
      }).finally(()=>{
        this.chart.hideLoading()
      })
    },
    setOption(x,data) {
      this.chart.setOption({
        color:['#FFBD00','#3C81FF'],
        // legend: {
        //   left: 'center',
        //   bottom: '10',
        // },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: "15%"
        },
        textStyle:{
          color:"#333333",
        },
        xAxis: {type:'category',data:x},
        yAxis: {
          type: 'value',
        },
        dataZoom: [
          {
            type: 'inside',
            start: 0,
            end: 10
          },
          {
            start: 0,
            end: 10
          }
        ],
        series: [
          {
            name:'电池平均SOC',
            data: data,
            type: 'line',
            areaStyle: {
              color:'#FFBD00'
            }

          }]
      })
    }
  }
}

</script>
