<template>
  <div>
  <div :style="`height:${vh}`" v-loading="loading" element-loading-text="数据加载中">
        <el-form :inline="true" :model="form" >
            <el-form-item >
                <timePicker ref="timejcpg"  @querfromdata="querfromdata" :treenode="treenode" :interval="interval" :name="name" :dataTree="dataTree"></timePicker>
            </el-form-item>
            <el-form-item>
              <el-button  @click="querfromdata" type="primary"  icon="el-icon-search" >查询</el-button>
            </el-form-item> 
        </el-form>
        <div id="wtzhpg">
          <div style="position:absolute;right:10px;top:90px;font-weight:bold;z-index: 200000;">
            <!-- <span  class="wr h3" style="color:#000;margin-right:10px">综合评估</span> -->
             <span class="h3" style="color:#CC0000;margin-right:10px">极差[1,2]</span>
            <span class="h3" style="color:#996600;margin-right:10px">较差(2,3]</span>
            <span class="h3" style="color:#FF9900;margin-right:10px">合格(3,4]</span>
            <span class="h3" style="color:#99CC99;margin-right:10px">良好(4,4.5]</span>
            <span class="h3" style="color:#00CC00;margin-right:10px">优质(4.5,5]</span>
            <el-button   style="z-index:200000"
            type="text"
            icon="el-icon-download"
            title="生成图片"
            @click="download"
            >下载</el-button>
        </div>   
        <div  ref="zhuEcharts"  id="Powerquality"

         ></div>
         <!-- -->
        </div>
    </div>
  </div>
