<template>
  <div id="wqq">
    <el-button  style="position:absolute;right:10px;z-index:20000"
                    type="text"
                    icon="el-icon-download"
                    title="生成图片"
                    @click="download"
                    >下载</el-button> 
  <div ref="wqd"
    id="outlineechart"

  ></div>
</div>
</template>
<script>
import yan from "@/assets/commjs/color";
export default {
  data() {
    return {
      echartsheight: undefined,
      device: 1,
      zoom: "",
      myChartes:''
    };
  },
  props: {
    xdata: {
      type: Array,
      default: undefined,
    },
    ydata: {
      type: Array,
      default: undefined,
    },
    title: {
      type: String,
      default: undefined,
    },
  },
  created() {},
  mounted() {
    this.resizeEchartd()
    this.zoom = 1 / document.body.style.zoom;
    window.addEventListener("resize", () => {
      this.zoom = 1 / document.body.style.zoom;
      if(this.myChartes!==''){
        this.myChartes.resize();
      }
      
    });

    this.$nextTick(() => {
      this.evaluation();
    });
  },
  methods: {
    resizeEchartd(){
      var elementResizeDetectorMaker = require("element-resize-detector")
      if(document.getElementById("wqq")){
     
      var erd = elementResizeDetectorMaker()
        erd.listenTo(document.getElementById("wqq"), (element) => {
        let  domeId=document.getElementById("wqq")
        let  domeId2=document.getElementById("outlineechart")
        domeId2.style.width= domeId.offsetWidth - 0 + "px"
       if(this.myChartes !==''){
        this.myChartes.resize();
       }
      });
      }
    },
    download() {
    // 转换成canvas
    html2canvas(document.getElementById("wqq"), {
      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(); // 下载之后把创建的元素删除
    });
  },
    evaluation() {
      const echarts = require("echarts");
      let domID1 = document.getElementById("outlineechart");
      setTimeout(() => {
        domID1.style.width =
        document.getElementById("wqq").offsetWidth - 0+'px'
        domID1.style.height =
          (window.sessionStorage.getItem("appheight") - 50) / 2 + "px";
      }, 0);
      var myChartes = echarts.init(domID1);
      this.myChartes = myChartes
      let echartsColor = JSON.parse(window.localStorage.echartsColor);
      var option = {
        title: {
          text: "区域",
          left: "center",
          textStyle: {
            fontSize: 20,
            color: echartsColor.WordColor,
          },
          // subtext: ''
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
                            type: "shadow",
                            label: {
                            color: "#fff",
                            fontSize: 16,
                            },
                        },
                        textStyle: {
                            color: "#fff",
                            fontStyle: "normal",
                            opacity: 0.35,
                            fontSize: 14,
                        },
                        backgroundColor: "rgba(0,0,0,0.35)",
          formatter: function (params) {
            var tips = "";
            tips +=
              '<span style="">' +
              params[0].name +'</br>'
              "</span>";
            for (var i = 0; i < params.length; i++) {
              if (params[i].value == 0.05) {
                tips += params[i].seriesName + ":暂无数据<br/>";
              } else {
                tips += params[i].seriesName + ":"+params[i].value;
              }
            }
            return tips;
          },
        },
        grid: yan.EchartObject.grid7,

        xAxis: [

          {
            name:yan.EchartObject.area,
            nameTextStyle: {
                            color: echartsColor.WordColor,
                            verticalAlign:'top',//标题位置
                            padding: [-5, 0, 0, 10]
                        },
            type: "category",
            data: this.xdata,
            splitLine: {
              show: false,
              type: "dashed",
              lineStyle: {
                color: echartsColor.thread,
              },
            },
            axisTick: {
              alignWithLabel: true,
            },
            axisLine: {
              show: true,
              symbol: ["none", "arrow"],
              symbolOffset:25,//箭头距离x轴末端距离
                        lineStyle: {
                        shadowOffsetX:25,          
                        color: echartsColor.thread,
                        shadowColor:  echartsColor.thread,   //设置阴影的颜色
                        },
            },
            axisLabel: {
              interval: 0, 
              textStyle: {
                fontFamily: "dinproRegular",
                color: echartsColor.WordColor,
              },
              rotate: yan.rotate(this.xdata.length)
            },
          },
        ],
        //toolbox:  yan.EchartObject.toolbox1,
        yAxis: {
          name: yan.EchartObject.grade,
          nameTextStyle: {
                            color: echartsColor.WordColor,
                            verticalAlign:'top',//标题位置
                            padding: [-22, 0, 10, 0]
                        },
          type: "value",
          min: 0,
          max: 2,
          interval: 0.2,
          axisLabel: {
            show: true,
            textStyle: {
              color: echartsColor.WordColor,
            },
            // 这里重新定义就可以
            formatter: function (value, index) {
              var texts = [];
              if (value ===1) {
                texts.push("1级");
              } else if (value ===1.2) {
                texts.push("2级");
              } else if (value === 1.4) {
                texts.push("3级");
              } else if (value === 1.6) {
                texts.push("4级");
              } else if (value === 2) {
                texts.push("5级");
              }
              return texts;
            },
          },
          splitLine: {
             show:false,
              lineStyle: {
                // 使用深浅的间隔色
                color: [echartsColor.thread],
                //type: "dashed",
                opacity: 0.5,
              },
            },
          axisLine: {
            show: true,
            symbolOffset:25,//箭头距离x轴末端距离
                        lineStyle: {
                        shadowOffsetY:-25,          
                        color: echartsColor.thread,
                        shadowColor:  echartsColor.thread,   //设置阴影的颜色
                        },
            symbol: ["none", "arrow"],
          },
        },
        dataZoom: yan.EchartObject.dataZoom,
        series: [
          {
            name: "污染值",
            barMaxWidth: yan.EchartObject.barMaxWidth,
            type: "bar",
            label: {
              normal: {
                show: false,
                position: "top",
              },
            },
            itemStyle: {
              normal: {
                // 随机显示
                //color:function(d){return "#"+Math.floor(Math.random()*(256*256*256-1)).toString(16);}

                // 定制显示（按顺序）
                color: function (params) {
                  if (params.value >2) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color: yan.ColorObject.zdContaminated,
                        },
                      ],
                      false
                    );
                  } else if (1.6< params.value && params.value <= 2) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color:  yan.ColorObject.centerContaminated,
                        },
                      ],
                      false
                    );
                  } else if (1.2< params.value && params.value <= 1.6) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color:  yan.ColorObject.qdContaminated,
                        },
                      ],
                      false
                    );
                  } else if (1< params.value && params.value <=1.2) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color:  yan.ColorObject.qwContaminated,
                        },
                      ],
                      false
                    );
                  } else if (
                    0 < params.value &&
                    params.value <=1 &&  params.value >= 0 &&
                    params.value !== 0.05
                  ) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color:  yan.ColorObject.noContaminated,
                        },
                      ],
                      false
                    );
                  } else if (params.value == 0.05) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color: "#999999",
                        },
                      ],
                      false
                    );
                  }
                },
              },
            },
            markLine: {
              silent: false,
              symbol: "circle",
              lineStyle: {
                color: "red",
                width: 1,
              },
              emphasis: {
                lineStyle: {
                  width: 1,
                },
              },
              data: [
                {
                  name: "",
                  yAxis: 1,
                  lineStyle: {
                    color:  yan.ColorObject.noContaminated,
                  },
                  label: {
                    //position: "middle",
                    formatter: "{b}",
                    textStyle: {
                      color:  yan.ColorObject.noContaminated,
                    },
                  },
                },
                {
                  name: "",
                  yAxis: 1.2,
                  lineStyle: {
                    color:  yan.ColorObject.qwContaminated,
                  },
                  label: {
                   // position: "middle",
                    formatter: "{b}",
                    textStyle: {
                      color:  yan.ColorObject.qwContaminated,
                    },
                  },
                },
                {
                  name: "",
                  yAxis: 1.4,
                  lineStyle: {
                    color:  yan.ColorObject.qdContaminated,
                  },
                  label: {
                   // position: "middle",
                    formatter: "{b}",
                    textStyle: {
                      color:  yan.ColorObject.qdContaminated,
                    },
                  },
                },
                {
                  name: "",
                  yAxis: 1.6,
                  lineStyle: {
                    color:  yan.ColorObject.centerContaminated,
                  },
                  label: {
                   // position: "middle",
                    formatter: "{b}",
                    textStyle: {
                      color:  yan.ColorObject.centerContaminated,
                    },
                  },
                },
                {
                  name: "",
                  yAxis: 2,
                  lineStyle: {
                    color:  yan.ColorObject.zdContaminated,
                  },
                  label: {
                   // position: "middle",
                    formatter: "{b}",
                    textStyle: {
                      color:  yan.ColorObject.zdContaminated,
                    },
                  },
                },
              ],
            },
            data: this.ydata,
          },
        ],
      };
      myChartes.setOption(option)
window.echartsArr.push(myChartes);
      // setTimeout(() => {
      //   myChartes.resize();
      // }, 100);
      let _this = this;
      _this.$erd.listenTo(_this.$refs.wqd, (element) => {
        _this.$nextTick(() => {
          domID1.style.width= document.getElementById("wqq").offsetWidth - 0+'px'
          myChartes.resize();
        });
      });
      window.onresize = function () {
        myChartes.resize();
      };

      //  setInterval(()=>{
      //     myChartes.resize()
      //  },100)
    },
  },
};
</script>
