Chart of the Week: Create a .NET MAUI Column Chart to Visualize which Milk Is the Most Sustainable
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)
Create a .NET MAUI Column Chart to Visualize which Milk Is the Most Sustainable

Chart of the Week: Create a .NET MAUI Column Chart to Visualize which Milk Is the Most Sustainable

Welcome to the Chart of the Week blog series!

Today, we will explore the statistics of the most sustainable kinds of milk using the Syncfusion .NET MAUI Column Chart.

The .NET MAUI Column Chart, a vertical bar chart, uses vertical bars (called columns) to display values for one or more items. The column chart is likely the most common chart type being used. Points from the series are drawn as columns next to each other. The chart supports zooming, scrolling, tooltips, and selection.

Exploring environmental impact

Plant-based milks like soy, rice, oat, and almond are increasingly popular due to concerns about animal welfare, dietary restrictions, and climate impact. So, determining their environmental friendliness compared to dairy milk is important. Generally, nondairy options surpass cow’s milk in carbon emissions, land use, and water consumption.

The processes involved in creating and shipping almond, oat, soy, and rice milk emit significantly less carbon than dairy milk. Almond milk is the lowest at 0.7kg per liter, followed by oat (0.9kg), soy (1kg), and rice (1.2kg). Plant-based options also require less land, ranging from 0.3 to 0.8 square meters per liter, compared to nine square meters for cow milk.

Contrary to popular belief, the production of almond milk uses less water than dairy, with 371 liters of water per liter of milk compared to 628 liters of water for dairy. Rice milk needs 270 liters of water, while soy and oats are even more efficient, needing only 28 and 48 liters, respectively.

Charting overall environmental impact

Notably, the production of alternative milks like soy and oat has minimal environmental impact compared to dairy, with factors like packaging and transportation becoming significant contributors to their ecological footprint. This comprehensive analysis underscores the importance of considering the entire life cycle of milk production.

The following chart visually presents the data discussed, providing a clear comparison of sustainability numbers between dairy and alternative milk.

Visualizing the most sustainable milk data using .NET MAUI Column ChartNow, let’s explore the steps involved in showcasing these details using the Syncfusion .NET MAUI Column Chart.

Step 1: Gather data for milk

Before proceeding, we need to gather data on the different kinds of milk from the BBC Report for our column chart.

Step 2: Populate the data for the chart

To illustrate the sustainability of different milk types based on distinct factors, we will create a class named MilkSourceModel. This class will encompass the following attributes:

  • Source: Signifying the various plant types utilized in milk production.
  • WaterQuantity: Storing data on the volume of water consumed in milk production.
  • CO2Emission: Storing data on carbon emissions produced during milk production.

Refer to the next code example for further clarification.

public class MilkSourceModel
{
       public string Source { get; set; }
       public double WaterQuantity  { get; set; }
       public double CO2Emission  { get; set; }

       public MilkSourceModel(string source, double waterQuantity, double cO2Emission)
       {
           Source = source;
           WaterQuantity = waterQuantity;
           CO2Emission = cO2Emission;
       }
}

Next, we’ll generate the data collection illustrating the milk production details using the MilkSourceData class with the Data property.

Refer to the following code example.

public class MilkSourceData : INotifyPropertyChanged
{
     public List<MilkSourceModel> Data { get; set; }

     public MilkSourceData()
     {
          Data = new List<MilkSourceModel>
          {
                new MilkSourceModel("Cow", 628, 3.2),
                new MilkSourceModel("Almond", 371, 0.7),
                new MilkSourceModel("Rice", 270, 1.2),
                new MilkSourceModel("Oat", 48, 0.9),
                new MilkSourceModel("Soy", 28, 1.0),
          };
         
     }
}

Step 3: Configure 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.

<chart:SfCartesianChart>

   <chart:SfCartesianChart.XAxes>
       <chart:CategoryAxis>
       </chart:CategoryAxis>
   </chart:SfCartesianChart.XAxes>

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

</chart:SfCartesianChart>

Step 4: Data binding for column chart visualization

Next, we create a column chart to represent the various types of milk. For this purpose, initialize the column chart in the code-behind and segment it as necessary.

Refer to the following code example.

public class ColumnSeriesExt : ColumnSeries
{

}

public class ColumnSegmentExt : ColumnSegment
{
        
}

To bind the data to the chart, use a customized column instance. Bind the Data collection to the ItemSource property. The XBindingPath and YBindingPath properties should be associated with the Source and WaterQuantity properties, respectively.

<model:ColumnSeriesExt ItemsSource="{Binding Data}" XBindingPath="Source" YBindingPath="WaterQuantity">

</model:ColumnSeriesExt>

Step 5: Customize the column chart

Create an extension of the standard ColumnSeries with the ColumnSeriesExt class. Within this class, override the CreateSegment method. This is a crucial step that enables the creation of a distinct ColumnSegmentExt instance, allowing the chart to display specialized segments.

public class ColumnSeriesExt : ColumnSeries
{
     protected override ChartSegment CreateSegment()
     {
         return new ColumnSegmentExt();
     }
}

Next, implement the ColumnSegmentExt class as a derivative of ColumnSegment. This class contains the drawing logic for the custom column shape, resulting in a visually appealing and personalized column chart.

For a detailed example, please refer to the following code example.

public class ColumnSegmentExt : ColumnSegment
{
    protected override void Draw(ICanvas canvas)
    {
        if (Series is CartesianSeries series && series.ActualYAxis is NumericalAxis yAxis)
        {
            var top = yAxis.ValueToPoint(Convert.ToDouble(yAxis.Maximum ?? double.NaN));
            var trackRect = new RectF() { Left = Left, Top = (float)top, Right = Right, Bottom = Bottom };
            DrawTrackPath(canvas, trackRect);
            canvas.SaveState();
            DrawColumn(canvas);
            canvas.RestoreState();
        }
    }

