Chart of the Week: Create a .NET MAUI Stacked Column Chart for the Top-Earning Female Athletes
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Chart of the Week Create a .NET MAUI Stacked Column Chart to Visualize the Top-Earning Female Athletes of 2023

Chart of the Week: Create a .NET MAUI Stacked Column Chart for the Top-Earning Female Athletes

Welcome to our Chart of the Week blog series!

In this week’s edition, we are visualizing the highest-paid female athletes in 2023 using the Syncfusion .NET MAUI stacked column chart. In professional sports, female athletes are making significant financial strides both on and off the field. Let’s create some visual proof of that.

Refer to the following image.Visualizing the top-earning female athletes’ data using .NET MAUI stacked column chart

Let’s get started!

Step 1: Gathering the data

First, let’s gather the data for the highest-paid female athletes in 2023.

Step 2: Preparing the data for the chart

Create the FemaleAthletesModel class with AthleteName, OnFieldEarning, OffFieldEarning, and Country properties to store the athletes’ data.

public class FemaleAthletesModel
{
    public string AthleteName { get; set; } = string.Empty;

    public double OnFieldEarning { get; set; }

    public double OffFieldEarning { get; set; }

    public string Country { get; set; } = string. Empty;
}

Next, configure the TopPaidFemaleAthleteInfo class to create a collection of athletes’ data along with their earnings on and off the field and store it in an observable collection using the AthletesData property. Then, create a dictionary to store the streams of each athlete’s country flag image using the Streams property.

public class TopPaidFemaleAthleteInfo
{
    public ObservableCollection<FemaleAthletesModel> AthletesData { get; set; }

    public Dictionary<string, Stream> Streams { get; } = new Dictionary<string, Stream>();

    Stream? stream;
    public TopPaidFemaleAthleteInfo()
    {
        AthletesData = new ObservableCollection<FemaleAthletesModel>()
        {
            new FemaleAthletesModel() {AthleteName = "Leyla Fernandez", OnFieldEarning = 1.8, OffFieldEarning = 7,Country="canada"},
            new FemaleAthletesModel() {AthleteName = "Elena Rybakina", OnFieldEarning = 5.5, OffFieldEarning = 4,Country="kazakhstan"},
            new FemaleAthletesModel() {AthleteName = "Venus Williams", OnFieldEarning = 0.2, OffFieldEarning = 12,Country="united_states"},
            new FemaleAthletesModel() {AthleteName = "Jessica Pegula", OnFieldEarning = 6, OffFieldEarning = 6.5,Country="united_states"},
            new FemaleAthletesModel() {AthleteName = "Aryna Sabalenka", OnFieldEarning = 8.2, OffFieldEarning = 6.5,Country="belarus"},
            new FemaleAthletesModel() {AthleteName = "Naomi Osaka", OnFieldEarning = 0, OffFieldEarning = 15,Country="japan"},
            new FemaleAthletesModel() {AthleteName = "Emma Raducanu", OnFieldEarning = 0.2, OffFieldEarning = 15,Country="united_kingdom"},
            new FemaleAthletesModel() {AthleteName = "Coco Gauff", OnFieldEarning = 6.7,OffFieldEarning = 15,Country="united_states"},
            new FemaleAthletesModel() {AthleteName = "Eileen Gu",OffFieldEarning=22,OnFieldEarning=0.1,Country="china"},
            new FemaleAthletesModel() {AthleteName = "Iga Swiatek",OnFieldEarning=9.9,OffFieldEarning=14,Country="poland"},
        };

        Streams = GetImageSources();
    }

    private Dictionary<string, Stream> GetImageSources()
    {
        Dictionary<string, Stream> keyValues = new Dictionary<string, Stream>();

        Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;

        for (int i = 0; i < AthletesData.Count; i++)
        {
            string imageName = $"{AthletesData[i].Country}.png";
            stream = assembly.GetManifestResourceStream($"HighestPaidFemaleAthletes.Resources.Images.{imageName}");

            if (stream != null && AthletesData[i].AthleteName != null)
            {
                keyValues.Add(AthletesData[i].AthleteName.ToString(), stream);
            }
        }
        return keyValues;
    }
}

Step 3: Configuring the Syncfusion .NET MAUI Charts control

Let’s configure the Charts control using this documentation.

<chart:SfCartesianChart>                            
 <chart:SfCartesianChart.Xaxes>
  <chart:CategoryAxis />
 </chart:SfCartesianChart.Xaxes>
 <chart:SfCartesianChart.Yaxes>
  <chart:NumericalAxis/>
 </chart:SfCartesianChart.Yaxes>                       
</chart:SfCartesianChart>

Step 4: Transposing the chart

In a column chart, the bars are oriented vertically, whereas in a bar chart, they are oriented horizontally. We are going to visualize our data in a horizontal orientation. So, we need to transpose the chart by enabling the IsTransposed property. With this, we can use it as a stacked bar chart.

