<template>
  <div
    id="outlineechart"
    :style="'width:calc(50vw - 25px);height:45vh'"
  ></div>
</template>
<script>
import yan from "@/assets/commjs/color";
export default {
  data() {
    return {
     // echartsheight: undefined,
     // device: 1,
    };
  },
  props: {
    xdata: {
      type: Array,
      default: undefined,
    },
    ydata: {
      type: Array,
      default: undefined,
    },
    title: {
      type: String,
      default: undefined,
    },
  },
  created() {},
  mounted() {
  //  this.device = window.devicePixelRatio;
  
    this.evaluation();
    
  },
  methods: {
    evaluation() {
      const echarts = require("echarts");
      var dome1=document.getElementById("outlineechart")
      var myChartes = echarts.init(dome1);
     // dome1.style.width = document.getElementById("klj").offsetWidth - 10+'px'
      
      var option = {
        title: {
          text: "区域",
          left: "center",
          // subtext: ''
        },
        tooltip: {
          trigger: "axis",
          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: {
          left: "10px",
          right: "40px",
          bottom: "20px",
          containLabel: true,
        },
        xAxis: [
          {
            name:'区域',
            type: "category",
            data: this.xdata,
            splitLine: {
              show: false,
            },
            axisTick: {
              alignWithLabel: true,
            },
            axisLine: {
              show: true,
              symbol: ["none", "arrow"],
            },
            axisLabel: {
              textStyle: {
                fontFamily: "dinproRegular",
                color: "#000",
              },
              rotate: 0,
            },
          },
        ],
        toolbox: {
          show: true,
          feature: {
            //   dataZoom: {
            //     yAxisIndex: 'none'
            //   },
            //  dataView: { readOnly: false },
            //magicType: { type: ['line', 'bar'] },
            // //restore: { show: true },
          //  saveAsImage: { show: true },
          },
        },
        yAxis: {
          name: "单位(等级)",
          type: "value",
          min: 0,
          max: 2,
          interval: 0.2,
          axisLabel: {
            show: true,
            textStyle: {
              color: "#00",
            },
            // 这里重新定义就可以
            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;
            },
          },
          axisLine: {
            show: true,
            symbol: ["none", "arrow"],
          },
        },
        series: [
          {
            name: "数值",
            barMaxWidth: 30,
            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);
      window.onresize = function () {
        myChartes.resize();
      };
      //  setInterval(()=>{
      //     myChartes.resize()
      //  },100)
    },
  },
  beforeDestroy(){
    this.$destroy()
    window.gc()
  },
  destroyed() {
      
  }
};
</script>