    private void DrawColumn(ICanvas canvas)
    {
        var path = new PathF();

        #if ANDROID || IOS
        path.MoveTo(Left + 20, Bottom);
        path.LineTo(Right - 20, Bottom);
        path.LineTo(Right - 20, Top);
        path.LineTo(Left + 20, Top);
        path.Close();
        
#elif WINDOWS || MACCATALYST path.MoveTo(Left + 10, Bottom); path.LineTo(Right - 10, Bottom); path.LineTo(Right - 10, Top); path.LineTo(Left + 10, Top); path.Close();
#endif var color = (Fill is SolidColorBrush brush) ? brush.Color : Colors.Transparent; canvas.FillColor = color; canvas.FillPath(path); } private void DrawTrackPath(ICanvas canvas, RectF trackRect) { var path = new PathF(); #if ANDROID || IOS path.MoveTo(Left + 20, Bottom); path.LineTo(Right - 20, Bottom); path.LineTo(Right - 20, trackRect.Top + 10); path.LineTo(Right - 40, trackRect.Top + 5); path.LineTo(Right - 40, trackRect.Top); path.LineTo(Left + 40, trackRect.Top); path.LineTo(Left + 40, trackRect.Top + 5); path.LineTo(Left + 20, trackRect.Top + 10); path.Close();
#elif WINDOWS || MACCATALYST path.MoveTo(Left + 10, Bottom); path.LineTo(Right - 10, Bottom); path.LineTo(Right - 10, trackRect.Top + 30); path.LineTo(Right - 30, trackRect.Top + 15); path.LineTo(Right - 30, trackRect.Top); path.LineTo(Left + 30, trackRect.Top); path.LineTo(Left + 30, trackRect.Top + 15); path.LineTo(Left + 10, trackRect.Top + 30); path.Close();
#endif canvas.FillColor = Color.FromRgb(243, 241, 255); canvas.FillPath(path); } }

Step 6: Customize the chart appearance

Let’s enhance the .NET MAUI column chart by customizing its properties and style to improve the data readability.

Customizing the chart title

You can customize the chart title to make the chart more visually appealing and informative like in the following code example.

<chart:SfCartesianChart.Title>
 <Grid>
  <Grid.ColumnDefinitions>
   <ColumnDefinition Width="2*" />
   <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <Grid Margin="0,0,0,50" Grid.Column="0">
   <Grid.ColumnDefinitions>
    <ColumnDefinition Width="13"/>
    <ColumnDefinition Width="*"/>
   </Grid.ColumnDefinitions>
   <StackLayout Grid.RowSpan="2" Margin="0,5,0,0" Orientation="Vertical" BackgroundColor="#9BD241" />
   <StackLayout Grid.Column="1" Margin="5,0,0,0" HorizontalOptions="Start" Orientation="Vertical">
    <Label Text="Which Milk Is the Most Sustainable?" FontSize="Large" FontAttributes="Bold" TextColor="#666666" FontFamily="AntaRegular"/>
    <Label Text="Based on quantity of water used and CO₂ emitted per liter of milk" FontSize="{OnPlatform Android=14,iOS=14, Default=17}" TextColor="Gray" Margin="0,2,0,0" FontFamily="AntaRegular"/>
   </StackLayout>
  </Grid>
                    
  <VerticalStackLayout HorizontalOptions="End" Margin="40,0,0,0" Grid.Column="1">
   <Label Text="CO₂ Emission (in kg)" FontSize="16" FontFamily="AntaRegular" HorizontalOptions="Center" Margin="0,0,0,5"/>

   <Frame BorderColor="#031257" CornerRadius="5" Background="#0DC3ECB2"  WidthRequest="{OnPlatform Android=165,iOS=165,Default=180}" Padding="5">
    <Grid>
     <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
      <ColumnDefinition/>
     </Grid.ColumnDefinitions>
     <Image Grid.Row="1" Grid.Column="0" Source="{Binding SourceImage}" Margin="10,0,0,0" HeightRequest="{OnPlatform Android=50,iOS=50, Default=50}"
WidthRequest="{OnPlatform Android=50,iOS=50, Default=50}"/>
     <Image Grid.Row="1" Grid.Column="1" Source="right.png" Margin="15,0,0,0" HeightRequest="50" WidthRequest="40" VerticalOptions="Center"/>
     <Label Grid.Row="1" Grid.Column="2" Text="{Binding EmissionText}" FontFamily="AntaRegular" FontSize="18" Margin="-20,0,-25,0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
     </Grid>
    </Frame>
  </VerticalStackLayout>
 </Grid>
</chart:SfCartesianChart.Title>

Customizing the chart axis

You can customize the chart axis using the following properties:

  • ShowMajorGridLines: Adjusts the visibility of the major grid lines.
  • LabelStyle: Modifies the style of the axis labels.
  • AxisLineStyle: Alters the style of the axis line.
  • MajorTickStyle: Changes the style of the major tick lines.
  • Title: Customizes the appearance of the axis title.

Refer to the following code example.

<chart:SfCartesianChart.XAxes>
 <chart:CategoryAxis ShowMajorGridLines="False">
 <chart:CategoryAxis.LabelStyle>
   <chart:ChartAxisLabelStyle FontFamily="AntaRegular" TextColor="Black" FontSize="14"/>
 </chart:CategoryAxis.LabelStyle>

 <chart:CategoryAxis.AxisLineStyle>
   <chart:ChartLineStyle StrokeWidth ="0"/>
 </chart:CategoryAxis.AxisLineStyle>

  <chart:CategoryAxis.MajorTickStyle>
   <chart:ChartAxisTickStyle StrokeWidth ="0"/>
  </chart:CategoryAxis.MajorTickStyle>
 </chart:CategoryAxis>
</chart:SfCartesianChart.XAxes>

<chart:SfCartesianChart.YAxes>
 <chart:NumericalAxis ShowMajorGridLines="False" Maximum="800">
  <chart:NumericalAxis.Title>
   <chart:ChartAxisTitle Text="Water use (liters)" FontSize="20" TextColor="Black" FontFamily="AntaRegular" Margin="0,-50,0,0"/>
  </chart:NumericalAxis.Title>

  <chart:NumericalAxis.LabelStyle>
   <chart:ChartAxisLabelStyle TextColor="Transparent"/>
  </chart:NumericalAxis.LabelStyle>

  <chart:NumericalAxis.AxisLineStyle>
   <chart:ChartLineStyle StrokeWidth ="0"/>
  </chart:NumericalAxis.AxisLineStyle>

  <chart:NumericalAxis.MajorTickStyle>
   <chart:ChartAxisTickStyle StrokeWidth ="0"/>
  </chart:NumericalAxis.MajorTickStyle>
 </chart:NumericalAxis>
</chart:SfCartesianChart.YAxes>

Customizing the series appearance

Next, customize the series appearance by specifying the column color.

<model:ColumnSeriesExt Fill="#B8D86D">

</model:ColumnSeriesExt>

Customize the data label appearance

To improve readability, we can activate the chart data labels using the ShowDataLabels property.

<model:ColumnSeriesExt ShowDataLabels="True">

 <chart:ColumnSeries.DataLabelSettings>
  <chart:CartesianDataLabelSettings LabelPlacement="Outer" UseSeriesPalette="False">
   <chart:CartesianDataLabelSettings.LabelStyle>
    <chart:ChartDataLabelStyle FontSize="{OnPlatform Android=11,iOS=11, Default=14}"
 FontFamily="AntaRegular" TextColor="Black"/>
   </chart:CartesianDataLabelSettings.LabelStyle>
  </chart:CartesianDataLabelSettings>
 </chart:ColumnSeries.DataLabelSettings>

</model:ColumnSeriesExt>

In the previous code example, the data labels display information about the water used during the production of the milk.

Enhancing chart interactivity

To augment the informativeness of our column chart, we will integrate selection features and relevant information into the title view. The selection features allow us to highlight a specific data point when it is selected.

In this instance, we include images of a cow and the plants from which milk is made and their carbon emissions values in the title view and enable segment selection. Consequently, the view will show information about the selected segment.

<model:ColumnSeriesExt>

  <chart:ColumnSeries.SelectionBehavior>
    <chart:DataPointSelectionBehavior SelectionBrush="#9BD241" Type="SingleDeselect" SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"/>
  </chart:ColumnSeries.SelectionBehavior>

</model:ColumnSeriesExt>

In the following code example, we have created the SourceImage, EmissionText, and SelectedIndex properties to store the carbon emissions data.

public class MilkSourceData : INotifyPropertyChanged
{

      private string sourceImage = "";
      public string SourceImage
      {
          get { return sourceImage; }
          set
          {
              if (sourceImage != value)
              {
                  sourceImage = value;
                  OnPropertyChanged(nameof(SourceImage));
              }
          }
      }

      private string emissionText = "";
      public string EmissionText
      {
          get { return emissionText; }
          set
          {
              if (emissionText != value)
              {
                  emissionText = value;
                  OnPropertyChanged(nameof(EmissionText));
              }
          }
      }

      private int selectedIndex;
      public int SelectedIndex
      {
          get { return selectedIndex; }
          set
          {
              selectedIndex = value;
              UpdateCountryAndCount();
              OnPropertyChanged(nameof(SelectedIndex));
          }
      }

      public MilkSourceData()
      {
          SelectedIndex = 1;
      }

      public event PropertyChangedEventHandler? PropertyChanged;
      protected virtual void OnPropertyChanged(string propertyName)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }

      private void UpdateCountryAndCount()
      {
          if (SelectedIndex >= 0 && SelectedIndex < Data?.Count)
          {
              SourceImage = Data[SelectedIndex].Source.ToLower() + ".png";
              EmissionText = Data[SelectedIndex].CO2Emission.ToString();
          }
      }
 }

After executing the previous code examples, the output will resemble the following GIF image.Visualizing the most sustainable milk data using .NET MAUI Column Chart

GitHub reference

For more details, refer to Visualizing the most sustainable types of milk using the .NET MAUI Column Chart GitHub demo.

Conclusion

Thank you for reading! In this blog, we have explored how to visualize the data on which milk is most sustainable based on various factors using the Syncfusion .NET MAUI Column Chart. We encourage you to follow the steps outlined in this blog and share your feedback in the comments section below.

Current customers can download the newest version of Essential Studio from the License and Downloads page. If you are not a Syncfusion customer, try our 30-day free trial to check out our features.

If you require assistance, please don’t hesitate to contact us via our support forumsupport portal, or feedback portal. We are always eager to help you!

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