</template>
<script>
import yan from "@/assets/commjs/color";
import html2canvas from "html2canvas";
import TimePicker from "../components/TimePicker/index4";
import api from "@/api/harmonic/onlineData";
import echarts from "echarts";
export default {
  name: "wtzhpg",
  components: {
    TimePicker,
  },
  data() {
    return {
      vh:null,
      timefalg:false,
      echartsheight: undefined,
      echartswidth: undefined,
      device: 1,
      interval: 3,
      time: [],
      ydata1: [],
      ydata2: [],
      zoom: '',
      loading: false,
      form:{},
      myChartes:null,
      nodeid:this.treeid,
    };
  },
  watch:{

  },
  props: {
    treenode: {
      type: Object,
      default: undefined,
    },
    dataTree: {
      type: Array,
      default: undefined,
    },
    name: {
      type: String,
      default: undefined,
    },
    treeid: {
      type: String,
      default: undefined,
    },
    view: {
      type: Boolean,
      default: undefined,
    },
  },
  created() {
    this.$bus.$on("chushicurrenrNode", (data,data2) => {
      this.nodeid = data;
      this.timefalg = true
    });
   
  },
  mounted() {
    this.resizeEchart ()
    this.zoom = 1 / document.body.style.zoom;
    this.vh =window.sessionStorage.getItem('appheight') - 80 + "px";
    window.addEventListener("resize", () => {
    this.zoom = 1 / document.body.style.zoom;
    if(this.myChartes !==''){
      this.myChartes.resize();
    }
    });
    this.querfromdata()
    this.$nextTick(() => {
      this.Powerquality();
    });
  },
  beforeDestroy() {
    this.$bus.$off("chushicurrenrNode");
  },
  methods: {
    download() {
    // 转换成canvas
    html2canvas(document.getElementById("wtzhpg"), {
      background:'#fff',
      scale: 1,
    }).then(function (canvas) {
      // 创建a标签，实现下载
      var creatIMg = document.createElement("a");
      creatIMg.download ="稳态综合评估.png"; // 设置下载的文件名，
      creatIMg.href = canvas.toDataURL(); // 下载url
      creatIMg.click();
      creatIMg.remove(); // 下载之后把创建的元素删除
    });
  },
    //日期组件触发事件
    querfromdata() {
      this.loading = true
      this.interval = this.$refs.timejcpg.intervald;
      let arr = [];
      if (this.$refs.timejcpg.timehbValue == "") {
        this.timeValue = this.$refs.timejcpg.timeValue;
  
        arr.push(this.timeValue[0] + "至" +  this.timeValue[1]);
        this.time = arr;
        var data = {
          id: this.treeid,
          searchBeginTime: this.timeValue[0],
          searchEndTime: this.timeValue[1],
          periodBeginTime: "",
          periodEndTime: "",
        };
        api.getQualityAssessData(data).then((res) => {
          if (res.code == "A0000") {
           
            var data = res.data.syn[0];
            if (data == 3.14159) {
              this.ydata1 = 0.25;
            } else if (data == 3.1415) {
              this.ydata1 = 0.15;
            } else {
              this.ydata1 = data;
            }
            //pan = false
            //this.$emit("chufaload", pan);
            this.loading = false
            this.$nextTick(() => {
              this.Powerquality();
            });
          }
        });
      }
    },
    //电能质量图表
    Powerquality() {
      const echarts = require("echarts");
      let domID1 = document.getElementById("Powerquality")
                    setTimeout(() => {
                        domID1.style.width = document.getElementById("drag-dept-right").offsetWidth - 30 + "px",
                        domID1.style.height = window.sessionStorage.getItem('appheight') - 130+"px";
                    }, 0);
      this.myChartes = echarts.init(domID1,null, { renderer: "svg" });

      let echartsColor = JSON.parse(window.localStorage.echartsColor);
      this.myChartes.clear();
      var option = {
        title: {
          text: "稳态综合评估",
          x: "center",
          y: "0%",
          //subtext: '综合评估',
          textStyle: {
            color: echartsColor.WordColor,
            fontSize: yan.EchartObject.titleNamefontSize,
          },
          subtextStyle: {
            color: "#90979c",
            fontSize: yan.EchartObject.xNamefontSize,
          },
        },
        tooltip: {
          trigger: "item",
          axisPointer: {
            type: "shadow",
            label: {
              color: "#fff",
              fontSize: 16,
            },
          },
          textStyle: {
            color: "#fff",
            fontStyle: "normal",

            opacity: 0.35,
            fontSize: 14,
          },
          borderColor:'#fff',
          backgroundColor: "rgba(0,0,0,0.35)",
          formatter: function (params) {
            if (params.value >= 5) {
              return params.name + " : " + "优质";
            } else if (params.value > 4 && params.value <= 5) {
              return params.name + " : " + "良好";
            } else if (params.value > 3 && params.value <= 4) {
              return params.name + " : " + "合格";
            } else if (params.value > 2 && params.value <= 3) {
              return params.name + " : " + "较差";
            } else if (params.value <= 2 && params.value >= 1) {
              return params.name + " : " + "极差";
            } else if (params.value >= 0.25 && params.value < 1) {
              return params.name + " : " + "暂无数据";
            } else if (params.value <= 0.15 && params.value >= 0) {
              return params.name + " : " + "/";
            }
          },
        },
        grid: yan.EchartObject.grid1,
        legend: {
          show: false,
          data: ["综合评估"],
          left: yan.EchartObject.lengd,
          textStyle: {
            color: echartsColor.WordColor,
          },
        },

        xAxis: [
          {
            name: yan.EchartObject.zhibiao,
            nameTextStyle: {
              color: echartsColor.WordColor,
            },
            type: "category",
            //data: this.time,
            data:['稳态综合评估'],
            axisLabel: {
              margin: 10,
              color: echartsColor.WordColor,
              textStyle: {
                fontSize: yan.EchartObject.xNamefontSize,
              },
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              symbol: ["none", "arrow"],
              lineStyle: {
                color: echartsColor.thread,
              },
            },
          },
        ],
        //toolbox: yan.EchartObject.toolbox2,
        yAxis: {
         name: yan.EchartObject.grade,
          type: "value",
          min: 0,
          max: 6,
          axisLabel: {
            show: true,
            textStyle: {
              color: echartsColor.WordColor,
              fontSize: yan.EchartObject.yNamefontSize
            },
            // 这里重新定义就可以
            formatter: function (value, index) {
              var texts = [];
              if (value === 1) {
                texts.push("1级");
              } else if (value === 2) {
                texts.push("2级");
              } else if (value === 3) {
                texts.push("3级");
              } else if (value === 4) {
                texts.push("4级");
              } else if (value === 5) {
                texts.push("5级");
              }
              return texts;
            },
          },
          axisLine: {
            show: true,
            symbol: ["none", "arrow"],
              lineStyle: {
                color: echartsColor.thread,
              },
            
          },
          splitLine: {
              show:false,
              lineStyle: {
                // 使用深浅的间隔色
                color: [echartsColor.thread],
                type: "dashed",
                opacity: 0.5,
              },
            },
        },
        series: [
          {
            name: "综合评估",
            type: "bar",
            barMaxWidth:yan.EchartObject.barMaxWidth,
            label: {
                            show: true,
                            position: 'top',
                            distance: 2,
                            color: '#fff',
                            fontWeight: 'bolder',
                            fontSize: yan.EchartObject.NamefontSize,
                            },
            itemStyle: {
              normal: {
                color: function (params) {
                  if (params.value > 5) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      0,
                      0,
                      1,
                      [
                        {
                          offset: 1,
                          color: yan.ColorObject.highquality,
                        },
                      ],
                      false
                    );
                  } else if (params.value > 4 && params.value <= 5) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      0,
                      0,
                      1,
                      [
                        {
                          offset: 1,
                          color: yan.ColorObject.good,
                        },
                      ],
                      false
                    );
                  } else if (params.value > 3 && params.value <= 4) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      0,
                      0,
                      1,
                      [
                        {
                          offset: 1,
                          color: yan.ColorObject.qualified,
                        },
                      ],
                      false
                    );
                  } else if (params.value > 2 && params.value <= 3) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      0,
                      0,
                      1,
                      [
                        {
                          offset: 1,
                          color: yan.ColorObject.poor,
                        },
                      ],
                      false
                    );
                  } else if (params.value <= 2 && params.value >= 1) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      0,
                      0,
                      1,
                      [
                        {
                          offset: 1,
                          color: yan.ColorObject.range,
                        },
                      ],
                      false
                    );
                  } else if (params.value <= 0.25) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      0,
                      0,
                      1,
                      [
                        {
                          offset: 1,
                          color: "#ccc",
                        },
                      ],
                      false
                    );
                  }
                },
              },
            },
            //silent: true,
            barWidth: "45",
            //barGap: '-100%', // Make series be overlap
            data: [
              {
                value: this.ydata1,
              },
            ],
            markLine: {
              silent: false,
              symbol: "circle",
              data: [
                {
                  name: "优质",
                  yAxis: 5,

                  lineStyle: {
                    // cap: 'round' ,
                    color: yan.ColorObject.highquality,
                  },
                  label: {
                    color: yan.ColorObject.highquality, // 文字颜色
                    padding: [2, 2, 5, 0], // 可用padding调整图片内文字距离
                    show: true,
                    formatter: function (params) {
                      var tips = "";
                      tips += "优质";

                      return tips;
                    },
                  },
                },
                {
                  name: "良好",
                  yAxis: 4,
                  lineStyle: {
                    color: yan.ColorObject.good,
                  },
                  label: {
                    color: yan.ColorObject.good, // 文字颜色
                    padding: [2, 2, 5, 0], // 可用padding调整图片内文字距离
                    show: true,
                    formatter: function (params) {
                      // alert(params)
                      var tips = "";
                      tips += "良好";

                      return tips;
                    },
                  },
                },
                {
                  name: "合格",
                  yAxis: 3,
                  lineStyle: {
                    color: yan.ColorObject.qualified,
                  },
                  label: {
                    color: yan.ColorObject.qualified, // 文字颜色
                    padding: [2, 2, 5, 0], // 可用padding调整图片内文字距离
                    show: true,
                    formatter: function (params) {
                      // alert(params)
                      var tips = "";
                      tips += "合格";

                      return tips;
                    },
                  },
                },
                {
                  name: "较差",
                  yAxis: 2,
                  lineStyle: {
                    color: yan.ColorObject.poor,
                  },
                  label: {
                    color: yan.ColorObject.poor, // 文字颜色
                    padding: [2, 2, 5, 0], // 可用padding调整图片内文字距离
                    show: true,
                    formatter: function (params) {
                      // alert(params)
                      var tips = "";
                      tips += "较差";

                      return tips;
                    },
                  },
                },
                {
                  name: "极差",
                  yAxis: 1,
                  lineStyle: {
                    color: yan.ColorObject.range,
                  },
                  label: {
                    color: yan.ColorObject.range, // 文字颜色
                    padding: [2, 2, 5, 0], // 可用padding调整图片内文字距离
                    show: true,
                    formatter: function (params) {
                      // alert(params)
                      var tips = "";
                      tips += "极差";

                      return tips;
                    },
                  },
                },
              ],
            },
          },
        ],
      };
      this.myChartes.setOption(option)
