<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts">
  import { defineComponent, PropType, ref, Ref, reactive, watchEffect } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { cloneDeep } from 'lodash-es';
  export default defineComponent({
    name: 'barw',
    props: {
      chartData: {
        type: Array,
        default: () => [],
      },
      option: {
        type: Object,
        default: () => ({}),
      },
      width: {
        type: String as PropType<string>,
        default: '100%',
      },
      height: {
        type: String as PropType<string>,
        default: 'calc(100vh - 78px)',
      },
    },
    setup(props) {
      const chartRef = ref<HTMLDivElement | null>(null);
      const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
      const option = reactive({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
            label: {
              show: true,
              backgroundColor: '#333',
            },
          },
        },
         grid: {
          left: '3%',
          right: '4%',
          bottom: '10%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          axisLine: {
            show: false
          },
          axisTick: {
              show: false
          },
          axisLabel: {
              textStyle: {
                  color: '#999'
              },
              fontSize: 14  // 设置字体大小为14px
          }
        },
        yAxis: {
          
          type: 'category',
          data: [],
          axisLabel: {
            inside: true,
            fontSize: 14,  // 设置字体大小为14px
            textStyle: {
                  color: 'black'
              },
          },
          
        
          z: 10
        },
        series: [
            { // For shadow
              name: '总分',
              type: 'bar',
              label: {
                  show: true,
                  position: 'insideRight'  ,
                  fontSize: 14 , // 设置字体大小为14px
                  textStyle: {
                  color: 'black'
                    },
              },
              itemStyle: {
                   color: 'rgba(0,0,0,0.05)'
              },
              barGap: '-100%',
              barCategoryGap: '40%',
              data: []    
          },
          {
            name: '得分',
            type: 'bar',
            label: {
                show: true,
                position: 'insideRight',
                fontSize: 14 , // 设置字体大小为14px
                textStyle: {
                  color: 'black'
                    },
            },
            itemStyle: {
                color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1,
                    [
                        {offset: 0, color: '#83bff6'},
                        {offset: 0.5, color: '#188df0'},
                        {offset: 1, color: '#188df0'}
                    ]
                )
            },
            data: [],
          },
        ],
      });

      watchEffect(() => {
        props.chartData && initCharts();
      });

      function initCharts() {
        if (props.option) {
          Object.assign(option, cloneDeep(props.option));
        }
        let seriesData = props.chartData.map((item) => {
          return item.value;
        });
        let stdseriesData = props.chartData.map((item) => {
          return item.stdvalue;
        });
        let yAxisData = props.chartData.map((item) => {
          return item.name;
        });
        option.series[1].data = seriesData;
        option.series[0].data = stdseriesData;
        option.yAxis.data = yAxisData;
        setOptions(option);


      }

      console.log('--------why',chartRef);

      
      return { chartRef };
    },

    
  });

    
   



</script>
