Search results

Add or Remove Series in Blazor Chart component

You can add or remove the chart series dynamically by using the AddSeries or RemoveSeries method.

To add or remove the series dynamically, follow the given steps:

Step 1:

To add a new series to chart dynamically, pass the series value to the AddSeries method.

 public void AddChartSeries()
    {
        List<ChartSeries>seriesCollection = new List<ChartSeries>();
        seriesCollection.Add(new ChartSeries { Name = nameof(MyDataModel.XValue), XName = nameof(MyDataModel.XValue), YName = nameof(MyDataModel.YValue), DataSource = this.GetData(), Fill = colors[rnd.Next(colors.Length - 1)], Animation = new ChartSeriesAnimation { Enable = false } });
        Chartobj.AddSeries(seriesCollection);
    }

Step 2: To remove the new series from chart dynamically, pass the series index to the RemoveSeries method.

   public void RemoveChartSeries()
   {
       Chartobj.RemoveSeries(Chartobj.Series.Count - 1);
   }
add-remove.razor
@using Syncfusion.Blazor.Charts

<button class="btn btn-success" @onclick="AddChartSeries">Add Series</button>
<button class="btn btn-success" @onclick="RemoveChartSeries">Remove Series</button>
<br />
<br />
<div class="col-10">
    <SfChart @ref="Chartobj">
        <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Double">
            <ChartSeriesCollection>
                <ChartSeries Name="@nameof(MyDataModel.XValue)" DataSource="@chartData" YName="@nameof(MyDataModel.YValue)" XName="@nameof(MyDataModel.XValue)">
                    <ChartSeriesAnimation Enable="false"> </ChartSeriesAnimation>
                </ChartSeries>
            </ChartSeriesCollection>
        </ChartPrimaryXAxis>
    </SfChart>
</div>

@code {
    public SfChart Chartobj;
    public string[] colors = new string[] { "Red", "Green", "Blue", "Yellow", "Orange", "Purple", "Black", "Aqua", "Lime", "Gray" };
    private Random rnd = new Random();

    public class MyDataModel
    {
        public int XValue { get; set; }
        public int YValue { get; set; }
    }
    public List<MyDataModel>
    chartData = new List<MyDataModel>
        ();
    protected override async Task OnInitializedAsync()
    {
        for (int i = 0; i < 10; i++)
        {
            chartData.Add(new MyDataModel() { XValue = i, YValue = rnd.Next(100, 400) });
        }
    }
    public List<MyDataModel>
        GetData()
    {
        List<MyDataModel> data = new List<MyDataModel>();
        for (int i = 0; i < 10; i++)
        {
            data.Add(new MyDataModel() { XValue = i, YValue = rnd.Next(100, 400) });
        }
        return data;
    }
    public void AddChartSeries()
    {
        List<ChartSeries> seriesCollection = new List<ChartSeries>();
        seriesCollection.Add(new ChartSeries { Name = nameof(MyDataModel.XValue), XName = nameof(MyDataModel.XValue), YName = nameof(MyDataModel.YValue), DataSource = this.GetData(), Fill = colors[rnd.Next(colors.Length - 1)], Animation = new ChartSeriesAnimation { Enable = false } });
        Chartobj.AddSeries(seriesCollection);
    }
    public void RemoveChartSeries()
    {
        Chartobj.RemoveSeries(Chartobj.Series.Count - 1);
    }
}