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

How to create realtime data chart?

Is it possible to create a chart that allows realtime data to be displayed without creating new DataSource(IEnumerable) instead add one value and refresh chart? 

Something like this:
    public class ChartData
    {
        public string X;
        public double Y;
        public string Color;
    }
    public List DataSource = new List
    {
        new ChartData { X= "South Korea", Y= 39.4, Color="red" },
        new ChartData { X= "India", Y= 61.3, Color="green" },
        new ChartData { X= "Pakistan", Y= 20.4, Color="#ff0097" },
        new ChartData { X= "Germany", Y= 65.1, Color="crimson" },
        new ChartData { X= "Australia", Y= 15.8, Color="blue" },
    };
    void AddNewChartData()
    {
        this.DataSource.Add(new ChartData { X= "Italy", Y= 29.2, Color="darkorange" });
    }

Thank you.


6 Replies

KC Kalaimathi Chellaiah Syncfusion Team August 21, 2019 12:52 PM UTC

Hi Lukas, 
 
Greetings from Syncfusion 
 
Yes, that is possible to create a chart that allows realtime data to be displayed without creating new DataSource(IEnumerable) instead of adding one value and refresh chart using  ObservableCollection<T> Class. We have prepared a sample using your code snippet for your reference.  In that, chart dataSource is updated properly when changing the dataSource through a button click. Please find below code snippet, 
 
Code snippet: 
@using System.Collections.ObjectModel; 
<button type="button" class="btn btn-primary" @onclick="AddNewChartData">Add New ChartData</button> 
<EjsChart> 
    <ChartPrimaryXAxis ValueType="Syncfusion.EJ2.Blazor.Charts.ValueType.Category" Interval="1"></ChartPrimaryXAxis> 
    <ChartSeriesCollection> 
        <ChartSeries DataSource="@DataSource" XName="X" YName="Y" Type="ChartSeriesType.Column" PointColorMapping="Color"></ChartSeries> 
    </ChartSeriesCollection> 
</EjsChart> 
 
@code { 
    public ObservableCollection<ChartData> DataSource { get; set; } 
    public class ChartData 
    { 
        public string X { get; set; } 
        public double Y { get; set; } 
        public string Color { get; set; } 
        public static ObservableCollection<ChartData> GetData() 
        { 
            ObservableCollection<ChartData> DataSource = new ObservableCollection<ChartData> { 
                new ChartData { X= "South Korea", Y= 39.4, Color="red" }, 
                new ChartData { X= "India", Y= 61.3, Color="green" }, 
                new ChartData { X= "Pakistan", Y= 20.4, Color="#ff0097" }, 
                new ChartData { X= "Germany", Y= 65.1, Color="crimson" }, 
                new ChartData { X= "Australia", Y= 15.8, Color="blue" }, 
            }; 
            return DataSource; 
        } 
    } 
    protected override void OnInitialized() 
    { 
        this.DataSource = ChartData.GetData(); 
    } 
    public void AddNewChartData() 
    { 
        this.DataSource.Add(new ChartData { X = "Italy", Y = 29.2, Color = "darkorange" }); 
    } 
} 
 
Screenshot: 
Initial Rendering 
After Button Click 
 
 
 
 
Also here we have attached documentation link to know more information about ObservableCollection<T> Class, 
 
Let me know if you have any queries. 
Regards, 
Kalai. 


 



LU Lukas August 21, 2019 06:31 PM UTC

Hi, 

thank you for response. I tried this solution, its working, but only for first click. When I trying to add second value to chart with click method, collection is updated but chart not showing new values. Only for first time. Im trying to find where is a problem.



KC Kalaimathi Chellaiah Syncfusion Team August 22, 2019 11:03 AM UTC

Hi Lukas, 
 
Sorry for the inconvenience.  
 
We have a known bug in updating dynamic DataSource to series in the chart. I suggested that you use Series datasource wrongly in my previous update and apologize for that. So you can bind DataSource directly to chart instead of series. In this case, chart data is updated when every click is working fine as you expected. I have prepared a sample for your reference. Please find below the sample and code snippet. 
  
Code Snippet: 
@using System.Collections.ObjectModel; 
<EjsButton OnClick="AddNewChartData">Add New ChartData</EjsButton> 
       
  <EjsChart DataSource="@DataSource"> 
            <ChartSeriesCollection> 
                <ChartSeries XName="X" YName="Y" Type="ChartSeriesType.Column"></ChartSeries> 
            </ChartSeriesCollection> 
        </EjsChart> 
 
@code { 
    int Count = 5; 
    public ObservableCollection<ChartData> DataSource { get; set; } 
    public class ChartData 
    { 
        public double X { get; set; } 
        public double Y { get; set; } 
        public static ObservableCollection<ChartData> GetData() 
        { 
            ObservableCollection<ChartData> DataSource = new ObservableCollection<ChartData> { 
                new ChartData { X= 1, Y= 39.4 }, 
                new ChartData { X= 2, Y= 61.3 }, 
                new ChartData { X= 3, Y= 20.4 }, 
                new ChartData { X= 4, Y= 65.1 }, 
                new ChartData { X= 5, Y= 15.8 } 
            }; 
            return DataSource; 
        } 
    } 
    protected override void OnInitialized() 
    { 
        this.DataSource = ChartData.GetData(); 
    } 
    public void AddNewChartData() 
    { 
        Count += 1; 
        this.DataSource.Add(new ChartData { X = Count, Y = new Random().Next(10, 100) }); 
    } 
} 
 
 
 
We also consider that the DataSource series is not being updated properly as a bug and that the fix will be available in our upcoming weekly patch release scheduled for August 27, 2019. 
 
Let me know if you have any queries. 
 
Regards, 
Kalai. 
 



LU Lukas August 27, 2019 01:27 PM UTC

Hi,

it works well.

Thank you.


KC Kalaimathi Chellaiah Syncfusion Team August 28, 2019 11:18 AM UTC

Hi Lukas, 
 
Most welcome. Kindly get in touch with us, if you would require further assistance. We are always happy in assisting you. Also we would like to let you know still we are working on the reported issue in series data binding , so the fix will be available in our next weekly patch release which is scheduled to be rolled out on or before September 3rd , 2019. We appreciate your patience until then. 
 
Let me know if you have any concerns. 
 
Thanks,   
Kalai.  



KC Kalaimathi Chellaiah Syncfusion Team September 17, 2019 11:31 AM UTC

Hi Lukas, 
 
Thanks for being patience. 
 
We have validated your reported issue in our latest blazor bi-weekly release. In that series databinding is working fine as you expect. So you can use the latest (17.2.0.50-beta) Syncfusion.EJ2.Blazor NuGet package version and updated interop CDN file to get rid of the reported issue.  
 
 
 
 
Kindly revert us, if you have any concerns.  

Regards, 
Kalai. 



Loader.
Live Chat Icon For mobile
Up arrow icon