Newer
Older
Nanping_sponge_GCYPG / src / views / home / leftComp / foot.vue
@liyingjing liyingjing on 25 Oct 3 KB 工程预评估
<template>
  <div ref="assetEchart" class="asset_echart-nopx"></div>
</template>
<script setup>
import { init } from 'echarts';
import { reactive, toRefs, ref, onMounted, watchEffect } from 'vue';
const assetEchart = ref();
let {
  data: { xData, colorList, yData },
} = defineProps(['data']);
console.log('data', xData);
const emit = defineEmits(['select-echart'])
// const all = yData.reduce((n,v)=>{
//   return n + v.value
// },0)
const max = Math.max(...yData)
const initM = () => {
  const chart = init(assetEchart.value);
  window.addEventListener('resize', () => {
    chart.resize();
  });
  chart.setOption({
    grid: {
      left: '5%',
      right: '5%',
      bottom: '5%',
      top: '10%',
      containLabel: true,
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'none',
      },
      formatter: function (params) {
        return (
          params[0].name +
          '<br/>' +
          `<span style='display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:${colorList[params[0].dataIndex]}'></span>` +
          params[0].seriesName +
          ' : ' +
          params[0].value
        );
      },
    },
    xAxis: {
      type: 'value',
      minInterval: 1,
      min: 0,
      axisLine: {
        lineStyle: {
          color: '#6E8098',
        },
      },
      splitLine: {
        show: false,
      },
    },
    yAxis: [
      {
        type: 'category',
        inverse: true,
        axisLabel: {
          show: true
        },
        splitLine: {
          show: false,
        },
        axisTick: {
          show: false,
        },
        axisLine: {
          show: false,
        },
        data: xData,
      },
      {
        type: 'category',
        inverse: true,
        axisTick: 'none',
        axisLine: 'none',
        show: true,
        axisLabel: {
          color: '#000',
          fontSize: '12',
          // formatter: function (value) {
          //   return value + ' %';
          // },
        },
        data: yData,
      },
    ],
    series: [
      {
        name: '数量',
        type: 'bar',
        zlevel: 1,
        itemStyle: {
          borderRadius: 0,
          color: params => {
            return colorList[params.dataIndex];
          },
        },
        barWidth: 20,
        data: yData,
      },
      {
        name: '背景',
        type: 'bar',
        barWidth: 20,
        barGap: '-100%',
        data: [max, max, max, max],
        itemStyle: {
          color: '#EEF5FF',
          borderRadius: 0
        },
      },
    ],
  });
  chart.getZr().on('click', (event) => {
    if (!event.target) {
      // console.log(event)
      emit('select-echart')
    }
  })
};
function chartSize(container, charts) {
  function getStyle(el, name) {
    if (window.getComputedStyle) {
      return window.getComputedStyle(el, null);
    } else {
      return el.currentStyle;
    }
  }
  const hi = getStyle(container, 'height').height;
  charts.style.height = hi;
}
watchEffect(() => {});
onMounted(() => {
  initM();
});
</script>

<style lang="scss" scoped>
.asset_echart-nopx {
  // width: 200px !important;
  height: 200px;
  margin: 10px 14px 0 16px;
  &_echart {
    width: 100%;
    height: 100%;
  }
}
</style>