<template>
  <div class="chart" :id="id"></div>
</template>

<script>
import util from "@/helper/util.js";
import partten from "@/helper/partten.js";
import * as echarts from "echarts";

export default {
  name: "multi-arrow-line-chart",
  componentName: "multi-arrow-line-chart",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "13.889vh",
    },
    // 数据
    list: {
      type: Array,
      default: () => [
        {
          title: "功率",
          yAxisIndex: 0,
          value: [
            {
              text: "0m/s",
              value: 1,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "2m/s",
              value: 2,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "3m/s",
              value: 16,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "4m/s",
              value: 3,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "5m/s",
              value: 3,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "6m/s",
              value: 31,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "7m/s",
              value: 33,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "8m/s",
              value: 43,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "9m/s",
              value: 3,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "10m/s",
              value: 3,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "11m/s",
              value: 31,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "12m/s",
              value: 3,
              show: true,
              size: 12,
              route: -90,
            },
            {
              text: "13m/s",
              value: 3,
              show: true,
              size: 12,
              route: -90,
            },
          ],
        },
      ],
    },
    // 单位
    units: {
      type: Array,
      default: () => ["()"],
    },
    showLegend: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      color: ["#606769"],
    };
  },
  computed: {
    legend() {
      return this.list.map((t) => {
        return t.title;
      });
    },
    xdata() {
      return this.list[0].value.map((t) => {
        return t.text;
      });
    },
    yAxis() {
      let result = [];
      this.units.forEach((value, index) => {
        result.push({
          type: "value",
          name: value,
          axisLabel: {
            formatter: "{value}",
            fontSize: util.vh(14),
          },
          //分格线
          splitLine: {
            lineStyle: {
              color: partten.getColor("gray") + 55,
              type: "dashed",
            },
          },
        });
      });

      return result;
    },
    series() {
      let result = [];

      this.list.forEach((value, index) => {
        var data = value.value.map((t) => {
          return t.value;
        });

        var arrowArr = [];
        data.forEach((dvalue, dindex) => {
          var item = dvalue;
          if (value.value[dindex].show) {
            arrowArr.push({
              symbol: "arrow",
              symbolSize: value.value[dindex].size,
              symbolRotate: value.value[dindex].route,
              value: item,
            });
          } else {
            arrowArr.push({ symbol: "none", value: item });
          }
        });
        result.push({
          name: value.title,
          type: "line",
          smooth: true,
          zlevel: index,
          lineStyle: {
            type: "dotted", //dashed
          },
          yAxisIndex: value.yAxisIndex,
          data: arrowArr,
        });
      });

      return result;
    },
  },
  methods: {
    initChart() {
      const chart = echarts.init(this.$el);

      let option = {
        color: this.color,
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(0,0,0,0.4)",
          borderColor: partten.getColor("gray"),
          textStyle: {
            color: "#fff",
            fontSize: util.vh(16),
          },
        },
        legend: {
          show: this.showLegend,
          data: this.legend,
          right: 56,
          icon: "circle",
          itemWidth: 6,
          inactiveColor: partten.getColor("gray"),
          textStyle: {
            color: partten.getColor("grayl"),
            fontsize: 12,
          },
        },
        grid: {
          top: util.vh(32),
          left: util.vh(40),
          right: util.vh(40),
          bottom: util.vh(24),
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            axisLabel: {
              formatter: "{value}",
              fontSize: util.vh(14),
              textStyle: {
                color: partten.getColor("gray"),
              },
            },
            data: this.xdata,
          },
        ],
        yAxis: this.yAxis,
        series: this.series,
      };

      chart.clear();
      chart.setOption(option);

      this.resize = function() {
        chart.resize();
      };

      window.addEventListener("resize", this.resize);
    },
  },
  created() {
    this.id = "pie-chart-" + util.newGUID();
  },
  mounted() {
    this.$nextTick(() => {
      this.$el.style.width = this.width;
      this.$el.style.height = this.height;
      this.initChart();
    });
  },
  updated() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  unmounted() {
    window.removeEventListener("resize", this.resize);
  },
};
</script>

<style lang="less">
.chart {
  width: 100%;
  height: 100%;
  display: inline-block;
}
</style>
