在背景長條圖中,怎麼在 label text 後方,以及長條圖上加上圖示 #3148
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
标题:Bar 图中百分比≥100% 时,如何在 label 和柱状图添加图标 1. 在 label 右侧添加图标(如绿勾)问题分析
解决方案 Ant Design Charts 文档暂未支持在 label 直接插入图标(React 节点或 HTML/SVG),可以考虑:
文档相关章节: 2. 在柱状图 100% 位置添加旗子图标/文字问题分析
标准用法: import { Bar } from '@ant-design/charts';
const config = {
data,
xField: 'value',
yField: 'labelName',
seriesField: 'labelName',
label: {
formatter: (datum) => datum.percentage,
},
annotations: [
{
type: 'text',
position: ['100', 'labelName'], // x轴数据为 100 对应的位置
content: '旗子',
style: {
fontSize: 14,
fill: 'red',
},
},
// 如需图片,用 image 类型
{
type: 'image',
position: ['100', 'labelName'],
src: '你的旗子图片 url',
width: 20,
height: 20,
// 可用 offsetX/Y 微调
},
],
};参数说明:
可用 annotation 类型(查看文档):
示例代码: import { Bar } from '@ant-design/charts';
const config = {
data,
xField: 'value',
yField: 'labelName',
label: {
formatter: (datum) => datum.percentage,
},
annotations: data
.map((item) =>
parseFloat(item.percentage) >= 100
? [
{
type: 'text',
position: ['100', item.labelName],
content: '旗子',
style: {
fontSize: 14,
fill: '#f00',
},
},
{
type: 'image',
position: ['100', item.labelName],
src: '/flag.png',
width: 16,
height: 16,
offsetY: -20,
offsetX: 0,
},
]
: []
)
.flat(),
};
<Bar {...config} />总结
相关文档链接如有具体的旗子图片/文本样式需求,可补充说明场景! 此回答由AI自动生成,仅供参考。如有疑问,请等待维护者回复。 |
Beta Was this translation helpful? Give feedback.
-
|
問題解決了,以下擷取相關的部分,分享自己的作法給有需要的開發者們參考: import { Bar } from '@ant-design/charts';
// Render flag SVG
const renderFlag = ({ x, y }: { x: number; y: number }, context: any) => {
const { document } = context;
const g = document.createElement('g', {});
// 使用 document.createElement 繪製各個部分
// 使用 g.appendChild() 加入繪製的部分
return g;
};
// Render check icon SVG
const renderCheckIcon = ({ x, y }: { x: number; y: number }, context: any) => {
const { document } = context;
const g = document.createElement('g', {});
// 使用 document.createElement 繪製各個部分
// 使用 g.appendChild() 加入繪製的部分
return g;
};
const config = {
data: officeData,
xField: 'office_name',
yField: 'count',
paddingRight: 160, // label + check icon
markBackground: {
label: {
text: (originData: OfficeDistributionItem & { originData: OfficeDistributionItem }) => {
const { count, target, percentage } = originData.originData;
return `${count}/${target} (${percentage})`;
},
position: 'right',
dx: 110,
},
},
annotations: [
// Flag: at target value position (only for items that achieved goal)
{
type: 'shape' as const,
data: officeData.filter((item) => item.goalAchieved),
xField: 'office_name',
yField: 'target',
style: {
render: renderFlag,
},
},
// Check icon: to the right of label percentage (only for items that achieved goal)
{
type: 'shape' as const,
data: officeData
.filter((item) => item.goalAchieved)
.map((item) => ({
...item,
xPos: domainMax,
})),
xField: 'office_name',
yField: 'xPos',
style: {
render: (coords: { x: number; y: number }, context: ShapeRenderContext) => {
const checkIconX = coords.x + 10 + 105 + 4;
return renderCheckIcon({ x: checkIconX, y: coords.y }, context);
},
},
},
],
}
<Bar {...config} /> |
Beta Was this translation helpful? Give feedback.

問題解決了,以下擷取相關的部分,分享自己的作法給有需要的開發者們參考: