Articles in this section
Category / Section

How to customize the individual legend item based on a condition in Xamarin.Forms Chart?

1 min read

You can customize all the legend items by setting individual style using the LegendItemCreated event. This event will be fired when a Xamarin Chart legend item is created. For more details about the event arguments, refer to this documentation.

The following code sample demonstrates how to set individual style to all the legend items and you can download the complete sample here.

XAML:

<chart:SfChart x:Name="chart" LegendItemCreated="Chart_LegendItemCreated">

C#:

chart.LegendItemCreated += Chart_LegendItemCreated;
 
private void Chart_LegendItemCreated(object sender, ChartLegendItemCreatedEventArgs e)
{
   Model model = e.LegendItem.DataPoint as Model;
   e.LegendItem.Label = model.XValue + ": " + model.YValue.ToString();
   e.LegendItem.LabelStyle = new ChartLegendLabelStyle()
   {
        TextColor = model.YValue > 50 ? Color.Green : Color.Red,
        FontFamily = model.YValue > 50 ? "Times New Roman" : "Arial"
    };
}

Output:

Pie Chart with customized legend items in Xamarin.Forms

 

Conclusion

I hope you enjoyed learning about how to customize the individual legend item based on a condition in Xamarin.Forms Chart.

You can refer to our Xamarin Chart’s feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin Chart Documentation to understand how to present and manipulate data.

For current customers, you can check out our Xamarin components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our Xamarin Chart and other Xamarin components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied