<template>
  <div style="height:100%;width:100%" :id="echartsId"></div>
</template>
<script setup>
import * as echarts from "echarts";
import { onMounted, ref, reactive, defineProps, onUnmounted, watch, nextTick } from "vue";
const props = defineProps({
  echartsId:{
    type:String,
    default:'lineId'
  },
  data:{
    type:Object,
    default:{
      list:[],
      colors:[]
    }
  }
});
watch(()=>props.data,val=>{
  nextTick(()=>{ 
     chartFn()
   })
},{immediate:true,deep:true})

function chartFn() {
  var myChart = echarts.init(document.getElementById(props.echartsId));
  var option = {
    grid: {
      top: "16%",
      right: "2%",
      left: "2%",
      bottom: "4%",
      containLabel:true
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        lineStyle: {
          color: "#ddd"
        }
      },
      backgroundColor: "rgba(255,255,255,1)",
      padding: [5, 10],
      textStyle: {
        color: "#7588E4"
      },
    },
    xAxis: {
      type: "category",
      data:props.data.list.map(v=>{return v.label}),
      axisTick: { show: false },
      axisLine: { show: false },
      axisLabel: { color: "#fff",magin:20 },
    },
    yAxis: {
      type: "value",
      splitLine: {
        show:true ,
        lineStyle: {
          type:'dashed',
          color: "rgba(14,95,255,0.5)"
        }
      },
      axisTick: { show: false },
      axisLine: { show: false },
      axisLabel: { color: "#fff" },
    },
    series: [
      {
        type: "bar",
        barGap:'80%',
        barWidth: "20px",
        data: props.data.list.map(v=>{return v.val}),
        label:{
          normal:{show:true,position:'top',color:'#EB00FF'}
        },
        itemStyle: {
          normal: {
            color:function name(params) {
                let colorList = []
                if(props.data.colors && props.data.colors.length>0){
                  props.data.colors.forEach(item => {
                    colorList.push(new echarts.graphic.LinearGradient( 0,0,0, 1,[{offset:0,color:item[0]},{offset:1,color:item[1]}]))
                  });
                }
              if(colorList.length > 0) return colorList[params.dataIndex]
              else return new echarts.graphic.LinearGradient( 0,0,0, 1,[{ offset: 0,color: "rgba(0,244,255,1)"},{ offset: 1, color: "rgba(0,77,167,1)" }])
            },
            shadowColor: "rgba(0,160,221,1)",
            shadowBlur: 2
          }
        },
      }
    ]
  };
  option && myChart.setOption(option);
  window.addEventListener('resize',function(){
    myChart.resize();
  })
}
</script>

<style lang="scss" scoped>

</style>