【echarts 图表叠加自定义背景图片】

文章介绍了如何在Vue应用中使用ECharts库创建一个动态加载数据的饼状图,展示了模板、脚本和样式代码,用于展示不同设备类型的状态统计。
摘要由CSDN通过智能技术生成

效果图:
在这里插入图片描述
代码:

<template>
  <div class="chart-container">
    <!-- 叠加的背景图片 -->
    <div class="background-image"></div>
    <div class="background-image1"></div>
    <div class="background-image2"></div>
    <div class="background-image3"></div>

    <!-- echarts 图表 -->
    <div class="echarts-chart" ref="myEchartsTop"></div>
  </div>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue';
import * as echarts from 'echarts';
import { useRouter } from 'vue-router';
const router = useRouter();
const myEchartsTop = ref<HTMLInputElement | null>(null);
let chartTop: echarts.ECharts;

const loadData = async val => {
  let params = {};
  if (val) {
    params = { search_EQ_C_id: val };
  } else {
  }
  initChart();
};

const statuSumData = ref([
  { value: 60, name: '已完成', itemStyle: { color: '#36e386' } },
  { value: 25, name: '未完成', itemStyle: { color: '#fbd153' } },
  { value: 15, name: '超时', itemStyle: { color: '#e53153' } }
]);

const initChart = () => {
  if (!myEchartsTop.value) {
    return;
  }
  chartTop = echarts.init(myEchartsTop.value);
  const option: echarts.EChartsOption = {
    title: [
      {
        text: `巡更总数 ${statuSumData.value.reduce((acc, cur) => acc + cur.value, 0)}`,
        //subtext: '设备总数',
        left: '14%', // 将 x 改成 left
        top: 'center', // 将 y 改成 top
        textStyle: {
          fontSize: 14,
          fontWeight: 'bold',
          color: '#fff'
        }
      }
    ],
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      top: 'middle',
      right: '5%',
      orient: 'vertical',
      itemGap: 50, // 设置图例项之间的间隔为20像素
      icon: 'circle',
      textStyle: {
        color: '#fff', // 设置图例文字颜色为白色
        fontSize: '23px'
      },
      formatter: function (name) {
        const seriesData = statuSumData.value;
        const total = seriesData.reduce((acc, cur) => acc + cur.value, 0);
        const dataIndex = seriesData.findIndex(item => item.name === name);
        const value = seriesData[dataIndex].value;
        // 添加条件判断,避免除法错误
        const percentage = total !== 0 ? ((value / total) * 100).toFixed(0) : 0;
        return `${name}:       ${percentage}%`;
      }
    },
    series: [
      {
        name: '设备类型',
        type: 'pie',
        center: ['23%', '50%'], // 设置图表位置
        radius: ['52%', '60%'],
        avoidLabelOverlap: false,
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: false,
            fontSize: 16,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: statuSumData.value
      }
    ]
  };
  chartTop.setOption(option);
  window.onresize = function () {
    chartTop.resize();
  };
};

defineExpose({
  loadData
});

onMounted(async () => {
  await loadData();
});
</script>
<style lang="less" scoped>
.chart-container {
  position: relative;
}

.background-image {
  position: absolute;
  top: 9.5%;
  left: 3%;
  width: 81%;
  height: 81%;
  background: url('@/assets/images/lowerCamelCase.png') no-repeat 0 0 / 50% 100%; /* 替换为你的背景图片路径 */
  opacity: 1; /* 根据需要调整透明度 */
}

.background-image1 {
  position: absolute;
  top: 8%;
  left: 50%;
  width: 100%;
  height: 24%;
  background: url('@/assets/images/lowerCamelCase2.png') no-repeat 0 0 / 50% 100%; /* 替换为你的背景图片路径 */
  opacity: 0.7; /* 根据需要调整透明度 */
}

.background-image2 {
  position: absolute;
  top: 38%;
  left: 50%;
  width: 100%;
  height: 24%;
  background: url('@/assets/images/lowerCamelCase2.png') no-repeat 0 0 / 50% 100%; /* 替换为你的背景图片路径 */
  opacity: 0.7; /* 根据需要调整透明度 */
}

.background-image3 {
  position: absolute;
  top: 68%;
  left: 50%;
  width: 100%;
  height: 24%;
  background: url('@/assets/images/lowerCamelCase2.png') no-repeat 0 0 / 50% 100%; /* 替换为你的背景图片路径 */
  opacity: 0.8; /* 根据需要调整透明度 */
}

.echarts-chart {
  width: 100%;
  height: 250px; /* 调整为适当的高度 */
}
</style>

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值