<!--
 * @Author: zhp
 * @Date: 2024-04-28 13:42:51
 * @LastEditTime: 2024-06-28 09:28:56
 * @LastEditors: zhp
 * @Description:
-->
<!--
    filename: right-chart-base
    author: liubin
    date: 2024-04-10 08:59:28
    description:
-->

<template>
  <chart-container class="right-chart-base">
    <div class="legend">
      <span
        v-for="item in legend"
        :key="item.label"
        class="legend-item"
        :style="{ fontSize: isFullscreen ? '0.72vw' : '0.7vw' }"
        >{{ item.label }}</span
      >
    </div>
    <div
      ref="chart"
      style="max-width: 22vw"
      :style="{ height: vHeight + 'vh' }"
    ></div>
  </chart-container>
</template>

<script>
import screenfull from "screenfull";
import ChartContainerVue from "../../components/ChartContainer.vue";
import chartMixin from "@/mixins/chart.js";

export default {
  name: "RightChartBase",
  components: {
    ChartContainer: ChartContainerVue,
  },
  mixins: [chartMixin],
  props: {
    vHeight: {
      type: Number,
      default: 34,
    },
    legend: {
      type: Array,
      required: true,
    },
    xAxis: {
      type: Array,
      required: true,
    },
    series: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      isFullscreen: false,
      options: {
        grid: {
          left: "5%",
          right: "4%",
          bottom: "0",
          top: "18%",
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // type: "cross",
            crossStyle: {
              color: "rgba(237,237,237,0.5)",
            },
          },
          formatter: params => {
            // 获取xAxis data中的数据
            let dataStr = `<div><p style="font-weight:bold;margin:0 8px 15px;">${params[0].name}</p></div>`
            params.forEach(item => {
              let color = Object.prototype.toString.call(item.color) == "[object String]" ? item.color : item.color.colorStops ? item.color.colorStops[0].color : ''
              console.log(item.color, color);
              dataStr += `<div>
          <div style="margin: 0 8px;">
            <span style="display:inline-block;margin-right:5px;width:10px;height:10px;background-color:${color};"></span>
            <span>${item.seriesName}</span>
            <span style="float:right;color:#000000;margin-left:20px;">${item.data}</span>
          </div>
        </div>`
            })
            return dataStr
          }
        },
        xAxis: {
          axisTick: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "#4561AE",
            },
          },
          axisLabel: {
            color: "#fff",
            fontSize: 14,
          },
          data: this.xAxis,
        },
        yAxis: {
          name: "单位/片",
          position: 'left',
          nameTextStyle: {
            color: "#fff",
            fontSize: 14,
            align: 'right'
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: "#fff",
            fontSize: 14,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "#4561AE",
            },
          },
          splitLine: {
            lineStyle: {
              color: "#4561AE",
            },
          },
        },
        series: [
          {
            name: "", // "2024年目标值",
            type: "line",
            lineStyle: {
              color: "#f3c000",
            },
            itemStyle: {
              color: "#f3c000",
            },
            // areaStyle: {
            //   color: {
            //     type: "linear",
            //     x: 0,
            //     y: 0,
            //     x2: 0,
            //     y2: 1,
            //     colorStops: [
            //       {
            //         offset: 0,
            //         color: "#f3c000", // 0% 处的颜色
            //       },
            //       {
            //         offset: 0.55,
            //         color: "#f3c00033",
            //       },
            //       {
            //         offset: 1,
            //         color: "transparent", // 100% 处的颜色
            //       },
            //     ],
            //     global: false, // 缺省为 false
            //   },
            // },
            data: [], // this.series[0].data,
          },
          {
            name: "", // "2023年",
            type: "bar",
            barWidth: 12,
            itemStyle: {
              borderRadius: [10, 10, 0, 0],
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "#12f7f1", // 0% 处的颜色
                  },
                  {
                    offset: 0.35,
                    color: "#12f7f177", // 100% 处的颜色
                  },
                  {
                    offset: 0.75,
                    color: "#12f7f133", // 100% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "transparent", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
            data: [], // this.series[1].data,
          },
          {
            name: "", // "2024年",
            type: "bar",
            barWidth: 12,
            // tooltip: {
            //   valueFormatter: function (value) {
            //     return value + " ml";
            //   },
            // },
            itemStyle: {
              borderRadius: [10, 10, 0, 0],
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "#57abf8", // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "#364BFE66", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
            data: [], // this.series[2].data,
          },
        ],
      },
      actualOptions: null,
    };
  },
  watch: {
    /** 全屏状态切换时,对柱子粗细和字体大小进行相应调整 */
    isFullscreen(val) {
      this.actualOptions.series.map((item) => {
        item.barWidth = val ? 18 : 12;
      });
      this.actualOptions.xAxis.axisLabel.fontSize = val ? 18 : 14;
      this.actualOptions.yAxis.axisLabel.fontSize = val ? 18 : 14;
      this.actualOptions.yAxis.nameTextStyle.fontSize = val ? 18 : 14;
      this.initOptions(this.actualOptions);
    },
    series(val) {
      if (!val) {
        this.initOptions(this.options);
        return;
      }
      const actualOptions = JSON.parse(JSON.stringify(this.options));
      actualOptions.series[0].data = val[0].data;
      actualOptions.series[0].name = val[0].name;
      actualOptions.series[1].data = val[1].data;
      actualOptions.series[1].name = val[1].name;
      actualOptions.series[2].data = val[2].data;
      actualOptions.series[2].name = val[2].name;
      this.actualOptions = actualOptions;
      this.initOptions(actualOptions);
    },
  },
  mounted() {
    this.actualOptions = this.options;
    this.initOptions(this.options);

    if (screenfull.isEnabled) {
      screenfull.on("change", () => {
        this.isFullscreen = screenfull.isFullscreen;
      });
    }
  },
};
</script>

<style scoped lang="scss">
.right-chart-base {
  // position: relative;

  .legend {
    position: absolute;
    top: 5.2vh;
    right: 1vw;
  }

  .legend-item {
    position: relative;
    // font-size: 12px;
    margin-right: 0.67vw;

    &::before {
      content: "";
      display: inline-block;
      // width: 10px;
      // height: 10px;
      width: 0.531vw;
      height: 0.531vw;
      background-color: #ccc;
      border-radius: 2px;
      margin-right: 0.22vw;
    }
  }

  .legend-item:nth-child(1):before {
    // width: 12px;
    // height: 2px;
    width: 0.638vw;
    height: 0.1064vw;
    background-color: #f3c000;
    position: absolute;
    top: 50%;
    // left: -16px;
    left: -0.851vw;
    transform: translateY(-50%);
  }
  .legend-item:nth-child(1):after {
    background-color: #f3c000;
    content: "";
    display: inline-block;
    position: absolute;
    // width: 6px;
    // height: 6px;
    width: 0.3191vw;
    height: 0.3191vw;
    border-radius: 100%;
    top: 50%;
    left: -0.851vw;
    // left: -16px;
    transform: translateY(-50%) translateX(50%);
  }

  .legend-item:nth-child(2):before {
    background-color: #12f7f1;
  }

  .legend-item:nth-child(3):before {
    background-color: #58adfa;
  }

  /** add more */
  // .legend-item:nth-child(3):before {
  //   background-color: #f7f1;
  // }
}
</style>