Syncfusion饼/环形图AccumulationChartComponent的dataLabel不显示问题
Syncfusion饼图/环形图数据标签无法显示问题
我用Syncfusion构建饼图和环形图,图表能正常渲染,但数据标签(dataLabel)始终无法显示。查阅官方文档、直接复制示例代码都没能解决问题。
数据示例
const sample = [ { name: "label", value: 45, text: "45%"}, { name: "label1", value: 10, text: "30%"}, { name: "label2", value: 20, text: "10%"}, { name: "label3", value: 5, text: "5%"}, ];
组件配置代码
<AccumulationChartComponent id='charts' legendSettings={{ visible: true, background: 'white' }} height={"500px"} width={'500px'} background={'transparent'} > <AccumulationSeriesCollectionDirective> <Inject services={[AccumulationLegend, PieSeries, AccumulationDataLabel, AccumulationTooltip]} /> <AccumulationSeriesDirective dataSource={sample} xName='name' yName='value' innerRadius='40%' startAngle={0} endAngle={360} radius="70%" explode explodeOffset="10%" explodeIndex={2} dataLabel={{ visible: true, name: 'name', position: 'Outside', }} > </AccumulationSeriesDirective> </AccumulationSeriesCollectionDirective> </AccumulationChartComponent>
当前显示效果

可行解决办法
调整
Inject组件位置
你把Inject放在了AccumulationSeriesCollectionDirective内部,这是错误的。Syncfusion的服务注入组件必须直接作为AccumulationChartComponent的子元素,与AccumulationSeriesCollectionDirective同级。修改后结构如下:<AccumulationChartComponent ...> <Inject services={[AccumulationLegend, PieSeries, AccumulationDataLabel, AccumulationTooltip]} /> <AccumulationSeriesCollectionDirective> <AccumulationSeriesDirective ... /> </AccumulationSeriesCollectionDirective> </AccumulationChartComponent>修正dataLabel配置项
配置中的name不是有效字段,若要显示数据里的自定义内容,可使用content属性指定字段:dataLabel={{ visible: true, content: '{text}', // 绑定数据中的text字段 position: 'Outside', // 显式设置样式避免和背景融合 style: { color: '#333', fontSize: '12px' } }}也可以用模板组合字段,比如
content: '{name}: {value}%'。排查样式覆盖问题
图表背景为透明,若标签颜色与页面背景一致会导致看不见,务必给dataLabel显式设置文字颜色。核对依赖版本
若存在版本兼容问题,尝试将Syncfusion React图表包升级至最新稳定版,或与官方示例使用相同版本。
内容的提问来源于stack exchange,提问作者BlackH3art
相关产品推荐
相关产品推荐

