Articles in this section
Category / Section

How to update chart data and series type using a listbox in Blazor Charts?

9 mins read

This article explains how to update chart data and series type using a listbox in Blazor Charts.

Update a Chart Using ListBox Selection

Blazor Charts provides an option to update the chart data and its series type using a selected item in the ListBox component.

You need to access the selected item value in the ValueChange event using the GetDataByValue method of the ListBox component. Then, assign the obtained series type to the ChartSeriesType and assign new data to the ChartSeries DataSource. The chart will be updated with the new data and the selected series type.

The code example below demonstrates how to update the chart data and series type using a ListBox selection.

Index.razor:

@using Syncfusion.Blazor.Charts
@using Syncfusion.Blazor.DropDowns
<div class="container-fluid">
   <div class="row">
       <div class="col-lg-4">
           <SfListBox @ref="ListBoxObj" Value="@value" DataSource="@chartTypes" TItem="ListItem" TValue="string[]">
               <ListBoxSelectionSettings Mode="Syncfusion.Blazor.DropDowns.SelectionMode.Single" ShowCheckbox="true"></ListBoxSelectionSettings>
               <ListBoxEvents TItem="ListItem" TValue="string[]" ValueChange="OnListValueChange"></ListBoxEvents>
               <ListBoxFieldSettings Text="Type" Value="Id"></ListBoxFieldSettings>
           </SfListBox>
       </div>
       <div class="col-lg-8">
           <SfChart Title="Sales Analysis">
               <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
               <ChartSeriesCollection>
                   <ChartSeries DataSource="@Sales" Width=2 XName="Month" YName="SalesValue" Type="@seriesType">
                       <ChartMarker Visible="true"></ChartMarker>
                   </ChartSeries>
               </ChartSeriesCollection>
           </SfChart>
       </div>
   </div>
</div>
@code {
   SfListBox<string[], ListItem> ListBoxObj;
   public Syncfusion.Blazor.Charts.ChartSeriesType seriesType { get; set; } = Syncfusion.Blazor.Charts.ChartSeriesType.Line;
   public void OnListValueChange(ListBoxChangeEventArgs<string[], ListItem> args)
   {
       var data = ListBoxObj.GetDataByValue(args.Value);
       seriesType = data[0].Type;
       switch (seriesType)
       {
           case Syncfusion.Blazor.Charts.ChartSeriesType.Spline:
               Sales = new List<SalesInfo>
               {
                   new SalesInfo { Month = "Jan", SalesValue = 45 },
                   new SalesInfo { Month = "Feb", SalesValue = 38 },
                   new SalesInfo { Month = "Mar", SalesValue = 44 },
                   new SalesInfo { Month = "Apr", SalesValue = 42 },
                   new SalesInfo { Month = "May", SalesValue = 50 },
                   new SalesInfo { Month = "Jun", SalesValue = 42 },
                   new SalesInfo { Month = "Jul", SalesValue = 45 }
               };
               break;
           case Syncfusion.Blazor.Charts.ChartSeriesType.StepLine:
               Sales = new List<SalesInfo>
               {
                   new SalesInfo { Month = "Jan", SalesValue = 55 },
                   new SalesInfo { Month = "Feb", SalesValue = 48 },
                   new SalesInfo { Month = "Mar", SalesValue = 54 },
                   new SalesInfo { Month = "Apr", SalesValue = 52 },
                   new SalesInfo { Month = "May", SalesValue = 60 },
                   new SalesInfo { Month = "Jun", SalesValue = 52 },
                   new SalesInfo { Month = "Jul", SalesValue = 55 }
               };
               break;
           case Syncfusion.Blazor.Charts.ChartSeriesType.Area:
               Sales = new List<SalesInfo>
               {
                   new SalesInfo { Month = "Jan", SalesValue = 65 },
                   new SalesInfo { Month = "Feb", SalesValue = 58 },
                   new SalesInfo { Month = "Mar", SalesValue = 64 },
                   new SalesInfo { Month = "Apr", SalesValue = 62 },
                   new SalesInfo { Month = "May", SalesValue = 70 },
                   new SalesInfo { Month = "Jun", SalesValue = 62 },
                   new SalesInfo { Month = "Jul", SalesValue = 65 }
               };
               break;
           case Syncfusion.Blazor.Charts.ChartSeriesType.Column:
               Sales = new List<SalesInfo>
               {
                   new SalesInfo { Month = "Jan", SalesValue = 75 },
                   new SalesInfo { Month = "Feb", SalesValue = 68 },
                   new SalesInfo { Month = "Mar", SalesValue = 74 },
                   new SalesInfo { Month = "Apr", SalesValue = 72 },
                   new SalesInfo { Month = "May", SalesValue = 80 },
                   new SalesInfo { Month = "Jun", SalesValue = 72 },
                   new SalesInfo { Month = "Jul", SalesValue = 75 }
               };
               break;
           default:
               Sales = new List<SalesInfo>
               {
                   new SalesInfo { Month = "Jan", SalesValue = 35 },
                   new SalesInfo { Month = "Feb", SalesValue = 28 },
                   new SalesInfo { Month = "Mar", SalesValue = 34 },
                   new SalesInfo { Month = "Apr", SalesValue = 32 },
                   new SalesInfo { Month = "May", SalesValue = 40 },
                   new SalesInfo { Month = "Jun", SalesValue = 32 },
                   new SalesInfo { Month = "Jul", SalesValue = 35 }
               };
               break;
       }
   }
   public class SalesInfo
   {
       public string Month { get; set; }
       public double SalesValue { get; set; }
   }

   public List<SalesInfo> Sales = new List<SalesInfo>
   {
   new SalesInfo { Month = "Jan", SalesValue = 35 },
   new SalesInfo { Month = "Feb", SalesValue = 28 },
   new SalesInfo { Month = "Mar", SalesValue = 34 },
   new SalesInfo { Month = "Apr", SalesValue = 32 },
   new SalesInfo { Month = "May", SalesValue = 40 },
   new SalesInfo { Month = "Jun", SalesValue = 32 },
   new SalesInfo { Month = "Jul", SalesValue = 35 }
   };
   private string[] value = new string[] { "List-01" };
   private List<ListItem> chartTypes = new List<ListItem>
   {
       new ListItem { Type = Syncfusion.Blazor.Charts.ChartSeriesType.Line, Id = "List-01" },
       new ListItem { Type = Syncfusion.Blazor.Charts.ChartSeriesType.Spline, Id = "List-02" },
       new ListItem { Type = Syncfusion.Blazor.Charts.ChartSeriesType.StepLine, Id = "List-03" },
       new ListItem { Type = Syncfusion.Blazor.Charts.ChartSeriesType.Area, Id = "List-04" },
       new ListItem { Type = Syncfusion.Blazor.Charts.ChartSeriesType.Column, Id = "List-05" },
   };
   public class ListItem
   {
       public Syncfusion.Blazor.Charts.ChartSeriesType Type { get; set; }
       public string Id { get; set; }
   }
}

The following screenshot illustrates the output of the code snippet.

Output:

image.png

Live Sample for Updating a Chart Using ListBox

Conclusion:

We hope you enjoyed learning how to update chart data and series type using a ListBox in the Blazor Chart component.

You can refer to our Blazor Chart feature tour page to learn about its other groundbreaking features, documentation, and how to quickly get started with configuration specifications. You can also explore our Blazor Chart example to understand how to create and manipulate data.

For current customers, our Blazor components are available on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to evaluate our Blazor Charts and other Blazor components.

If you have any questions or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, 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