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

<script>
  import resize from '../../../../components/Charts/mixins/resize'
  import echarts from 'echarts'
  import cc from '../../../curvecolors'
  export default {
    mixins: [resize],
    watch: {
      drawData:{
        handler(newValue, oldValue) {
          this.cName = newValue.cName
          if(newValue.cName.ws10==undefined){
            this.company = 'W/㎡'
            this.thFormat = [
              {key:"directRadiation",value:'直辐射'},
              {key:"diffuseRadiation",value:'散辐射'},
              {key:"swr",value:'总辐射'}
              ]
          }else {
            this.thFormat = [{key:"ws10",value:this.cName.ws10},
              {key:"ws30",value:this.cName.ws30},
              {key:"ws50",value:this.cName.ws50},
              {key:"ws70",value:this.cName.ws70},
              {key:"ws80",value:this.cName.ws80},
              {key:"ws90",value:this.cName.ws90},
              {key:"ws100",value:this.cName.ws100},
              {key:"ws170",value:this.cName.ws170}]
          }

          this.draw(newValue.times, newValue.datas)

        },
        deep: true
      },
      resizeKey:function(newQuestion, oldQuestion){
        if(this.chart !=null){
          this.chart.resize();
        }
      }
    },
    props: {
      drawData:{
        type:Object,
      },
      resizeKey:{
        type:Number
      }
    },
    data() {
      return {
        lineColor:'',
        cName:{},
        chart: null,
        thFormat:[],
        company:'m/s',
        lineStyle:[
          {
            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: cc.ws10,
                borderColor: 'rgba(219,50,51,0.2)',
                borderWidth: 12
              }
            },
          },
          {
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(219,196,50,0.3)'
                }, {
                  offset: 0.8,
                  color: 'rgba(219, 50, 51, 0)'
                }], false),
                shadowColor: 'rgba(0, 0, 0, 0.1)',
                shadowBlur: 10
              }
            },
            itemStyle: {
              normal: {
                color: cc.ws30,
                borderColor: 'rgba(219,196,50,0.2)',
                borderWidth: 12
              }
            },
          },
          {
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(101,219,50,0.3)'
                }, {
                  offset: 0.8,
                  color: 'rgba(219, 50, 51, 0)'
                }], false),
                shadowColor: 'rgba(0, 0, 0, 0.1)',
                shadowBlur: 10
              }
            },
            itemStyle: {
              normal: {
                color: cc.ws50,
                borderColor: 'rgba(101,219,50,0.2)',
                borderWidth: 12
              }
            },
          },
          {
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(50,219,171,0.3)'
                }, {
                  offset: 0.8,
                  color: 'rgba(219, 50, 51, 0)'
                }], false),
                shadowColor: 'rgba(0, 0, 0, 0.1)',
                shadowBlur: 10
              }
            },
            itemStyle: {
              normal: {
                color: cc.ws70,
                borderColor: 'rgba(50,219,171,0.2)',
                borderWidth: 12
              }
            },
          },
          {
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(50,118,219,0.3)'
                }, {
                  offset: 0.8,
                  color: 'rgba(219, 50, 51, 0)'
                }], false),
                shadowColor: 'rgba(0, 0, 0, 0.1)',
                shadowBlur: 10
              }
            },
            itemStyle: {
              normal: {
                color: cc.ws80,
                borderColor: 'rgba(50,118,219,0.2)',
                borderWidth: 12
              }
            },
          },
          {
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(140,50,219,0.3)'
                }, {
                  offset: 0.8,
                  color: 'rgba(219, 50, 51, 0)'
                }], false),
                shadowColor: 'rgba(0, 0, 0, 0.1)',
                shadowBlur: 10
              }
            },
            itemStyle: {
              normal: {
                color: cc.ws90,
                borderColor: 'rgba(140,50,219,0.2)',
                borderWidth: 12
              }
            },
          },
          {
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(202,50,219,0.3)'
                }, {
                  offset: 0.8,
                  color: 'rgba(219, 50, 51, 0)'
                }], false),
                shadowColor: 'rgba(0, 0, 0, 0.1)',
                shadowBlur: 10
              }
            },
            itemStyle: {
              normal: {
                color: cc.ws100,
                borderColor: 'rgba(202,50,219,0.2)',
                borderWidth: 12
              }
            },
          },
          {
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(50,199,219,0.3)'
                }, {
                  offset: 0.8,
                  color: 'rgba(219, 50, 51, 0)'
                }], false),
                shadowColor: 'rgba(0, 0, 0, 0.1)',
                shadowBlur: 10
              }
            },
            itemStyle: {
              normal: {
                color: cc.ws110,
                borderColor: 'rgba(50,199,219,0.2)',
                borderWidth: 12
              }
            },
          },
          {
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(219,143,50,0.3)'
                }, {
                  offset: 0.8,
                  color: 'rgba(219, 50, 51, 0)'
                }], false),
                shadowColor: 'rgba(0, 0, 0, 0.1)',
                shadowBlur: 10
              }
            },
            itemStyle: {
              normal: {
                color: cc.ws120,
                borderColor: 'rgba(219,143,50,0.2)',
                borderWidth: 12
              }
            },
          }
        ]
      }
    },
    mounted() {
      if(sessionStorage.getItem('styleSwitch') === 'blue'){
        this.lineColor = 'white'
      }else{
        this.lineColor = '#3b3b3b'
      }
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      draw(timeaxis,datas) {
        this.chart = echarts.init(document.getElementById('nwpcharts'))
        var option = {
          backgroundColor: 'transparent',
          title: {
            top: 20,
            text: 'nwp二期信息',
            textStyle: {
              fontWeight: 'normal',
              fontSize: 16,
              color: this.lineColor
            },
            left: '1%'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              lineStyle: {
                color: '#57617B'
              }
            }
          },
          toolbox: {
            feature: {
              // dataView: {show: true, readOnly: false},
              // magicType: {show: true, type: ['line', 'bar']},
              // restore: {show: true},
              saveAsImage: {show: true}
            }
          },
          legend: {
            top: 20,
            width:'70%',
            icon: 'rect',
            itemWidth: 14,
            itemHeight: 5,
            itemGap: 13,
            data: [],
            right: '4%',
            textStyle: {
              fontSize: 12,
              color: this.lineColor
            },
            selected:{
            }
          },
          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:[]
          }],
          yAxis: [{
            type: 'value',
            name: this.company,
            axisTick: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: this.lineColor
              }
            },

            axisLabel: {
              margin: 10,
              textStyle: {
                fontSize: 14,
                color: this.lineColor
              },
              formatter: '{value}',
            },
            splitLine: {
              lineStyle: {
                color: '#57617B'
              }
            }
          }],
          series: []
        }
        option.xAxis[0].data = timeaxis
        var index = 0;
        for(var key in datas){
          var keyName='';
          for(var i = 0 ;i<this.thFormat.length;i++){
            if(key ==this.thFormat[i].key ){
              keyName =this.thFormat[i].value;
            }
          }
          option.legend.data.push(keyName)
          if(index>5){
            option.legend.selected[keyName] = false
          }else{
            option.legend.selected[keyName] = true
          }
          var sValue = {
            name: '',
            type: 'line',
            smooth: false,
            symbol: 'circle',
            symbolSize: 5,
            showSymbol: false,
            lineStyle: {
              normal: {
                width: 2
              }
            },
            // areaStyle: {},
            itemStyle: {},
            data: []
          }
          sValue.name = keyName
          sValue.data = datas[key]
          // sValue.areaStyle = this.lineStyle[index].areaStyle
          sValue.itemStyle = this.lineStyle[index].itemStyle
          option.series.push(sValue)
          index++;
        }
        this.chart.setOption(option,true)
      },

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