<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: {
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: [120, 200, 150, 80, 70, 110, 130],
        type: 'bar'
      }
    ]
  },//默认的配置项
  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>