<template>
  <div :class="className" style="height: 300px" />
</template>

<script>
import echarts from 'echarts'

export default {
  name: 'HotDataChart',
  props: {
    className: {
      type: String,
      default: 'chart'
    }
  },
  data() {
    return {
      chart: null,
      option: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          top: '0%',
          left: '0%',
          right: 20,
          bottom: 20,
          containLabel: true
        },
        xAxis: {
          type: 'value',
          position: 'top',
          splitLine: {
            lineStyle: {
              type: 'dashed'
            },
            show: true // 隐藏或显示
          },
          splitArea: {
            show: false
          },
          axisLine: { show: false },
          axisTick: { show: false },
          axisLabel: { color: this.$store.state.settings.darkTheme?'#fff':'#000' }
        },
        yAxis: {
          type: 'category',
          inverse: true,
          axisLabel: { textStyle: { color: this.$store.state.settings.darkTheme?'#fff':'#000' }},
          axisLine: {
            lineStyle: {
              color: '#ddd'
            }
          },
          axisTick: {
            show: false
          },
          scale: true
        },
        series: [{
          type: 'bar',
          color: '#ff6160',
          barWidth: '20',
          label: {
            show: true,
            position: 'right'
          },
          itemStyle: {
            color: '#0088ff'
          }
        }]
      }
      this.chart.setOption(this.option)
    },
    setData(chartData) {
      if (!chartData || !chartData.count || chartData.count.length === 0) {
        if (this.chart) {
          this.chart.clear()
        }
        return
      }
      const num = chartData.count.length
      this.option.xAxis.max = (value) => {
        return parseInt(value.max + (value.max + value.min) / num)
      }
      this.option.yAxis.data = chartData.resourceName
      this.option.series[0].data = chartData.count
      this.chart.setOption(this.option)
    }
  }
}
</script>

<style scoped>

</style>
