<template>
  <div class="main" :class="chartname"></div>
</template>

<script>
import * as echarts from 'echarts';
import loopShowTooltip from '../echartsautotooltip.js'
export default {
  props: {
    chartname: {
      type: String,
    },
    yunit: {
      type: String,
    },
    yname: {
      type: Array,
      default: () => {
        return []
      }
    },
    x: {
      type: Array,
      default: () => {
        return []
      }
    },
    y: {
      type: Array,
      default: () => {
        return []
      }
    },
    chartype: {
      type: Array,
      default: () => {
        return []
      }
    },
    chartcolor: {
      type: Array,
      default: () => {
        return []
      }
    },
    linebarwidth: {
      type: Array,
      default: () => {
        return []
      }
    },
  },
  data() {
    return {
    };
  },

  components: {},

  computed: {},

  beforeDestroy() {
    this.myChart = null;
    echarts.dispose(this.myChart);
    window.removeEventListener("resize", this.chartResize);
  },
  mounted() {
    console.log(loopShowTooltip, 11)
    this.chart()
    this.init()
  },
  methods: {
    init() {
      window.addEventListener("resize", this.chartResize);
    },
    chartResize() {
      this.myChart.resize();
    },
    chart() {
      // 基于准备好的dom，初始化echarts实例
      this.myChart = echarts.init(
        document.querySelector("." + this.chartname)
      );
      var op = {
        grid: {
          top: "15%",
          left: "5%",
          right: "10%",
          bottom: "10%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: true,
            axisLine: {
              lineStyle: {
                color: "rgba(36, 53, 80, 1)",
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(36, 53, 80, 1)",
              },
            },
            axisTick: {
              show: false,
            },
            splitArea: {
              show: false,
            },
            axisLabel: {
              interval: 0,
              color: "#FFF",
              fontSize: 14,
            },
            data: this.x,
          },
        ],
        yAxis: [
          {
            name: this.yunit,
            nameTextStyle: {
              color: "#03D8EF",
              fontSize: 14,
            },
            type: "value",
            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(36, 53, 80, 1)",
              },
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            splitArea: {
              show: false,
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(36, 53, 80, 1)",
              },
            },
            axisLabel: {
              interval: 0,
              color: "rgba(48, 171, 231, 1)",
              fontSize: 14,
            },
          },
          {
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
          },
        ],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            lineStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "rgba(0, 255, 233,0)",
                  },
                  {
                    offset: 0.5,
                    color: "rgba(255, 255, 255,1)",
                  },
                  {
                    offset: 1,
                    color: "rgba(0, 255, 233,0)",
                  },
                ],
                global: false,
              },
            },
          },
        },
        series: [
        ],
      };
      for (var i = 0; i < this.y.length; i++) {
        if (this.chartype[i] == "line") {
          op.series.push({
            name: this.yname[i],
            type: "line",
            smooth: true, // 是否平滑曲线显示
            showAllSymbol: false,
            symbol: "emptyCircle",
            symbolSize: 10,
            symbol: "none",
            label: {
              show: false,
              position: "top",
              textStyle: {
                color: "#fff",
              },
            },
            lineStyle: {
              width: this.linebarwidth[i],
            },
            data: this.y[i],
          })
        }
        else {
          op.series.push({
            name: this.yname[i],
            type:  "bar",
            barWidth: this.linebarwidth[i],
            itemStyle: {
              normal: {
                color: this.chartcolor[i]
              }
            },
            data: this.y[i],
          })
        }
      }
      this.myChart.setOption(op);
      loopShowTooltip(this.myChart, op, { loopSeries: true, interval: 5000 });
    }

  },
}

</script>
<style scoped>
.main {
  width: 100%;
  height: 100%;
}
</style>