如何自定义雷达图富文本轴标签样式?

问题来源:VChart 能否支持类似富文本样式的轴标签样式?

问题描述

期望在标签中支持自定义 icon 和不同的文本样式,类似于:

解决方案

首先,分析一下需求,图中是极坐标系下角度轴的标签。

  1. 添加轴配置

    1. 在 VChart 中可以通过 axes 属性来配置轴,axes 接收一个数组,添加一项,将 axes[0].type: 'angle' 设置轴类型为角度轴;
  2. 配置轴标签

    1. 通过格式化函数 formatMethod 配置 axes[0].label 为富文本。
    2. formatMethod 返回一个富文本内容的配置对象
    3. type: 'rich':定义返回文本类型为富文本
    4. text: 富文本的详细配置。支持文字和图片两种类型,详细配置可以参考配置项文档
scala 复制代码
formatMethod: (value, data, c, d) => {
  return {
    type: "rich",
    text: [
      {
        image:
          '<svg t="1714116158819" class="icon" viewBox="0 0 1228 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="19433" width="200" height="200"><path d="M1152 76.8v870.4h-1075.2v-870.4h1075.2M1228.8 0H0v1024h1228.8V0z" fill="#0686E5" p-id="19434"></path><path d="M0 0h1228.8v1024H0z" fill="#0686E5" p-id="19435"></path></svg>',
        width: 2,
        height: 10,
      },
      {
        text: ` ${value} `,
        fontSize: 16,
        fill: "black",
        fontWeight: "bold",
      },
      {
        text: ` ${values.find((v) => v.key === value)?.value} `,
        fontSize: 16,
        fill: "rgb(22,100,255)",
        fontWeight: "bold",
      },
    ],
  };
},

代码示例

🔔 下面的代码,可以复制粘贴到编辑器中查看效果

yaml 复制代码
const values = [
        {
          key: 'Strength',
          value: 5
        },
        {
          key: 'Speed',
          value: 5
        },
        {
          key: 'Shooting',
          value: 3
        },
        {
          key: 'Endurance',
          value: 5
        },
        {
          key: 'Precision',
          value: 5
        },
        {
          key: 'Growth',
          value: 5
        }
      ];

const spec = {
  type: 'radar',
  data: [
    {
      id: 'radarData',
      values
    }
  ],
  categoryField: 'key',
  valueField: 'value',
  point: {
    visible: false // disable point
  },
  area: {
    visible: true, // display area
    state: {
      // The style in the hover state of the area
      hover: {
        fillOpacity: 0.5
      }
    }
  },
  line: {
    style: {
      lineWidth: 4
    }
  },
  axes: [
    {
      orient: 'radius', // radius axis
      zIndex: 100,
      min: 0,
      max: 8,
      domainLine: {
        visible: false
      },
      label: {
        visible: true,
        space: 0,
        style: {
          textAlign: 'center',
          stroke: '#fff',
          lineWidth: 4
        }
      },
      grid: {
        smooth: false,
        style: {
          lineDash: [0]
        }
      }
    },
    {
      orient: 'angle', // angle axis
      zIndex: 50,
      tick: {
        visible: false
      },
      domainLine: {
        visible: false
      },
      label: {
        space: 20,
         formatMethod: (value, data,c,d) => {
          console.log(value,data,c,d)
      return {
        type: 'rich',
        text: [
          {
            image: '<svg t="1714116158819" class="icon" viewBox="0 0 1228 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="19433" width="200" height="200"><path d="M1152 76.8v870.4h-1075.2v-870.4h1075.2M1228.8 0H0v1024h1228.8V0z" fill="#0686E5" p-id="19434"></path><path d="M0 0h1228.8v1024H0z" fill="#0686E5" p-id="19435"></path></svg>',
            width: 2,
            height: 10
          },
          {
            text: ` ${value} `,
            fontSize: 16,
            fill: 'black',
            fontWeight: 'bold'
          },
           {
            text: ` ${values.find(v => v.key === value)?.value} `,
            fontSize: 16,
            fill: 'rgb(22,100,255)',
            fontWeight: 'bold'
          }
        ]
      };
    }
      },
      grid: {
        style: {
          lineDash: [0]
        }
      }
    }
  ]
};

const vchart = new VChart(spec, { dom: CONTAINER_ID });
vchart.renderSync();

// Just for the convenience of console debugging, DO NOT COPY!
window['vchart'] = vchart;

相关文档

富文本 demo:visactor.io/vchart/demo...

相关api:visactor.io/vchart/opti...

github:github.com/VisActor/VC...

相关推荐
阿达_优阅达6 小时前
Tableau 2025.3 发布!可视化扩展升级、Server 版 Agent、平台数据 API,让 AI 深度融入业务工作流
人工智能·ai·数据分析·数据可视化·仪表板·tableau·版本更新
希艾席帝恩6 小时前
数字孪生如何重塑现代制造体系?
大数据·人工智能·数字孪生·数据可视化·数字化转型
Pyeako10 小时前
Python数据可视化--matplotlib库
python·matplotlib·数据可视化·画图·pylab
数据科学项目实践1 天前
建模步骤 3 :数据探索(EDA) — 1、初步了解数据:常用函数
人工智能·python·机器学习·数据挖掘·数据分析·pandas·数据可视化
AI小云1 天前
【数据操作与可视化】Serborn绘图-类别散点图和热力图
python·数据可视化
数据科学项目实践4 天前
建模步骤 3 :数据探索(EDA) — 1、初步了解数据:自定义函数
大数据·人工智能·python·机器学习·matplotlib·数据可视化
NocoBase4 天前
6 个最佳开源 AI 仪表盘工具
低代码·开源·数据可视化
云天徽上7 天前
【数据可视化-162】数据见证大爱!企业捐款驰援香港数据可视化分析(Pyecharts实现)
开发语言·python·信息可视化·数据可视化·pyecharts
互联网全栈开发实战8 天前
一款超优秀的数据可视化平台(大屏设计器【2025年 - 2026年】)-GoView(构建数字孪生可视化新世界)
信息可视化·数据分析·node.js·vue·数据可视化·大屏端
希艾席帝恩8 天前
从制造到“智造”:数字孪生驱动的工业革命
大数据·人工智能·数字孪生·数据可视化·数字化转型