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

问题来源: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...

相关推荐
衡石科技17 小时前
看板发出去之后:评论、订阅与受控发布的工程链路
大数据·运维·人工智能·数据可视化
金銀銅鐵3 天前
用 graphviz 画关系图:四个例子
数据可视化
Highcharts.js5 天前
Highcharts与TypeScript集成指南
前端·javascript·typescript·前端框架·数据可视化·highcharts
Highcharts.js6 天前
Workbuddy + Highcharts Orbit:工作区数据自动建模与可视化分析实践
前端·javascript·信息可视化·数据可视化·highcharts·可视化图表·workbuddy
Bigger6 天前
别再手搓调色板了:Vue 3 一个组件搞定纯色、渐变和画布吸管取色
前端·vue.js·数据可视化
IndustrialSphere8 天前
工业历史趋势图为什么会误导人?用三条数据检查倒序、缺测和截断
api·数据可视化·工业软件·industrialsphere·历史数据
慧都小妮子14 天前
Wyn商业智能国产数据库适配:数据接入、分析缓存与审计日志验证清单
数据库·数据分析·数据可视化·审计日志·商业智能
Flynn Liang15 天前
Apache Superset 重要特性详解(三):语义层 / 权限 / 缓存 / 嵌入的实战最佳实践(2026 版)
大数据·clickhouse·数据可视化·架构设计·superset·数据分析平台
一晌小贪欢16 天前
Python基础:第 09 篇 · while 循环:重复直到不满足
开发语言·c++·python·数据可视化·面向对象·python办公
报表开不开发18 天前
用开源报表工具SpringReport做车间大屏+Excel 导出,我踩过的一些坑,基于最初版本的设计开发
数据可视化