Chart of the Week: Creating a .NET MAUI Horizontal Bar Chart to Compare Grocery Prices Between Two Countries
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: Creating a NET MAUI Horizontal Bar Chart to Compare Grocery Prices Between Two Countries

Chart of the Week: Creating a .NET MAUI Horizontal Bar Chart to Compare Grocery Prices Between Two Countries

Welcome to our Chart of the Week blog series!

Today, we’ll create a horizontal bar chart to visualize the price comparison of groceries using the Syncfusion .NET MAUI Cartesian Charts control. This control is supported on both desktop (Windows and Mac) and mobile platforms (Android and iOS).

In this blog post, we’ll compare the prices of groceries in the United States and the United Kingdom using a horizontal bar chart that resembles a pyramid graph. It will allow us to identify and compare the prices of various grocery items easily.

Let’s get started!

Step 1: Collecting grocery price data

Before creating the chart, let’s gather data on grocery prices in the United States and the United Kingdom.

Step 2: Preparing data for the chart

Create a Model class that includes properties for storing information about grocery items and their prices in the United States and the United Kingdom.

Refer to the following code example.

public class Model
{
   public string GroceryItem { get; set; }        
   public double USPrice { get; set; }        
   public double UKPrice { get; set; }
        
}

Then, configure the ViewModel class to create a collection of grocery item price details and store it in an observable collection using the GroceryPriceDetails property.

Refer to the following code example.

public class ViewModel
{
   public ObservableCollection<Model> GroceryPriceDetails { get; set; }

   public ViewModel()
   {
      GroceryPriceDetails = new ObservableCollection<Model>();
      GroceryPriceDetails.Add(new Model() { GroceryItems = "Milk (1 L)", USPrice = 1.03, UKPrice = 1.05 });
      GroceryPriceDetails.Add(new Model() { GroceryItems = "White Bread (500 g)", USPrice = 3.54, UKPrice = 1.08 });
      GroceryPriceDetails.Add(new Model() { GroceryItems = "White Rice (1 kg)", USPrice = 4.56, UKPrice = 1.42 });
      GroceryPriceDetails.Add(new Model() { GroceryItems = "Eggs (12)", USPrice = 4.40, UKPrice = 2.27 });
      GroceryPriceDetails.Add(new Model() { GroceryItems = "Chicken Fillets(1 kg)", USPrice = 12.00, UKPrice = 5.89 }); ;
      GroceryPriceDetails.Add(new Model() { GroceryItems = "Cheese (1 kg)", USPrice = 12.87, UKPrice = 6.13 });
      GroceryPriceDetails.Add(new Model() { GroceryItems = "Beef Round (1 kg)", USPrice = 15.21, UKPrice = 9.15 });

    }   
 }

Step 3: Layout definition

Define the layout using a Border element. Inside the border, we utilize a Grid to place our content.

Refer to the following code example.

<Border StrokeShape="RoundRectangle 20" 
         StrokeThickness="4"
         Stroke="Gray"
         Margin="20">
 <Grid ColumnSpacing="0">
  <Grid.ColumnDefinitions>
   <ColumnDefinition Width="*"/>
   <ColumnDefinition Width="*"/>
  </Grid.ColumnDefinitions>

  <Grid.RowDefinitions>
   <RowDefinition Height="*"/>
  </Grid.RowDefinitions>
 </Grid>
</Border>

Step 4: Configuring the Syncfusion .NET MAUI Cartesian Charts

Let’s configure the Syncfusion .NET MAUI Cartesian Charts control using this documentation.

Refer to the following code example.

<Grid >

 <!--US Chart -->
 <chart:SfCartesianChart Grid.Row="1" Grid.Column="0">          
  <chart:SfCartesianChart.XAxes>
   <Chart:CategoryAxis />
  </chart:SfCartesianChart.XAxes>

  <chart:SfCartesianChart.YAxes>
   <Chart:NumericalAxis />
  </chart:SfCartesianChart.YAxes>
 </chart:SfCartesianChart>

 <!--UK Chart -->
 <chart:SfCartesianChart Grid.Column="1" Grid.Row="1">
  <chart:SfCartesianChart.XAxes>
   <Chart:CategoryAxis />
  </chart:SfCartesianChart.XAxes>

  <chart:SfCartesianChart.YAxes>
   <Chart:NumericalAxis />
  </chart:SfCartesianChart.YAxes>
 </chart:SfCartesianChart>

</Grid>

Now that we’ve configured the Syncfusion .NET MAUI Cartesian Charts control, let’s see how to create a horizontal bar chart that resembles a pyramid with it!

Step 5: Initialize the horizontal bar chart

To compare the grocery item prices, we’ll use the ColumnSeries instance and set the IsTransposed property to true to initialize the horizontal bar chart.

<!--US Grocery Items Price Chart -->
<chart:SfCartesianChart IsTransposed="True" >
 ..
 <Chart:ColumnSeries />
</chart:SfCartesianChart>

<!--UK Grocery Items Price Chart -->
<chart:SfCartesianChart IsTransposed="True" >
 …
 <Chart:ColumnSeries />
</chart:SfCartesianChart>

Step 6: Binding data to the bar chart

This step involves using the ColumnSeries instance to bind the grocery items’ price data.

Refer to the following code example.

<!--US Grocery Items Price Chart -->
<chart:SfCartesianChart IsTransposed="True" >
 …
 <chart:ColumnSeries ItemsSource="{Binding GroceryPriceDetails}" 
                     XBindingPath="GroceryItems"
                     YBindingPath="USPrice" />
</chart:SfCartesianChart>

<!--UK Grocery Items Price Chart -->
<chart:SfCartesianChart IsTransposed="True" >
 …
 <chart:ColumnSeries ItemsSource="{Binding GroceryPriceDetails}" 
                     XBindingPath="GroceryItems" 
                     YBindingPath="UKPrice"/>
</chart:SfCartesianChart>

In the previous code, we’ve bound the ItemSource property with the GroceryPriceDetails property. The XBindingPath is bound with the grocery items count, and the YBindingPath is bound with the price details.

Step 7: Initializing the legend

A legend displays information corresponding to the chart series. Let’s implement a legend in our bar chart, as shown in the following code example.

<chart:SfCartesianChart.Legend>
 <Chart:ChartLegend/>
</chart:SfCartesianChart.Legend>

Step 8: Customizing the chart appearance

We can customize the appearance of the horizontal bar chart using various properties and styles.

Customizing the title

Refer to the following code example, which customizes the chart title using the Label property. In it, we’ve defined the title’s Text, FontSize, FontAttributes, and other properties.

<Label Grid.Row="0"
       Grid.ColumnSpan="2"
       Text="Creating a Horizontal Bar Chart to Compare Grocery Item Prices of Two Countries"
       TextColor="Black" 
       FontSize="16"
       FontFamily="TimeSpan"
       FontAttributes="Bold"
       HorizontalOptions="Start"
       Padding="350,5,0,0"/>

Customizing the axes

Let’s customize the x- and y-axes using properties such as LabelStyle, AxisLineStyle, and MajorTickStyle.

<!--US Chart Axis Customization -->
 <chart:SfCartesianChart.XAxes>
  <chart:CategoryAxis IsInversed="True"
                      ShowMajorGridLines="False"
                      LabelCreated="CategoryAxis_LabelCreated">
   <Chart:CategoryAxis.LabelStyle>
    <chart:ChartAxisLabelStyle TextColor="#49454F" 
                               FontSize="12"
                               LabelAlignment="Center"/>
   </Chart:CategoryAxis.LabelStyle>
   <Chart:CategoryAxis.AxisLineStyle>
    <chart:ChartLineStyle StrokeWidth ="0"/>
   </Chart:CategoryAxis.AxisLineStyle>
   <Chart:CategoryAxis.MajorTickStyle>
    <chart:ChartAxisTickStyle Stroke="#C5C8CE"/>
   </Chart:CategoryAxis.MajorTickStyle>
  </Chart:CategoryAxis>
 </chart:SfCartesianChart.XAxes>

 <chart:SfCartesianChart.YAxes>
  <chart:NumericalAxis IsInversed="True" 
                       ShowMajorGridLines="False"
                       Maximum="18"
                       IsVisible="False">
  </Chart:NumericalAxis>
 </chart:SfCartesianChart.YAxes>

