Articles in this section

How to Customize an Individual Legend Item in .NET MAUI Pyramid Chart?

This article provides a detailed walkthrough on how to customize the legend item in a .NET MAUI Pyramid Chart using the LegendItemCreated event.

The SfPyramidChart provides a LegendItemCreated event, which is triggered when a chart legend item is created. This event is particularly useful for customizing the legend to match your application’s design.

The event argument contains the LegendItem object, which includes the following properties:

  • IconType – Set the type of icon for the legend.
  • IconHeight Get or set the icon height of the legend icon.
  • IconWidth – Get or set the icon width of the legend icon.
  • IconBrush – Change the color of the legend icon.
  • Text – Get or set the text of the label.
  • TextColor – Get or set the color of the label.
  • TextMargin – Get or set the margin size of labels.
  • FontSize – Get or set the font size for the legend label.
  • FontAttributes – Get or set the font style for the legend label.
  • FontFamily – Get or set the font family for the legend label.
  • IsToggled – Determine the toggle visibility of the legend.
  • DisableBrush – Get or set the legend icon and text disable color when toggled.
  • Index – Retrieve the index position of the legend.
  • Item – Get the data object for the associated segment.

Learn step-by-step instructions and gain insights to customize the legend items for each individual data item.

Step 1: Initialize the SfPyramidChart and populate it with data as follows.

XAML

<chart:SfPyramidChart ItemsSource="{Binding Data}"
                      XBindingPath="Category"
                      YBindingPath="Value">
   
</chart:SfPyramidChart> 

Step 2: Initialize the ChartLegend in SfPyramidChart and setting up the LegendItemCreated event.

XAML

<chart:SfPyramidChart.Legend>
     <chart:ChartLegend LegendItemCreated="OnLegendItemCreated"/>
</chart:SfPyramidChart.Legend> 

Step 3: Customize the legend item for each data point in the chart using the LegendItemCreated event handler method.

C#

private void OnLegendItemCreated(object sender, LegendItemEventArgs e)
{
   e.LegendItem.FontSize = 23;
   e.LegendItem.IconHeight = 17;
   e.LegendItem.IconWidth = 17;

   switch (e.LegendItem.Index)
   {
       case 0:
       SetLegendItem(e, ShapeType.Pentagon, FontAttributes.Bold, "#FF7272", "Arial");
       break;
       case 1:
       SetLegendItem(e, ShapeType.Triangle, FontAttributes.Italic, "#2196F3", "TimesNewRoman");
       break;
       case 2:
       SetLegendItem(e, ShapeType.Diamond, FontAttributes.Bold, "#F4890B", "Calibri");
       break;
       case 3:
       SetLegendItem(e, ShapeType.Hexagon, FontAttributes.Italic, "#9215F3", "Verdana");
       break;
       default:
       SetLegendItem(e, ShapeType.Circle, FontAttributes.None, "#FF0ADA23", "Georgia");
       break;
   }
}

private void SetLegendItem(LegendItemEventArgs e, ShapeType iconType, FontAttributes fontAttributes, string textColor, string fontFamily)
{
   e.LegendItem.IconType = iconType;
   e.LegendItem.FontAttributes = fontAttributes;
   e.LegendItem.TextColor = Color.FromArgb(textColor);
   e.LegendItem.FontFamily = fontFamily;
} 

Output:

Output demo of customized legend items in Pyramid Chart

Conclusion
I hope you enjoyed learning how to customize an individual legend item in .NET MAUI Pyramid Chart.
You can refer to our .NET MAUI Pyramid Chart feature tour page to learn about its other groundbreaking feature representations and documentation to understand how to present and manipulate data.

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

Please let us know in the comments section if you have any queries or require clarification. 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)
Access denied
Access denied