echarts利用graphic属性给饼图添加内圈图片及外圈图片(可自适应宽度位于饼图中心)

最终效果图:

目录

前言

思路是看到这个博客启发的:点击跳转查看博客,然后在graphic属性里改了我的实际需求,譬如图片的宽高、图片位置等。

资源

外围图片、内维图片可以自己定义。这里主要用到了两个图片:

主要部分

graphic介绍

在图表的option配置里,给graphic添加图片以及图片设置。left和top均设置为"center",可根据屏幕分辨率自动判断中心位置。

graphic 用于添加自定义图形或文本元素到图表中的配置项,可添加文本、图形、图片等自定义元素,

这个属性通常用于创建一些特殊的标注或装饰,比如在特定位置添加一个文本标签、绘制一个自定义形状,或者放置一个图像等

常用的是添加文字和图片,下面是给图表通过graphic添加自定义文字的方法:

javascript 复制代码
data.linebalance = 66.123;
option.graphic.push(
          {
            type: "text",
            right: 155,
            top: 28,
            style: {
              text: "人工站线平衡率:",
              fill: "#fff", // 文字颜色
              fontSize: 14, // 文字大小
            },
          },
          {
            type: "text",
            right: 100,
            top: 28,
            style: {
              text: (data.linebalance * 100).toFixed(1) + "%",
              fill: "#fc8c1c", // 文字颜色
              fontSize: 14, // 文字大小
            },
          }
        );
}

style介绍

style 是其中一个常用的属性,用于设置图形元素的样式
image:指定图形元素所使用的图片资源,可以是图片的 URL 或者 dataURI。

width:指定图形元素的宽度。可以是整数值(像素),也可以是百分比值。如果需要自适应图表宽度,可以使用百分比值,但需要注意,有些情况下百分比值可能导致图片不显示,这可能是因为容器大小未正确设置或者其他原因。

height:指定图形元素的高度。同样可以是整数值(像素)或者百分比值。

opacity:指定图形元素的不透明度,取值范围为 0(完全透明)到 1(完全不透明)之间。

代码

通过graphic添加自定义图片的方法:

javascript 复制代码
graphic: [
          {
            type: "image",
            id: "logo",//唯一值,不可与其他graphic里的id一致
            left: "center", //调整图片位置
            top: "center", //调整图片位置
            z: -10,//确保图片在饼图下方
            //设置图片样式
            style: {
              image: centerImg,
              //   width: 80,
              //   height: 80,
              width: ciclrImgChart.getWidth() - 105,
              height: ciclrImgChart.getWidth() - 105,
              opacity: 1,
            },
          },
          {
            type: "image",
            id: "logo2",//唯一值,不可与其他graphic里的id一致
            left: "center",
            top: "center",
            z: -10,
            //设置图片样式 width、height只能用整型值,不可用百分比(会导致图片不显示)
            style: {
              image: outCircleImg,
              //   width: 200,
              //   height: 200,
              width: ciclrImgChart.getWidth() + 5,
              height: ciclrImgChart.getWidth() + 5,
              opacity: 1,
            },
          },
        ],

加载饼图方法(option所有的配置)

javascript 复制代码
// 初始化圈形图
    initCircleImgFun() {
      var centerImg = require("./images/你的内圈图片路径.png");
      var outCircleImg = require("./images/你的外圈图片路径.png");

      if (ciclrImgChart != null && ciclrImgChart != "" && ciclrImgChart != undefined) {
        ciclrImgChart.dispose();
      }
      ciclrImgChart = echarts.init(document.getElementById("circleImgChart"));
      var option;
      option = {
        // 用于添加自定义图形或文本元素到图表中的配置项,可添加文本、图形、图片等自定义元素
        graphic: [
          {
            type: "image",
            id: "logo",
            left: "center", //调整图片位置
            top: "center", //调整图片位置
            z: -10,
            //设置图片样式
            style: {
              image: centerImg,
              //   width: 80,
              //   height: 80,
              width: ciclrImgChart.getWidth() - 105,
              height: ciclrImgChart.getWidth() - 105,
              opacity: 1,
            },
          },
          {
            type: "image",
            id: "logo2",//唯一值,不可与其他graphic里的id一致
            left: "center",
            top: "center",
            z: -10,
            //设置图片样式 width、height只能用整型值,不可用百分比(会导致图片不显示)
            style: {
              image: outCircleImg,
              //   width: 200,
              //   height: 200,
              width: ciclrImgChart.getWidth() + 5,
              height: ciclrImgChart.getWidth() + 5,
              opacity: 1,
            },
          },
        ],
        series: [
          {
            type: "pie",
            radius: ["85%", "65%"],
            data: [
              { value: 60, name: "4H未上架", percent: "30%" },
              { value: 60, name: "0~2H未上架", percent: "30%" },
              { value: 10, name: "2~4H未上架", percent: "10%" },
              { value: 60, name: "正在检验", percent: "30%" },
            ],
            hoverAnimation: false,
            label: {
              normal: {
                show: false,
              },
            },
            color: ["#ff8d1a", "#29c4e3", "#2a82e4", "#43cf7c"],
          },
        ],
      };

      option && ciclrImgChart.setOption(option);
    },
相关推荐
_菜鸟果果3 天前
Vue3+echarts 3d饼图
前端·javascript·echarts
java水泥工3 天前
基于Echarts+HTML5可视化数据大屏展示-电信厅店营业效能分析
前端·echarts·html5·大屏展示
paopaokaka_luck3 天前
基于SpringBoot+Vue的数码交流管理系统(AI问答、协同过滤算法、websocket实时聊天、Echarts图形化分析)
vue.js·人工智能·spring boot·websocket·echarts
麦麦大数据4 天前
F024 RNN+Vue+Flask电影推荐可视化系统 python flask mysql 深度学习 echarts
python·rnn·深度学习·vue·echarts·电影推荐
xiaohe06015 天前
🎉 Uni ECharts 2.0 正式发布:原来在 uni-app 中使用 ECharts 可以如此简单!
uni-app·echarts
码界筑梦坊7 天前
267-基于Django的携程酒店数据分析推荐系统
python·数据分析·django·毕业设计·echarts
HashTang8 天前
2025年10月实时最新获取地图边界数据方法,省市区县街道多级联动【文末附实时geoJson数据下载】
信息可视化·echarts·geojson·乡镇geojson·街道边界·geomap·街道geo
啦工作呢8 天前
数据可视化 ECharts
前端·信息可视化·echarts
U.2 SSD10 天前
Echart仪表盘示例
javascript·echarts
U.2 SSD11 天前
ECharts漏斗图示例
前端·javascript·echarts