<template>
  <div style="width: 100%;height: 100%">
    <div id="temperaturecharts"></div>
  </div>
</template>

<script>
  import resize from '../../../../components/Charts/mixins/resize'
  import echarts from 'echarts'
  /*import {mapState} from 'vuex'*/

  export default {
    mixins: [resize],
    watch: {
/*      temperatureObj: {
        handler(newValue, oldValue) {
          this.draw(newValue.time, newValue.data)
        },
        deep: true
      },*/

      temperatureData: {
        handler(newValue, oldValue) {
          this.draw(newValue.time, newValue.data)
        },
        deep: true
      }
    },
    props:{
      temperatureData:{
        type:Object
      }
    },

/*    computed: {
      ...mapState({
        temperatureObj: 'temperatureObj',
      })
    },*/
    data() {
      return {
        lineColor:'',
        chart: null,
      }
    },
    mounted() {
      if(sessionStorage.getItem('styleSwitch') === 'blue'){
        this.lineColor = 'white'
      }else{
        this.lineColor = '#3b3b3b'
      }
     /* this.draw(this.temperatureData.time, this.temperatureData.data)*/
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      draw(timeaxis, temperatureData) {
        var airT = []
        var cellT = []
        if(temperatureData !=null){
          if(temperatureData.airTs !=null){
            airT = temperatureData.airTs
          }

          if(temperatureData.cellTs !=null){
            cellT = temperatureData.cellTs
          }
        }



        this.chart = echarts.init(document.getElementById('temperaturecharts'))

        this.chart.setOption({
          backgroundColor: 'transparent',
          title: {
            top: 20,
            text: '气象信息-温度',
            textStyle: {
              fontWeight: 'normal',
              fontSize: 16,
              color: this.lineColor
            },
            left: '1%'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              lineStyle: {
                color: '#57617B'
              }
            }
          },
          legend: {
            top: 20,
            icon: 'rect',
            itemWidth: 14,
            itemHeight: 5,
            itemGap: 13,
            data: ['环境温度', '光伏板温度'],
            right: '4%',
            textStyle: {
              fontSize: 12,
              color: this.lineColor
            },
            selected:{
              '环境温度':true,
              '光伏板温度':true
            }
          },
          dataZoom: [{
            show: true,
            realtime: true,
            start: 0,
            end: 100,
            left: "15%",
            right: "15%",
            textStyle: {
              color: this.lineColor
            }
          }, {
            type: 'inside'
          }],
          grid: {
            top: 100,
            left: '2%',
            right: '2%',
            bottom: '10%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            boundaryGap: false,
            axisLine: {
              lineStyle: {
                color: this.lineColor
              }
            },
            data: timeaxis
          }],
          yAxis: [{
            type: 'value',
            name: '°C',
            axisTick: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: this.lineColor
              }
            },
            axisLabel: {
              margin: 10,
              textStyle: {
                fontSize: 14
              }
            },
            splitLine: {
              lineStyle: {
                color: '#57617B'
              }
            }
          }],
          series: [{
            name: '环境温度',
            type: 'line',
            smooth: true,
            symbol: 'circle',
            symbolSize: 5,
            showSymbol: false,
            lineStyle: {
              normal: {
                width: 2
              }
            },
            // areaStyle: {
            //   normal: {
            //     color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            //       offset: 0,
            //       color: 'rgba(219, 50, 51, 0.3)'
            //     }, {
            //       offset: 0.8,
            //       color: 'rgba(219, 50, 51, 0)'
            //     }], false),
            //     shadowColor: 'rgba(0, 0, 0, 0.1)',
            //     shadowBlur: 10
            //   }
            // },
            itemStyle: {
              normal: {

                color: 'rgb(219,50,51)',
                borderColor: 'rgba(219,50,51,0.2)',
                borderWidth: 12
              }
            },
            data: airT
          },

            {
              name: '光伏板温度',
              type: 'line',
              smooth: true,
              symbol: 'circle',
              symbolSize: 5,
              showSymbol: false,
              lineStyle: {
                normal: {
                  width: 2
                }
              },
              // areaStyle: {
              //   normal: {
              //     color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              //       offset: 0,
              //       color: 'rgba(0, 136, 212, 0.3)'
              //     }, {
              //       offset: 0.8,
              //       color: 'rgba(0, 136, 212, 0)'
              //     }], false),
              //     shadowColor: 'rgba(0, 0, 0, 0.1)',
              //     shadowBlur: 10
              //   }
              // },
              itemStyle: {
                normal: {
                  color: 'rgb(0,136,212)',
                  borderColor: 'rgba(0,136,212,0.2)',
                  borderWidth: 12

                }
              },
              data: cellT
            },




          ]
        })

      },
      jsonToDate(jsondata) {
        var dateee = new Date(jsondata).toJSON();
        return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')

      },
    }
  }
</script>
<style scoped>
  #temperaturecharts {
    width: 100%;
    height: calc(85vh - 50px);
  }
</style>
