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.

Update a chart using listbox selection

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

You need to access the selected item value in the ValueChange event using the GetDataByValue method of ListBox component. Then, assign the obtained series type to ChartSeries Type and assign new data to 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

I hope you enjoyed learning how to update chart data and series type using a listbox in Blazor Chart Component.

You can refer to our Blazor Chart 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 Chart 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, 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