<!--
 * @Author: your name
 * @Date: 2021-10-26 17:53:37
 * @LastEditTime: 2021-12-02 16:30:14
 * @LastEditors: Please set LastEditors
 * @Description: 成本分析柱状图
 * @FilePath: \hyyfScreen\src\views\Production\board\ChartCostLine.vue
-->
<template>
  <div class="cost-histogram">
    <div id="costHistogram"></div>
  </div>
</template>
<script>
export default {
  props: {
    data: {
      type: Array,
      default: () => [],
    },
    xAxisData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      myChart: null,
    };
  },
  methods: {
    init() {
      const commonSeriesItem = {
        name: "Forest",
        type: "bar",
        barGap: 0,
        emphasis: {
          focus: "series",
        },
      };
      let seriesData = [
        {
          ...commonSeriesItem,
          name: "公摊成本",
          color: "rgb(112,182,3)",
          data: [],
        },
        {
          ...commonSeriesItem,
          name: "兽药成本",
          color: "#3aa0ff",
          data: [],
        },
        {
          ...commonSeriesItem,
          name: "饲料成本",
          color: "#975fe4",
          data: [],
        },
      ];
      this.data.forEach((item) => {
        seriesData[0].data.push(item.gtValue);
        seriesData[1].data.push(item.syValue);
        seriesData[2].data.push(item.slValue);
      });
      let options = {
        // title: {
        //   text: '存栏变动',
        //   x: 10,
        //   y: 0
        // },
        tooltip: {
          trigger: "axis",
          // formatter: (value) => {
          //   return value + "元";
          // },
        },
        // legend: {
        //   data: ['头']
        // },
        // color: ['#3aa0ff', '#4dcb73', '#fad337', '#f2637b', '#975fe4'],
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          // {
          //   type: "value",
          //   name: "元",
          //   boundaryGap: [0, 0.01],
          //   axisPointer: {
          //     type: "shadow",
          //   },
          //   axisTick: {
          //     show: false,
          //   },
          // },
          {
            type: "category",
            axisTick: {
              show: false,
            },
            axisPointer: {
              type: "none",
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#fff",
              },
            },
            axisLabel: {
              textStyle: {
                fontSize: 10,
              },
            },
            data: this.xAxisData,
          },
        ],
        //[
        yAxis: {
          axisTick: {
            show: false,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "#fff",
            },
          },
          type: "value",
          name: "元",
        },
        series: seriesData,
      };
      const _this = this;
      this.myChart.setOption(options);
      this.myChart.on("click", function(param) {
        _this.$emit("changeCostIndex", param.dataIndex);
      });
    },
  },
  mounted() {
    this.myChart = this.$echarts.init(document.getElementById("costHistogram"));
    this.init();
  },
  watch: {
    data(value) {
      console.log("成本分析柱状图数据", value);
      this.init();
    },
  },
};
</script>
<style scoped>
.cost-histogram {
  width: 90%;
  height: 50%;
}
#costHistogram {
  width: 100%;
  height: 100%;
}
</style>
