Articles in this section

How to dynamically change stripline values in Blazor Charts?

This article explains how to dynamically change ChartStripline values in Blazor Charts.

Dynamically Change Stripline Values

Blazor Charts provides an option to dynamically change ChartStripline values. To dynamically update the stripline values, you need to set up an observable collection of stripline values and then bind the observable collection data to ChartStripline.

In this code snippet, an Chart component is utilized with dynamically updating striplines on both the x-axis and y-axis. The UpdateStriplines method generates random Start and End values within specified ranges, ensuring a gap between the Start and End values, and assigns random colors to the striplines. The striplines are managed using ObservableCollection to allow dynamic updates, and the chart automatically reflects these updates upon the button click

By clicking the “Update Striplines” button, the UpdateStriplines method is triggered, updating the striplines on the chart with new random values and colors, demonstrating the dynamic capabilities of Blazor Charts.

Index.razor

@using Syncfusion.Blazor.Charts
@using System.Collections.ObjectModel

<button @onclick="UpdateStriplines">Update Striplines</button>

<SfChart>
   <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category">
       <ChartStriplines>
           @foreach (var stripline in XAxisStriplines)
           {
               <ChartStripline Start="@stripline.Start" End="@stripline.End" Color="@stripline.Color" />
           }
       </ChartStriplines>
   </ChartPrimaryXAxis>

   <ChartPrimaryYAxis>
       <ChartStriplines>
           @foreach (var stripline in YAxisStriplines)
           {
               <ChartStripline Start="@stripline.Start" End="@stripline.End" Color="@stripline.Color" />
           }
       </ChartStriplines>
   </ChartPrimaryYAxis>

   <ChartSeriesCollection>
       <ChartSeries Type="ChartSeriesType.Column" DataSource="@WeatherReports" XName="X" YName="Y">
       </ChartSeries>
   </ChartSeriesCollection>
</SfChart>

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

   public class Stripline
   {
       public double Start { get; set; }
       public double End { get; set; }
       public string Color { 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 }
   };

   // Using ObservableCollection for dynamic updates
   public ObservableCollection<Stripline> XAxisStriplines { get; set; } = new ObservableCollection<Stripline>
   {
       new Stripline { Start = 2, End = 3, Color = "#EEFFCC" },
       new Stripline { Start = 4, End = 5, Color = "pink" }
   };

   public ObservableCollection<Stripline> YAxisStriplines { get; set; } = new ObservableCollection<Stripline>
   {
       new Stripline { Start = 20, End = 25, Color = "red" },
       new Stripline { Start = 32, End = 35, Color = "blue" }
   };

   private void UpdateStriplines()
   {
       Random random = new Random();

       int space = 1;

       XAxisStriplines.Clear();
       for (int i = 0; i < 2; i++)
       {
           double start = random.Next(1, 7 - space);
           double end = start + space + random.Next(1, 3);
           XAxisStriplines.Add(new Stripline
               {
                   Start = start,
                   End = end,
                   Color = GetRandomColor()
               });
       }

       YAxisStriplines.Clear();
       for (int i = 0; i < 2; i++)
       {
           double start = random.Next(20, 36 - space);
           double end = start + space + random.Next(1, 5);
           YAxisStriplines.Add(new Stripline
               {
                   Start = start,
                   End = end,
                   Color = GetRandomColor()
               });
       }
   }

   private string GetRandomColor()
   {
       Random random = new Random();
       return $"#{random.Next(0x1000000):X6}";
   }
} 

The following screenshot illustrates the output of the code snippet.

msedge_9ARgeo49ZP.gif

Live Sample for Dynamically Changing Stripline Values

Conclusion

I hope you enjoyed learning how to dynamically change ChartStripline values in Blazor Charts.

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