Articles in this section
Category / Section

How to filter data for series using a specific field from a list in Blazor Charts

4 mins read

This article explains how to filer data for chart series using a specific field.

Filtering chart series data using a condition

Blazor Charts provides an option to filter the data for each chart series using a specific field in a list.

You can achieve this by using the Where method to filter the list based on a specific field value, ensuring it matches a given number. The filtered results are then stored in a list for the chart series. Afterward, you can use a foreach loop to iterate through the series collection and assign each list to its corresponding series in the chart.

The below code example demonstrates how to filter data for chart series.

@using Syncfusion.Blazor.Charts
<SfChart>
   <ChartPrimaryXAxis IsIndexed="true"  ValueType="@Syncfusion.Blazor.Charts.ValueType.Category" Title="Project Numbers">
   </ChartPrimaryXAxis>
   <ChartPrimaryYAxis Title="Students" ></ChartPrimaryYAxis>
   <ChartSeriesCollection>
       @foreach (var series in SeriesCollection)
       {
           <ChartSeries DataSource="@series.Data" Name="@series.Name" XName="@series.XName" YName="@series.YName" ColumnWidth="0.7" ColumnSpacing="0.1" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Bar">
               <ChartMarker Visible="true" Shape="ChartShape.Diamond" Fill="red" Height="10" Width="10">
                   <ChartMarkerBorder Width="2" Color="blue"></ChartMarkerBorder>
                   <ChartDataLabel Visible="true" Name="Employee" />
               </ChartMarker>
           </ChartSeries>
       }
   </ChartSeriesCollection>
   <ChartLegendSettings Visible="true"></ChartLegendSettings>
</SfChart>

@code {
   public class KWDataModel
   {
       public double ProjectNumber { get; set; }
       public string Employees { get; set; }
       public double Hours { get; set; }
   }

   public class SeriesData
   {
       public string XName { get; set; }
       public string YName { get; set; }
       public string Name { get; set; }
       public IEnumerable<KWDataModel> Data { get; set; }
   }

   private List<KWDataModel> KWData = new List<KWDataModel>
   {
       new KWDataModel { ProjectNumber = 1, Employees = "Max", Hours = 5 },
       new KWDataModel { ProjectNumber = 1, Employees = "Moritz", Hours = 15 },
       new KWDataModel { ProjectNumber = 1, Employees = "Karl", Hours = 20 },
       new KWDataModel { ProjectNumber = 2, Employees = "Max", Hours = 5 },
       new KWDataModel { ProjectNumber = 2, Employees = "Karl", Hours = 15 }
   };

   private List<SeriesData> SeriesCollection;

   protected override void OnInitialized()
   {
       var project1Data = KWData.Where(k => k.ProjectNumber == 1).ToList();
       var project2Data = KWData.Where(k => k.ProjectNumber == 2).ToList();

       SeriesCollection = new List<SeriesData>
       {
           new SeriesData { Name="Series1", XName = nameof(KWDataModel.ProjectNumber), YName = nameof(KWDataModel.Hours), Data = project1Data },
           new SeriesData { Name="Series2", XName = nameof(KWDataModel.ProjectNumber), YName = nameof(KWDataModel.Hours), Data = project2Data }
       };
   }
} 

Output

The following screenshot illustrates the output of the code snippet.

image.png

Live Sample for Filtering ChartSeries Data

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