<script setup>
  import * as echarts from 'echarts'
  import styleUtil from '@/utils/styleUtil'
  import { fitChartSize } from '@/utils/dataUtil'

  const props = defineProps({
    width: {
      type: Number,
      default: () => 0
    },
    height: {
      type: Number,
      default: () => 0
    },
    id: {
      type: String,
      default: () => ''
    }
  })

  let barChart = null

  onMounted(() => {
    init()
  })
  const init = () => {
    // 基于准备好的dom，初始化echarts实例
    barChart = echarts.init(document.getElementById(props.id))
    barChart.setOption({
      grid: {
        left: '5%',
        right: '5%',
        bottom: '5%',
        containLabel: true
      },
      title: {
        text: 'ECharts 入门示例',
        left: 'center'
      },
      tooltip: {},
      xAxis: {
        data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
        axisLabel: {
          fontSize: fitChartSize(20)
        }
      },
      yAxis: {},
      series: [
        {
          name: '销量',
          type: 'bar',
          data: [5, 20, 36, 10, 10, 20]
        }
      ]
    })
    window.addEventListener('resize', resize)
  }
  const resize = () => {
    if (barChart) {
      barChart.resize()
    }
  }
</script>

<template>
  <div
    :id="id"
    :style="{
      width: styleUtil.px2vw(width),
      height: styleUtil.px2vh(height)
    }"
  />
</template>

<style lang="scss"></style>