<!--UK Chart Axis Customization -->
 <chart:SfCartesianChart.XAxes>
  <chart:CategoryAxis ShowMajorGridLines="False"
                      IsInversed="True">
   <Chart:CategoryAxis.LabelStyle>
    <chart:ChartAxisLabelStyle TextColor="Transparent"
                               FontSize="1"
                               Margin="-5"/>
   </Chart:CategoryAxis.LabelStyle>
   <Chart:CategoryAxis.AxisLineStyle>
    <chart:ChartLineStyle StrokeWidth ="0"/>
   </Chart:CategoryAxis.AxisLineStyle>
   <Chart:CategoryAxis.MajorTickStyle>
    <chart:ChartAxisTickStyle Stroke="#C5C8CE"/>
   </Chart:CategoryAxis.MajorTickStyle>
  </Chart:CategoryAxis>
 </chart:SfCartesianChart.XAxes>

 <chart:SfCartesianChart.YAxes>
  <chart:NumericalAxis IsVisible="False"
                       ShowMajorGridLines="False"
                       Maximum="10">
  </Chart:NumericalAxis>
 </chart:SfCartesianChart.YAxes>

Customizing the series data labels

Add the chart’s data label and customize the label formats as shown in the following code example.  

<!--US Chart Axis Customization -->
<chart:ColumnSeries ShowDataLabels="True"
                    Label="United States (US)">
 <Chart:ColumnSeries.DataLabelSettings>
  <chart:CartesianDataLabelSettings LabelPlacement="Outer">
   <chart:CartesianDataLabelSettings.LabelStyle>
    <chart:ChartDataLabelStyle LabelFormat="0.00'$" Background="White"/>
   </chart:CartesianDataLabelSettings.LabelStyle>
  </chart:CartesianDataLabelSettings>
 </Chart:ColumnSeries.DataLabelSettings>
</Chart:ColumnSeries>

<!--UK Chart Axis Customization -->
<chart:ColumnSeries ShowDataLabels="True"
                    Label="United Kingdom (UK)">
 <Chart:ColumnSeries.DataLabelSettings>
  <chart:CartesianDataLabelSettings LabelPlacement="Outer">
   <chart:CartesianDataLabelSettings.LabelStyle>
    <chart:ChartDataLabelStyle LabelForm”t="0.00’£" Background="White"/>
   </chart:CartesianDataLabelSettings.LabelStyle>
  </chart:CartesianDataLabelSettings>
 </Chart:ColumnSeries.DataLabelSettings>
</Chart:ColumnSeries>

Customizing the series appearance

We also apply the color for each series using the Fill property. Refer to the following example.

<!--US Series-->
<chart:ColumnSeries Fill="#CD6688">
</Chart:ColumnSeries>

<!--UK Series-->
<chart:ColumnSeries Fill="#AED8CC">
</Chart:ColumnSeries>

After executing the previous code examples, our output will look like the following image.

Visualizing the Price Comparison of Grocery Items Using a .NET MAUI Horizontal Bar Chart
Visualizing the Price Comparison of Grocery Items Using a .NET MAUI Horizontal Bar Chart

GitHub reference

For more information, refer to the project on GitHub.

Conclusion

Thanks for reading! This blog showed how to create a horizontal bar chart using the Syncfusion .NET MAUI Cartesian Charts control to visualize a price comparison of grocery items in the United States and the United Kingdom. We encourage you to follow the steps provided and share your thoughts on the experience in the comment section below.

You can contact us through our support forumsupport portal, or feedback portal. We are always happy to assist you!

Test Flight
App Center Badge
Google Play Store Badge
Microsoft Badge
Github Store Badge

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