Newer
Older
KaiFengPC / src / views / spongePerformance / mainIndex / top.vue
@zhangdeliang zhangdeliang on 23 May 5 KB 初始化项目
<template>
  <div>
    <div ref="assetEchart" class="asset_echart"></div>
  </div>
</template>
<script setup>
import { init } from 'echarts';
const assetEchart = ref(null);
let {
  xData,
  bottom,
  yData,
  yData2,
  all,
  realValue,
  itemUnit,
  itemTarget,
  data: { titleArray },
} = defineProps(['data', 'bottom', 'xData', 'yData', 'yData2', 'all', 'realValue', 'itemUnit', 'itemTarget']);

const initM = () => {
  const chart = init(assetEchart.value);
  window.addEventListener('resize', () => {
    chart.resize();
  });
  chart.setOption({
    grid: {
      top: '0%',
      right: '0%',
      left: '-10%',
      bottom: '11%',
      containLabel: true,
    },
    tooltip: {
      show: true,
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
      textStyle: {
        fontSize: 12,
      },
      formatter: function (params) {
        let dom = `${params[0].name}
              <br/>`;
        for (let i of params) {
          dom += `${i.marker}
              ${i.seriesName}
                :
                ${i.seriesName.indexOf('%')>-1?(i.value*1).toFixed(2):i.value}
              <br/>`;
        }
        return dom;
      },
    },
    legend: {
      show: false,
      itemWidth: 14,
      top: '0',
      left: 'center',
      data: titleArray,
      textStyle: {
        color: '#c6c6c6',
      },
    },
    xAxis: [
      {
        type: 'category',
        color: '#c6c6c6',
        data: xData,
        axisLabel: {
          margin: 10,
          color: '#c6c6c6',
          textStyle: {
            fontSize: 13,
          },
        },
        axisLine: {
          lineStyle: {
            color: '#c6c6c6',
          },
        },
        axisTick: {
          show: true,
          lineStyle: {
            color: '#ddd',
          },
        },
      },
    ],
    yAxis: [
      {
        min: 0,
        show: false,
        axisLabel: {
          color: '#c6c6c6',
          textStyle: {
            fontSize: 16,
          },
        },
        axisLine: {
          lineStyle: {
            color: '#c6c6c6',
          },
        },
        axisTick: {
          show: false,
        },
        splitLine: {
          lineStyle: {
            color: '#ddd',
            type: 'dashed',
          },
        },
      },
    ],
    series: [
      {
        name: titleArray[0] + itemUnit,
        type: 'bar',
        data: yData,
        barWidth: '30px',
        itemStyle: {
          normal: {
            label: {
              //柱体上显示数值
              rotate: 30, // 设置倾斜角度
              interval: 0,
              show: true, //开启显示
              // position: 'center',//在上方显示
              textStyle: {
                //数值样式
                fontSize: '10px',
                color: '#fff',
              },
              formatter: function (params) {
                return realValue > 0 ?itemUnit=='%' ?  (realValue*1).toFixed(2) + itemUnit  :realValue + itemUnit : '0';
                // return realValue > 0 ? realValue + itemUnit : '0';
              },
            },
            color: {
              type: 'linear',
              x: 0, //右
              y: 0, //下
              x2: 0, //左
              y2: 1, //上
              colorStops: [
                {
                  offset: 0,
                  color: '#4487F5  ', // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: '#A7EFFF ', // 100% 处的颜色
                },
              ],
            },
            barBorderRadius: [0, 0, 0, 0],
          },
        },
      },
      {
        name: titleArray[1] + itemUnit,
        type: 'bar',
        data: yData2,
        barWidth: '30px',
        itemStyle: {
          normal: {
            label: {
              //柱体上显示数值
              rotate: 30, // 设置倾斜角度
              interval: 0,
              show: true, //开启显示
              textStyle: {
                //数值样式
                fontSize: '10px',
                color: '#fff',
              },
              formatter: function (params) {
                return itemTarget > 0 ?itemUnit=='%' ?  (itemTarget*1).toFixed(2) + itemUnit : itemTarget + itemUnit : '0';
                // return itemTarget > 0 ? itemTarget + itemUnit : '0';
              },
            },
            color: {
              type: 'linear',
              x: 0, //右
              y: 0, //下
              x2: 0, //左
              y2: 1, //上
              colorStops: [
                {
                  offset: 0,
                  color: '#48E38F  ', // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: '#D1FBE4  ', // 100% 处的颜色
                },
              ],
            },
            barBorderRadius: [0, 0, 0, 0],
          },
        },
      },
    ],
  });
};

onMounted(() => {
  initM();
});
</script>

<style lang="scss" scoped>
.asset_echart {
  width: 148px;
  height: 150px;
  // margin: 0 0 0 1px;
  &_echart {
    width: 100%;
    height: 100%;
  }
}
</style>