Online Chart

1 Answer 87 Views
Chart
Daniel
Top achievements
Rank 1
Silver
Bronze
Daniel asked on 08 Mar 2022, 11:15 AM

Hi,

I want sample code that show chart that updated when new record inserted

Axis x - the number of records

Axis Y - how many records inserted in particular time

Thanks in advance,

1 Answer, 1 is accepted

Sort by
0
Antoan
Telerik team
answered on 11 Mar 2022, 01:58 PM

Hi Daniel,

Thank you for the provided image.

To achieve the desired result the chart can be bound to ObservableCollection where the records are stored. As soon as a new item is added to the collection, the UI will be updated. 

For more information on how the Chart can be populated using ObservableCollection, please go to the following link:

https://docs.telerik.com/devtools/maui/controls/chart/series/cartesian/line-series 

So, if we take the example from the article as a base and add a simple button with Clicked event:

  <Grid RowDefinitions="Auto, *">
            <Button Text="Add Item" Clicked="AddNewItem_Clicked" />      
            <telerikChart:RadCartesianChart x:Name="chart" Grid.Row="1">
                <telerikChart:RadCartesianChart.BindingContext>
                    <local:SeriesCategoricalViewModel />
                </telerikChart:RadCartesianChart.BindingContext>
                <telerikChart:RadCartesianChart.HorizontalAxis>
                    <telerikChart:CategoricalAxis LabelFitMode="MultiLine"
                                          PlotMode="OnTicks" />
                </telerikChart:RadCartesianChart.HorizontalAxis>
                <telerikChart:RadCartesianChart.VerticalAxis>
                    <telerikChart:NumericalAxis />
                </telerikChart:RadCartesianChart.VerticalAxis>
                <telerikChart:RadCartesianChart.Series>
                    <telerikChart:LineSeries ValueBinding="Value"
                                     CategoryBinding="Category"
                                     ItemsSource="{Binding Data1}" />
                    <telerikChart:LineSeries ValueBinding="Value"
                                     CategoryBinding="Category"
                                     ItemsSource="{Binding Data2}" />
                </telerikChart:RadCartesianChart.Series>
            </telerikChart:RadCartesianChart>          
        </Grid>

In the event handler add a new CategoricalData point:

private void AddNewItem_Clicked(object sender, EventArgs args)
{
    (chart.BindingContext as SeriesCategoricalViewModel).Data1.Add(new CategoricalData() { Category = "New Point", Value= 15});
}

As soon as the app user taps the button, the Chart will be updated and the new value will be added to the line series.

I hope this sheds some light on this case. Let me know if I can provide any further assistance.

Regards,
Antoan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 11 Mar 2022, 04:33 PM | edited

when l mean online i mean that the record added
Into database in other place. I want that collection will be update online. Hope is more clear. I use implemntation from the article.
Yana
Telerik team
commented on 14 Mar 2022, 08:40 AM

This is just a sample example - no matter how and when a point is added to the ObservableCollection ( on a button click or due to some other event in the code), the collection itself notifies the UI about the change and the Chart gets updated.  The logic for updating the collection depends on the app requirements and is not directly related to the Chart control.
Tags
Chart
Asked by
Daniel
Top achievements
Rank 1
Silver
Bronze
Answers by
Antoan
Telerik team
Share this question
or