<template>
  <div ref="divRef" style="height: 100%;">
  </div>
</template>

<script setup>
import * as echarts from 'echarts';
import { computed, onMounted, reactive, ref, watch, } from 'vue';
const props = defineProps({
  option: {
    type: Object,
    default: () => ({})
  }
})
const divRef = ref()
const state = reactive({
  defaultOption: {
    title: {
      text: 'ECharts 入门示例'
    },
    tooltip: {},
    xAxis: {
      data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
    },
    yAxis: {},
    series: [
      {
        name: '销量',
        type: 'bar',
        data: [5, 20, 36, 10, 10, 20]
      }
    ]
  },//默认的配置项
  finallyOption: computed(() => {
    return {
      ...state.defaultOption,
      ...props.option
    }
  }),//与传入配置Object.assign()之后的最终配置项
  myChart: null,
  renderChart: () => {
    if (state.myChart) {
      state.myChart.setOption(state.finallyOption)
    }
  }//渲染函数
})
watch(() => state.finallyOption, () => {
  state.renderChart()
}, { deep: true })

onMounted(() => {
  // 基于准备好的dom，初始化echarts实例
  state.myChart = echarts.init(divRef.value);
  // 绘制图表
  state.renderChart()
})
</script>

<style scoped></style>