window.echartsArr.push(this.myChartes);
      this.loading = false;
  


      let _this = this;
      _this.$erd.listenTo(_this.$refs.zhuEcharts, (element) => {
        _this.$nextTick(() => {
          //domID1.style.width = document.getElementById("drag-dept-right").offsetWidth - 30 + "px",
          //domID1.style.width ="100%" 
          _this.myChartes.resize();
        });
      });
     //this.myChartes.hideLoading();
    },
    resizeEchart(){
      var elementResizeDetectorMaker = require("element-resize-detector")
      var erd = elementResizeDetectorMaker()
      erd.listenTo(document.getElementById("drag-dept-right"), (element) => {
        let  domeId=document.getElementById("drag-dept-right")
        let  domeId2=document.getElementById("Powerquality")
        domeId2.style.width= domeId.offsetWidth - 30 + "px"
        if(this.myChartes !==''){
          this.myChartes.resize();
        }
      });
    }
  },
};
</script>
<style lang="less" scoped>
.cfd {
  display: inline-block;
  // margin-bottom: 10px;
  width: 100%;
  margin-top: 0%;
  position: relative;
  z-index: 100000;
}
.cf {
  display: inline-block;
  //margin-bottom: 10px;
  width: 100%;
  margin-top: 0%;
  position: relative;
  z-index: 100000;
}
</style>
