<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
// 柔和颜色随机
const getRandomColor = () => {
  // 定义艳丽颜色的RGB范围
  const min = 0;
  const max = 255;

  // 生成三个在指定范围内的随机数，分别代表R、G、B
  const r = Math.floor(Math.random() * (max - min + 1)) + min;
  const g = Math.floor(Math.random() * (max - min + 1)) + min;
  const b = Math.floor(Math.random() * (max - min + 1)) + min;

  // 为了确保颜色清新，适当调整色差
  const adjustColor = (color) => {
    if (Math.random() > 0.5) {
      return Math.min(255, color + Math.floor((255 - color) * 0.2));
    } else {
      return Math.max(0, color - Math.floor(color * 0.2));
    }
  };

  const adjustedR = adjustColor(r);
  const adjustedG = adjustColor(g);
  const adjustedB = adjustColor(b);

  // 确保颜色组合舒适自然
  const ensureComfortable = (r, g, b) => {
    // 计算颜色的亮度
    const brightness = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
    // 如果亮度太低或太高，调整其中一个颜色分量
    if (brightness < 0.3) {
      return [Math.min(255, r + 50), g, b];
    } else if (brightness > 0.7) {
      return [Math.max(0, r - 50), g, b];
    }
    return [r, g, b];
  };

  const [finalR, finalG, finalB] = ensureComfortable(
    adjustedR,
    adjustedG,
    adjustedB
  );

  // 将RGB值转换为16进制字符串
  const hexR = finalR.toString(16).padStart(2, "0");
  const hexG = finalG.toString(16).padStart(2, "0");
  const hexB = finalB.toString(16).padStart(2, "0");

  // 返回带有#前缀的十六进制颜色字符串
  return `#${hexR}${hexG}${hexB}`;
};
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ xdata, ydata } = {}) {
      let list = ydata.map((item) => {
        return {
          name: item.name,
          smooth: true,
          type: "line",
          itemStyle: {
            normal: {
              color: getRandomColor(), // 修改这里使用随机颜色
              lineStyle: {
                color: getRandomColor(), // 修改这里使用随机颜色
                width: 2,
              },
              areaStyle: {
                color: "#f3f8ff",
              },
            },
          },
          data: item.data,
          animationDuration: 2800,
          animationEasing: "quadraticOut",
        };
      });
      let lendata = ydata.map((item) => {
        return item.name;
      });
      this.chart.setOption({
        xAxis: {
          data: xdata,
          boundaryGap: false,
          axisTick: {
            show: false,
          },
        },
        grid: {
          left: 30,
          right: 30,
          bottom: 20,
          top: 90,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
        },
        legend: {
          data: lendata,
          //颜色、

          textStyle: {
            color: "#fff", // 图例文字颜色
          },
        },
        series: list,
      });
    },
  },
};
</script>
