<template>
    <echartsBase :option="setOption"/>
</template>

<script setup>
import { nextTick, onMounted, ref, watch } from 'vue'
import echartsBase from './index.vue'

const props = defineProps({
    title: {
        type: String,
        default: '管理费用项目分摊占比情况'
    },
    color: {
        type: Array,
        default: () => (['#5087EC', '#68BBC4'])
    },
    unit: {
        type: String,
        default: '元'
    },
    datas: {
        type: Array,
        default: () => ([])
    },
    name:{
          type: String,
         default: ''
    },
    ydata:{
         type: Array,
         default: () => ([])
    },
    xdata:{
        type: Array,
         default: () => ([])
    }
})

//初始变量
const setOption = ref({})
const titles = ref(props.title)
const colors = ref(props.color)
const units = ref(props.unit)
const datas = ref(props.datas)
const xdatas = ref(props.xdata)
const ydatas = ref(props.ydata)
const names=ref(props.name)

watch(() => [
    props.title,
    props.color,
    props.unit,
    props.name,
    props.xdata,
    props.ydata,
], ([title, color, unit,name,xdata,ydata]) => {
    titles.value = title
    colors.value = color
    units.value = unit
    names.value=name
    xdatas.value=xdata
    ydatas.value=ydata
    setOptions()
})

//深度监听数据变化
watch(() => [
    props.datas,
], ([data]) => {
    datas.value = data
    setOptions()
}, { deep: true })


//设置图表
const setOptions = () => {
    setOption.value = {
        color: colors.value,
        title: {
    
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    },
    formatter:function(params){
　　　　var relVal = params[0].name;
　　　　for (var i = 0, l = params.length; i < l; i++) {
　　　　　　relVal += '<br/>' + params[i].seriesName + ' : ' + params[i].value+units.value;
　　　　}
　　　　return relVal;
　　}

  },
  legend: {},
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'value',
    boundaryGap: [0, 0.01],
   
    axisLabel: {
        formatter: `{value}`+units.value // 在每个x轴坐标都添加了单位
    }
   
  
  },
  yAxis: {
    type: 'category',
    // data: ['张三', '李四', '王五', '赵六', '陈琦', '五把'],
    data: ydatas.value,
   
  },
  series: [
    {
      name: names.value,
      type: 'bar',
    //   data: [10, 20, 40, 60, 80, 99],
      data: xdatas.value,
      
    },
  
    
  ]
}
}

//渲染完成
onMounted(() => {
    nextTick(() => {
        setOptions()
    })
})
</script>