<chart:SfCartesianChart IsTransposed="True">                            
 <chart:SfCartesianChart.XAxes>
  <chart:CategoryAxis />
 </chart:SfCartesianChart.XAxes>
 <chart:SfCartesianChart.YAxes>
  <chart:NumericalAxis/>
 </chart:SfCartesianChart.YAxes>                       
</chart:SfCartesianChart>

Step 5: Bind the data to the chart

Bind the athletes’ income data to our StackedColumnSeries.

<!--On-Field Earning Series-->
 <chart:StackingColumnSeries ItemsSource="{Binding AthletesData}" XBindingPath="AthleteName" YBindingPath="OnFieldEarning">
</chart:StackingColumnSeries>

<!--Off-Field Earning Series-->
 <chart:StackingColumnSeries ItemsSource="{Binding AthletesData}" XBindingPath="AthleteName" YBindingPath="OffFieldEarning">
</chart:StackingColumnSeries>

In the previous example, we used the ItemsSource property to bind the stacked column chart with the AthletesData data collection, which contains the information on the on- and off-field earnings of the top-paid female athletes in 2023. Additionally, we set the XBindingPath with the AthleteName property and YBindingPath with the OnFieldEarning and OffFieldEarning properties in their series.

Step 6: Customizing the axis

We’ll use the category axis to visualize the athletes with their countries. To do so, we need to customize the category axis. Therefore, in the code behind, extend the CategoryAxisExt class by inheriting the CategoryAxis class.

Next, override the DrawAxis() method to achieve the drawing of country flag images with the axis label. Get the images from the resource Stream, then draw the image in the labels position.

public class CategoryAxisExt : CategoryAxis
{
     protected override void DrawAxis(ICanvas canvas, Rect arrangeRect)
     {
         base.DrawAxis(canvas, arrangeRect);

         foreach (ChartAxisLabel label in VisibleLabels)
         {
             string? labelText = label.Content.ToString();

             if (this.BindingContext is TopPaidFemaleAthleteInfo viewModel && labelText != null && viewModel.Streams.ContainsKey(labelText))
             {
                 Stream stream = viewModel.Streams[labelText];
                 var image = PlatformImage.FromStream(stream);
                 var top = ValueToPoint(label.Position); // Assuming positions start from 0
                 canvas.DrawImage(image, (float)arrangeRect.Left + xOffset, top - yOffset, 25, 25);
             }
         }
     }
 }

Add the CategoryAxisExt class instance to the XAxes collection in the SfCartesianChart.

<chart:SfCartesianChart>                            
 <chart:SfCartesianChart.XAxes>
  <local:CategoryAxisExt />
 </chart:SfCartesianChart.XAxes>
 <chart:SfCartesianChart.YAxes>
  <chart:NumericalAxis/>
 </chart:SfCartesianChart.YAxes>                       
</chart:SfCartesianChart>

Step 7: Customizing the chart appearance

We can customize the chart’s appearance by changing the chart title, axis elements, series, and more to enhance the readability of the data.

Customizing the chart title

Refer to the following code example for how to add a title and description in the chart for the plotted data.

<chart:SfCartesianChart.Title>
 <Grid>
  <Grid.ColumnDefinitions>
   <ColumnDefinition Width="1*"/>
   <ColumnDefinition Width="{OnPlatform Android=49*,WinUI=59*,MacCatalyst=59*,iOS=49*}"/>
  </Grid.ColumnDefinitions>
  <VerticalStackLayout Background="#E6FF0088 " Margin="10,5,0,15" Grid.Column="0" Grid.RowSpan="2"/>
   <VerticalStackLayout Grid.Column="1" Margin="5">
    <Label Text="Top Female Athletes in the World by Income, 2023" FontSize="Subtitle" FontFamily="centurygothic" Padding="0,0,5,5" HorizontalTextAlignment="Start" FontAttributes="Bold" TextColor="White"/>
    <Label Text="The highest paid female athletes in terms of total earnings in 2023" HorizontalTextAlignment="Start" FontSize="{OnPlatform Android=10,WinUI=10,MacCatalyst=16,iOS=11}" FontFamily="centurygothic" TextColor="White" Padding="0,0,0,10"/>
   </VerticalStackLayout>
 </Grid>
</chart:SfCartesianChart.Title>

Customizing the legend

Then, customize the chart legend using the ItemTemplate property and place it at the bottom using the Placement property.

<chart:SfCartesianChart.Legend>
 <chart:ChartLegend Placement="Bottom">
  <chart:ChartLegend.ItemTemplate>
   <DataTemplate>
    <StackLayout Orientation="Horizontal" Margin="5">
     <Rectangle HeightRequest="15" WidthRequest="15" Margin="3" Background="{Binding IconBrush}"/>
     <Label Text="{Binding Text}" Margin="3" TextColor="White" FontSize="12"/>
    </StackLayout>
   </DataTemplate>
  </chart:ChartLegend.ItemTemplate>
 </chart:ChartLegend>
