<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: "normal-line-chart",
  componentName: "normal-line-chart",
  props: {
    width: {
      type: String,
      default: "280px",
    },
    height: {
      type: String,
      default: "24px",
    },
    // 数据
    list: {
      type: Array,
      default: () => [
        {
          text: "1",
          value: 3,
        },
        {
          text: "2",
          value: 2,
        },
        {
          text: "3",
          value: 1,
        },
      ],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
    };
  },
  computed: {
    xdata() {
      return this.list.map((t,index) => {
        return t.text || index;
      });
    },
    values() {
      return this.list.map((t) => {
        return t.value || t;
      });
    },
  },
  methods: {
    hexToRgba(hex, opacity) {
      let rgbaColor = "";
      let reg = /^#[\da-f]{6}$/i;
      if (reg.test(hex)) {
        rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt("0x" + hex.slice(3, 5))},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
      }
      return rgbaColor;
    },
    initChart() {
      let chart = echarts.init(this.$el);
      let color = [partten.getColor("green")];

      let option = {
        color: color,
        grid: {
          top: 4,
          bottom: 0,
          left: 8,
          right: 8,
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            axisLine: {
              show: true,
              lineStyle: {
                color: partten.getColor("gray"),
              },
            },
            data: this.xdata,
          },
        ],
        yAxis: [
          {
            type: "value",
            // 分割线
            splitLine: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
          },
        ],
        series: [
          {
            type: "line",
            lineStyle: {
              normal: {
                color: color[0],
              },
            },
            animation: false,
            symbol: "circle", //数据交叉点样式
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: this.hexToRgba(color[0], 0.3),
                    },
                    {
                      offset: 1,
                      color: this.hexToRgba(color[0], 0.1),
                    },
                  ],
                  false
                ),
                shadowColor: this.hexToRgba(color[0], 0.1),
                shadowBlur: 10,
              },
            },
            data: this.values,
          },
        ],
      };

      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(() => {
      setTimeout(() => {
        this.$el.style.width = this.width;
        this.$el.style.height = this.height;
        this.initChart();
      }, 1000);
    });
  },
  updated() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  unmounted() {
    window.removeEventListener("resize", this.resize);
  },
};
</script>

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