I have not found a property like LabelContent that can be set to Percentage which I believe is available in Xamarin? I also tried playing around with the LabelFormat property but there's no documentation showing examples that I've found online for .NET MAUI. The only mention of it being possible is at the bottom of this page but it doesn't show how it was done.
How to change the Data Label of Syncfusion's .NET MAUI SfCircularChart to display as a percentage of the slice size?
336 Views Asked by Cody At
2
There are 2 best solutions below
0
On
I ended up emailing Syncfusion directly and they informed me there is no built-in solution for this problem. However you can override the DrawDatalabel function to achieve the same result as seen here:
public class DoughnutSeriesExt : DoughnutSeries
{
protected override void DrawDataLabel(ICanvas canvas, Brush fillcolor, string label, PointF point, int index)
{
double totalValues = 0;
foreach (var item in this.ItemsSource as ObservableCollection<Model>)
{
totalValues += item.Count;
}
var currentDataPoint = Double.Parse(label.Replace("%", ""));
label = ValueToPercentage.Converter(currentDataPoint, totalValues);
base.DrawDataLabel(canvas, fillcolor, label, point, index);
}
}
internal static class ValueToPercentage
{
public static string Converter(double value, double totalValue)
{
return Math.Round(value / totalValue * 100, 2) + "%";
}
}
This can be achieved by setting
LabelFormat="0'%"of theChartDataLabelStylelike below:Below is the step you can refer to:
#1: Install the Syncfusion.Maui.Charts.
#2: Register the handler in
MauiProgram.cs:#3:
MainPage.xaml:
#4:
ViewModel.cs:
The Output: