echarts饼图中间嵌套图片

最近做大屏,ui的样式图,需要一个饼图中间嵌套图片外层还有个圆环阴影效果,看一下实现效果:

其实实现方式和其他echarts是一样的,只是关键配置不同,看一下实现代码:

<template>
  <div class="chart">
    <div id="typeChart" class="box"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  data() {
    return {
      xData: [
        { value: 2456, name: "产品一", percent: "69.65%" },
        { value: 501, name: "产品二", percent: "14.21%" },
        { value: 218, name: "产品三", percent: "6.18%" },
        { value: 198, name: "产品四", percent: "5.62%" },
        { value: 153, name: "产品五", percent: "4.43%" },
      ],
    };
  },

  mounted() {
    this.getChartInit();
  },
  methods: {
    // echarts适配
    echartResize() {
      this.chart.resize(); // 适配窗口大小
    },
    getChartInit() {
      let myChart = document.getElementById("typeChart");
      this.chart = echarts.init(myChart);
      //引入需要使用图片
      var centerImg = require("./img/typeImg.png");
      const option = {
        tooltip: {
          trigger: "item",
        },
        grid: {
          top: "0%",
          bottom: "0%",
          left: "0%",
          right: "0%",
          containLabel: false,
        },
        //中心图片配置(关键代码)
        graphic: [
          {
            z: 4,
            type: "image",
            id: "logo",
            left: "29%", //调整图片位置
            top: "24%", //调整图片位置
            z: -10,
            bounding: "raw",
            rotation: 0, //旋转
            origin: [64.5, 32.5], //中心点
            scale: [1.0, 1.0], //缩放
            //设置图片样式
            style: {
              image: centerImg,
              width: 136,
              height: 136,
              opacity: 1,
            },
          },
        ],
        series: [
          {
            type: "pie",
            radius: ["94%", "60%"],
            data: this.xData,
            hoverAnimation: false,
            itemStyle: {
              normal: {
                borderColor: "#03125D", //设置间隔的背景颜色
                borderWidth: 10, //设置各个数据之间间隔
              },
            },
            label: {
              normal: {
                show: false,
              },
            },
            color: ["#2E4FF7", "#00EAE8", "#CC32F5", "#FF5050", "#FFA700"],
          },

          {
            name: "", //外圆环
            tooltip: {
              show: false,
            },
            type: "pie",
            radius: ["100%", "90%"], //设置圆环宽度
            center: ["50%", "50%"], //设置外圆环位置
            hoverAnimation: false,
            data: [
              {
                value: 300,
                name: "",
                itemStyle: {
                  normal: {
                    color: "#0C398B", //修改圆环颜色
                    opacity: 1,
                  },
                },
              },
            ],
          },
        ],
      };
      this.chart.setOption(option);
      // 监听事件
      window.addEventListener("resize", this.echartResize);
    },
  },
  destroyed() {
    // 解绑事件
    window.removeEventListener("resize", this.echartResize);
  },
};
</script>

<style lang="less" scoped>
.chart {
  width: 600px;
  height: 330px;
  background-color: #03135f;
  display: flex;
  .box {
    width: 330px;
    height: 250px;
    margin-left: 46px;
    margin-top: 40px;
  }
}
</style>

要在echarts饼图中间放字,可以通过在配置项中设置标题和文本图形来实现。在给定的代码中,可以在option对象中的title和graphic属性中进行相应的设置。 在title属性中,可以设置文本的内容、位置和样式。例如,可以将文本设置为"资产总数",位置设置为居中,并指定填充颜色、字体大小和字体粗细。 在graphic属性中,可以创建一个文本图形,并设置其位置和样式。例如,可以将文本设置为"资产总数",位置设置为居中,并指定填充颜色和字体大小。 以下是修改后的代码示例: ``` var option = { // 其他配置项... title: { text: "资产总数", left: "center", top: "50%", textStyle: { textAlign: "center", fill: "#333", fontSize: 18, fontWeight: 400 } }, graphic: { type: "text", left: "center", top: "40%", style: { text: "资产总数", textAlign: "center", fill: "#333", fontSize: 20 } }, // 其他配置项... }; ``` 在上述代码中,title属性指定了文本的内容为"资产总数",并设置了位置和样式。graphic属性创建了一个文本图形,将文本设置为"资产总数",并设置了位置和样式。 这样,echarts饼图中间就会显示"资产总数"这个文本。12 #### 引用[.reference_title] - *1* *2* [Echarts 饼图中间添加文字](https://blog.csdn.net/zuo_kaizheng/article/details/117330963)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值