<template>
  <div id="ChartPig" style="width: 100%; height: 100%"></div>
</template>

<script>
import { mapState } from "vuex";

export default {
  name: "ChartPig",
  data() {
    return {
      myChart: null,
    };
  },
  props: {
    data: {
      type: Object,
      default: function() {
        return {};
      },
    },
  },
  computed: {
    ...mapState(["color"]),
  },
  watch: {
    color(newVal) {
      if (newVal) {
        this.myChart.clear();
        this.init();
      }
    },
    data: {
      handler(newVal) {
        if (newVal) {
          this.myChart.clear();
          this.init();
        }
      },
      deep: true,
    },
  },
  methods: {
    init() {
      let dataAxis = this.data.timeList;
      let start = dataAxis.length - 12;
      let end = dataAxis.length - 1;
      let quantityList = this.data.quantityList;
      let moneyListTooltip = this.data.moneyList1;
      let options = {
        title: {
          x: 60,
          y: 20,
          text: "近期销售情况",
          textStyle: {
            fontSize: 14,
            color: "#6e7079",
          },
        },
        tooltip: {
          trigger: "axis",
          formatter: (params) => {
            var res = "<div>" + params[0].name + "</div>"; // 字符串形式的html标签会被echarts转换渲染成数据，这个res主要是画的tooltip里的上部分的标题部分
            const index = this.data.moneyList.findIndex(
              (item) => item === params[0].value
            );
            for (var i = 0; i < params.length; i++) {
              //因为是个数组，所以要遍历拿到里面的数据，并加入到tooltip的数据内容部分里面去
              res +=
                `<div style="color: #fff;font-size: 14px; padding:0 12px;">
                  <span style="display:inline-block;margin-right:5px;border-radius:5px;width:10px;height:10px;background-color:${[
                    params[i].color, // 默认是小圆点，我们将其修改成有圆角的正方形，这里用的是模板字符串。并拿到对应颜色、名字、数据
                  ]};"></span>
                  ${moneyListTooltip[index]}元
                </div>` +
                `<div style="color: #fff;font-size: 14px; padding:0 12px;"><span style="display:inline-block;margin-right:5px;border-radius:5px;width:10px;height:10px;background-color:${[
                  params[i].color,
                ]};"></span>${quantityList[index]}头</div>`;
            }
            return res;
          },
        },
        color: [this.color],
        dataZoom: [
          {
            type: "inside",
            startValue: start,
            endValue: end,
            show: false,
            // zoomOnMouseWheel: false,
            // zoomLock: true,
          },
        ],
        xAxis: [
          {
            type: "category",
            data: dataAxis,
            axisPointer: {
              type: "none",
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: "#6e7079",
              },
            },
            axisTick: {
              show: false,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            // name: "元",
            axisLabel: {
              formatter: "{value} 元",
              show: true,
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: "#6e7079",
              },
            },
            axisTick: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: "元",
            type: "line",
            // stack: 'Total',
            smooth: true,
            areaStyle: {},
            emphasis: {
              focus: "series",
            },
            itemStyle: {
              color: this.color,
              borderColor: this.color,
              normal: {
                label: {
                  show: true,
                  textStyle: {
                    fontSize: 14,
                  },
                },
              },
            },
            data: this.data.moneyList,
          },
        ],
      };
      this.myChart.setOption(options);
    },
  },
  mounted() {
    this.myChart = this.$echarts.init(document.getElementById("ChartPig"));
    this.init();
  },
};
</script>

<style scoped></style>
