如何将Syncfusion .NET MAUI SfCircularChart的数据标签设为扇区占比?
.NET MAUI Syncfusion环形图百分比标签实现方案
- 替代Xamarin中
LabelContent="Percentage"的功能,在.NET MAUI Syncfusion环形图里可通过LabelFormat属性配合数值格式化实现,具体方案如下:- 直接格式化小数数值:若数据是0~1区间的小数(如0.35代表35%),直接设置
LabelFormat为"{0:P}"即可自动转为百分比格式:
<syncfusion:DoughnutSeries ItemsSource="{Binding ChartData}" XBindingPath="Category" YBindingPath="Ratio" LabelFormat="{}{0:P}" />- 转换整数数值为百分比:若数据是整数形式(如35代表35%),需先将数值转为0~1的小数,可通过自定义值转换器实现:
在XAML中引用转换器并绑定:public class IntToPercentConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return value is int intVal ? intVal / 100.0 : 0; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }<ContentPage.Resources> <local:IntToPercentConverter x:Key="IntToPercent" /> </ContentPage.Resources> <syncfusion:DoughnutSeries ItemsSource="{Binding ChartData}" XBindingPath="Category" YBindingPath="Value" YBindingPathConverter="{StaticResource IntToPercent}" LabelFormat="{}{0:P}" />- 自定义标签模板:若需要更灵活的标签样式(如同时显示类别和百分比),可使用
LabelTemplate:
<syncfusion:DoughnutSeries ItemsSource="{Binding ChartData}" XBindingPath="Category" YBindingPath="Ratio"> <syncfusion:DoughnutSeries.LabelTemplate> <DataTemplate> <StackLayout> <Label Text="{Binding Category}" /> <Label Text="{Binding Ratio, StringFormat='{0:P}'}" /> </StackLayout> </DataTemplate> </syncfusion:DoughnutSeries.LabelTemplate> </syncfusion:DoughnutSeries> - 直接格式化小数数值:若数据是0~1区间的小数(如0.35代表35%),直接设置
内容的提问来源于stack exchange,提问作者Cody
相关产品推荐
相关产品推荐

