<template>
  <div :id="chartId" :style="{width: '100%', height: `${chartSettings.height||'3rem'}` }"></div>
</template>
  
<script>
import echartMixins from "@/utils/resizeMixins";
export default {
  props: {
    chartId: { type: String, default: "pieChart" },
    chartSettings: { type: Object, default: () => {} },
    chartXData: {
      type: Array,
      default: () => [],
    },
    chartYData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return { chart: null };
  },
  mixins: [echartMixins],
  mounted() {
    this.draw();
  },
  watch: {
    chartYData(val) {
      if (val.length > 0) {
        this.draw();
      }
    },
  },
  methods: {
    draw() {
      this.chart = this.$echarts.init(document.getElementById(this.chartId));
      let option = {
        backgroundColor: 'rgb(6, 17, 67)',
        grid: {
          left: "10%",
          right: "5%",
          top: "20%",
          bottom: "10%",
          containLabel: true
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        xAxis: {
          type: "category",
          data: this.chartXData,
          axisLabel: {
            color: "white",
            fontSize: 12,
            interval: this.chartSettings.interval || 'auto',
            rotate: this.chartSettings.rotate || '0'
          },
          axisLine: {
            lineStyle: {
              color: "rgba(255,255,255,0.3)"
            }
          },
          splitLine: {
            show: false
          }
        },
        yAxis: {
          type: "value",
          axisLabel: {
            color: "white",
            fontSize: 12
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: 'rgba(255,255,255,0.1)',
              width: 1,
              type: 'solid'
            }
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.3)"
            }
          }
        },
        legend: {
          data: this.chartSettings.legendTextArr,
          right: 10,
          top: 0,
          textStyle: {
            color: "white",
            fontSize: 12
          },
          itemWidth: 12,
          itemHeight: 12,
          icon: 'rect'
        },
        series: [
          {
            data: this.chartYData,
            type: "bar",
            barWidth: 30,
            name: this.chartSettings.legendTextArr[0],
            itemStyle: {
              color: '#ff0000'
            }
          }
        ]
      };
      this.chart.setOption(option);
    },
  },
};
</script>