<template>

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

<script>
import * as echarts from 'echarts';

export default {
  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() {
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          backgroundColor: "rgba(255,255,255,0.75)",
          extraCssText: "box-shadow: 2px 2px 4px 0px rgba(0,0,0,0.3);",
          textStyle: {
            fontSize: 14,
            color: "#000",
          },
          formatter: (params) => {
            const item = params[0];
            return item.name + " : " + item.value + " 条";
          },
        },
        grid: {
          top: 80,
          left: 60,
          right: 40,
          bottom: 60
        },
        title: {
          text: '达到预估寿命极限的设备统计', // 标题内容  // 水平居中
          top: '7%',
          left: '3%',   // 距离顶部5%
          textStyle: {     // 文字样式
            color: '#fff',
            fontSize: 16,
            fontWeight: 'normal'
          }
        },
        xAxis: [
          {
            type: "category",
            axisLabel: {
              interval: 0,
              color: "#fff",
              fontSize: 12,
            },
            axisLine: {
              lineStyle: {
                //y轴网格线设置
                color: "rgba(0, 168, 255, 0.4)",
                width: 1,
              },
            },
            axisTick: {
              show: false,
            },
            data: ['2025', '2026', '2027', '2028', '2029', '2030', '2031', '2032', '2033', '2034'],
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "",
            nameTextStyle: {
              color: "#fff",
              fontWeight: 400,
              fontSize: 14,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(0, 168, 255, 0.3)",
                width: 1,
              },
            },
            splitLine: {
              show: false,
            },
            axisLabel: {
              show: true,
              fontSize: 12,
              color: "#fff",
            },
          },
        ],
        series: [
          {
            type: "bar",
            barWidth: 16,
            label: {
              show: true,
              position: "top",
              color: "#00A8FF",
            },
            itemStyle: {
              borderRadius: [8, 8, 0, 0],
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#00A8FF",
                },
                {
                  offset: 1,
                  color: "rgba(0, 122, 255, 0)",
                },
              ]),
            },
            data: [134, 54, 342, 76, 153, 55, 77, 324, 252, 234],
          },
        ],
      })
    }
  }
}
</script>