</chart:SfCartesianChart.Legend>

Customizing the chart axis

Customize the x- and y-axes with the Maximum, Interval, LabelPlacement EdgeLabelsDrawingMode and ShowMajorGridLines properties, as well as the AxisTitle, LabelStyle, LineStyle and TickStyle.
Refer to the following code example.

<chart:SfCartesianChart.XAxes>
 <local:CategoryAxisExt ShowMajorGridLines="False" LabelPlacement="BetweenTicks" EdgeLabelsDrawingMode="{OnPlatform Android=Center,iOS= Center}" AutoScrollingMode="{OnPlatform Android=Start,iOS=Start}" AutoScrollingDelta="{OnPlatform Android=5,iOS=5}">
  <local:CategoryAxisExt.AxisLineStyle>
   <chart:ChartLineStyle Stroke="Black" StrokeWidth="0"/>
  </local:CategoryAxisExt.AxisLineStyle>
  <local:CategoryAxisExt.MajorTickStyle>
   <chart:ChartAxisTickStyle StrokeWidth="0"/>
  </local:CategoryAxisExt.MajorTickStyle>
  <local:CategoryAxisExt.LabelStyle>
   <chart:ChartAxisLabelStyle TextColor="white" FontAttributes="Bold" LabelAlignment="Center" Margin="{OnPlatform WinUI='40,4,4,4',Android='20,4,4,4'}" FontSize="10"/>
  </local:CategoryAxisExt.LabelStyle>
 </local:CategoryAxisExt>
</chart:SfCartesianChart.XAxes>

<chart:SfCartesianChart.YAxes>
 <chart:NumericalAxis Interval="5" Maximum="26" EdgeLabelsDrawingMode="Center"  ShowMajorGridLines="False">
  <chart:NumericalAxis.AxisLineStyle>
   <chart:ChartLineStyle StrokeWidth="0"/>
  </chart:NumericalAxis.AxisLineStyle>
  <chart:NumericalAxis.MajorTickStyle>
   <chart:ChartAxisTickStyle StrokeWidth="0"/>
  </chart:NumericalAxis.MajorTickStyle>
  <chart:NumericalAxis.LabelStyle>
   <chart:ChartAxisLabelStyle LabelFormat="$0M" TextColor="White" FontAttributes="Bold" FontSize="10"/>
  </chart:NumericalAxis.LabelStyle>
 </chart:NumericalAxis>
</chart:SfCartesianChart.YAxes>

Customize the series appearance

Finally, customize the chart series by applying the Fill color and displaying the athlete information using DataLabels.

<!--On-Field Earning Series-->
<chart:StackingColumnSeries ItemsSource="{Binding AthletesData}" XBindingPath="AthleteName" YBindingPath="OnFieldEarning" Fill="#E6FF0088 " Label="On-Field Earnings" ShowDataLabels="True">
 <chart:StackingColumnSeries.DataLabelSettings>
  <chart:CartesianDataLabelSettings LabelPlacement="Outer">
   <chart:CartesianDataLabelSettings.LabelStyle>
    <chart:ChartDataLabelStyle FontAttributes="Bold" LabelFormat="$0.#M"/>
   </chart:CartesianDataLabelSettings.LabelStyle>
  </chart:CartesianDataLabelSettings>
 </chart:StackingColumnSeries.DataLabelSettings>
</chart:StackingColumnSeries>

<!--Off-Filed Earning Series-->
<chart:StackingColumnSeries ItemsSource="{Binding AthletesData}" XBindingPath="AthleteName" YBindingPath="OffFieldEarning" Fill="#E640ACFF " Label="Off-Field Earnings" ShowDataLabels="True" >
 <chart:StackingColumnSeries.DataLabelSettings>
  <chart:CartesianDataLabelSettings LabelPlacement="Outer">
   <chart:CartesianDataLabelSettings.LabelStyle>
    <chart:ChartDataLabelStyle FontAttributes="Bold" LabelFormat="$0.#M"/>
   </chart:CartesianDataLabelSettings.LabelStyle>
  </chart:CartesianDataLabelSettings>
 </chart:StackingColumnSeries.DataLabelSettings>
</chart:StackingColumnSeries>

After executing the previous code examples, we will get output like in the following image.

Visualizing the top-earning female athletes’ data using Syncfusion .NET MAUI stacked column chart
Visualizing the top-earning female athletes’ data using Syncfusion .NET MAUI stacked column chart

GitHub reference

For more details, refer to the project on GitHub.

Conclusion

Thanks for reading! In this blog, we’ve seen how to visualize the top-earning female athletes’ data using the Syncfusion MAUI stacked column chart. We encourage you to follow the steps discussed in this blog and share your thoughts in the comments below.

If you require any assistance, you can contact us through our support forums, support portal, or feedback portal. We are always happy to assist you.

See you in our next blog!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed