Articles in this section

How to add and remove striplines dynamically in Blazor Charts?

This article explains how to add and remove striplines dynamically in Blazor Charts.

Add and Remove Striplines Dynamically

Blazor Charts provides an option to add and remove striplines dynamically. To dynamically add and remove striplines in Blazor Charts, you should use the ChartStriplines component to manage the striplines effectively.

The code example below demonstrates how to add and remove striplines dynamically. In this snippet, the custom StripCollection maintains the start, end, and color of the striplines. By iterating through the StripCollection with the ChartStriplines for the ChartPrimaryYAxis, you can manage the rendering of the striplines. Buttons are provided to trigger methods that add or remove striplines from the StripCollection, which is updated accordingly.

Index.razor:

@using Syncfusion.Blazor.Charts
@using Syncfusion.Blazor.Buttons

<SfButton @onclick="AddStripline">Add Stripline</SfButton>
<SfButton @onclick="RemoveStripline">Remove Stripline</SfButton>

<SfChart>
   <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category" />
   <ChartPrimaryYAxis>
       <ChartStriplines>
           @foreach (StripData stripline in StripCollection)
           {
               <ChartStripline Start="@stripline.Start" End="@stripline.End" Color="@stripline.Color" />
           }
       </ChartStriplines>
   </ChartPrimaryYAxis>
   <ChartSeriesCollection>
       <ChartSeries Type="ChartSeriesType.Line" Width="2" DataSource="@WeatherReports" XName="X" YName="Y">
       </ChartSeries>
   </ChartSeriesCollection>
</SfChart>

@code {
   List<StripData> StripCollection;
   
   public void AddStripline()
   {
       StripCollection.Add(new StripData
           {
               Start = 20,
               End = 25,
               Color = "red"
           });
   }
   public void RemoveStripline()
   {
       if (StripCollection.Count > 0)
       {
           StripCollection.Remove(StripCollection[StripCollection.Count - 1]);
       }
   }

   protected override void OnInitialized()
   {
       base.OnInitialized();
       StripCollection = new List<StripData>() {
           new StripData {
               Start = 30,
               End = 35,
               Color = "blue"
           }
       };
   }

   public class StripData
   {
       public double Start { get; set; }
       public double End { get; set; }
       public string Color { get; set; }
   }

   public class ChartData
   {
       public string X { get; set; }
       public double Y { get; set; }
   }

   public List<ChartData> WeatherReports = new List<ChartData>
   {
       new ChartData { X = "Sun", Y = 28 },
       new ChartData { X = "Mon", Y = 27 },
       new ChartData { X = "Tue", Y = 33 },
       new ChartData { X = "Wed", Y = 36 },
       new ChartData { X = "Thu", Y = 28 },
       new ChartData { X = "Fri", Y = 30 },
       new ChartData { X = "Sat", Y = 31 }
   };
} 

The following screenshot illustrates the output of the code snippet:

chrome_9Qzs9Bm73g.png

Live Sample for Adding and Removing Striplines Dynamically

Conclusion:

We hope you enjoyed learning how to add and remove striplines dynamically in Blazor Charts.

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)
Access denied
Access denied