Articles in this section
Category / Section

How to Filter Data for Blazor Chart Series Using Specific Field?

4 mins read

This article explains how to filer data for Blazor Charts 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

Conclusion
I hope you enjoyed learning how to filer data for Blazor Chart series using a specific field.
You can refer to our Blazor Charts feature tour page to know about its other groundbreaking feature representations and Documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor Charts example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. 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)
Please  to leave a comment
Access denied
Access denied