We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

getting error on chacking the count of SeriesCollection(SeriesCollection.Count() > 0)

Hi All,


I am getting the error while checking the series collection. If I remove the condition it works fine but I only want to display chart when it has series data as it is a realtime chart(data is populating continously using web socket)



Error details: 



Code:

@using Newtonsoft.Json

@using SharedBO.DTOs

@using Syncfusion.Blazor.Charts

@using Syncfusion.Blazor.Buttons

@using System.Collections.ObjectModel

@using System.Net.WebSockets

@using Syncfusion.Blazor.Lists

@using Syncfusion.Blazor.DropDowns

@using System.Text

@inject ILogger<MultipleRealTime> logger

<style>

    .e-chart {

        height: inherit !important;

        width: inherit !important;

    }


    .e-accumulationchart {

        height: inherit !important;

        width: inherit !important;

    }

    test123_svg{

        width: 800px!important;

    }

</style>


@*<SfListView DataSource="@points" ShowCheckBox="true" @ref="sfListView" Width="150px">

    <ListViewFieldSettings TValue="PointDataInfo" Id="PointId" Text="PointName"></ListViewFieldSettings>

</SfListView>*@


<SfButton @onclick="AddChartSeries">Real Time Chart</SfButton>

@if (SeriesCollection.Count() > 0)

{

    <div class="container" Width="100%">

        <SfChart @ref="liveChart" Width="100%" ID="test123">

            <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.DateTimeCategory" LabelFormat="hh:mm:ss tt" Title="Time (s)">

            </ChartPrimaryXAxis>

            <ChartAxes>

                @foreach (var item in SeriesCollection)

                {

                    <ChartAxis Name=@item.UOM OpposedPosition="false" Title=@item.UOM />

                }

            </ChartAxes>

            <ChartSeriesCollection>

                @foreach (var series in SeriesCollection)

                {

                    <ChartSeries Type="ChartSeriesType.Line" XName=@series.XName YName=@series.YName DataSource=@series.Data Name=@series.PointName YAxisName=@series.UOM>

                    </ChartSeries>

                }

            </ChartSeriesCollection>

        </SfChart>

    </div>

    }




@code {

    private SfListView<PointDataInfo> sfListView;

    private SfChart liveChart;

    public Boolean isChartLoaded = false;

    public class SeriesData

    {

        public string XName

        {

            get;

            set;

        }

        public string YName

        {

            get;

            set;

        }

        public ObservableCollection<ChartDataPoint> Data

        {

            get;

            set;

        }

        public Guid PointId { get; set; }


        public string PointName { get; set; }


        public string UOM { get; set; }

    }

    public PointDataInfo[] points = {

        new PointDataInfo { PointName ="Inverter 1A", PointId = Guid.Parse("37ef3157-2101-4d3f-8821-d5b43276d2e5") ,UOM="kW"},

        new PointDataInfo { PointName = "Irriadence", PointId = Guid.Parse("e219f4d9-5206-40dd-abf2-db732e54d817"),UOM="w/m2" },

        new PointDataInfo { PointName = "Backpanel Temp", PointId = Guid.Parse("c75ffb5d-af1a-4456-8d06-0dc9a543f4d7"),UOM="C"},

    };

    public class ChartDataPoint

    {

        public string Timestr { get; set; }

        //public double metrics { get; set; }

        public double metrics { get; set; }

    }


    ObservableCollection<SeriesData> SeriesCollection = new ObservableCollection<SeriesData>();


    public async Task AddChartSeries()

    {

        //SelectedItems<PointDataInfo> items = await sfListView.GetCheckedItemsAsync();

        await Connection(points.ToList());


    }


    protected override void OnInitialized()

    {

        base.OnInitialized();

    }


    public async Task Connection(List<PointDataInfo> reqList)

    {

        var clientConnection = new ChartClientConnection(new ClientWebSocket(), new Uri("wss://localhost:8081/pointInfo"), logger);

        var cts = new CancellationTokenSource();

        await foreach (var item in clientConnection.ConnectAsync(reqList, cts.Token))

        {

            foreach (var x in item)

            {

                var seriesData = SeriesCollection.Where(y => x.PointId == y.PointId).FirstOrDefault();

                if (seriesData != null)

                {

                    if (seriesData.Data.Count >10)

                    {

                        seriesData.Data.RemoveAt(0);

                    }


                    seriesData.Data.Add(new ChartDataPoint

                        {

                            Timestr = x.Timestr,

                            metrics = x.dataValue

                        });

                }

                else

                {

                    SeriesData SeriesData = new SeriesData();

                    SeriesCollection.Add(new SeriesData

                        {

                            XName = nameof(ChartDataPoint.Timestr),

                            YName = nameof(ChartDataPoint.metrics),

                            Data = new ObservableCollection<ChartDataPoint>

                            {

                                new ChartDataPoint

                                {

                                    Timestr = x.Timestr,

                                    metrics = x.dataValue

                                }

                            },

                            PointId=x.PointId,

                            PointName=x.PointName,

                            UOM=x.UOM

                        });

                    StateHasChanged();

                }

                //logger.LogError(DateTime.Now.ToString() +" "+ x.Timestr+ " " + x.PointId);

            }

        }

    }

}


Thanks

Pankaj


1 Reply

DG Durga Gopalakrishnan Syncfusion Team October 28, 2022 03:26 PM UTC

Hi Pankaj,


Greetings from Syncfusion.


We have ensured your reported scenario by checking the series collection count before rendering the chart; control is rendered fine, and we were able to add and remove the series dynamically without any exception. We have attached the tested sample and video for your reference.


Video : https://www.syncfusion.com/downloads/support/directtrac/general/ze/AddRemove1011012128.zip


Sample : https://www.syncfusion.com/downloads/support/directtrac/general/ze/SeriesCollection-688690602.zip


If you are still facing problem, please try to replicate the reported scenario in above sample or share us issue reproduced sample to validate this case further from our end.


Please revert us if you have any concerns.


Regards,

Durga Gopalakrishnan.


Loader.
Live Chat Icon For mobile
Up arrow icon