Is it possible to redraw sfChart when Series.ItemsSource changes?
It's OK when adding data to ItemsSource, but sfChart is not redraw when changing the data of ItemsSource already drawn to sfChart (ex. when ItemsSource has 5 drawn data, the change of ItemsSource[0] is not reflected to the chart).
My app keeps getting data. So I want to delete old data and draw only the latest 50 data in sfChart.
|
<chart:ColumnSeries
ItemsSource="{Binding Data}"
XBindingPath="Name"
YBindingPath="YValue" ListenPropertyChange="True" /> |
|
public class ChartDataModel : INotifyPropertyChanged
{
private double yValue;
public double YValue
{
get { return yValue; }
set
{
if (value != yValue)
{
yValue = value;
RaisePropertyChanged(nameof(YValue));
}
}
}
private string name;
public string Name
{
get { return name; }
set
{
if (value != name)
{
name = value;
RaisePropertyChanged(nameof(Name));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
} |
Hi Devakumar D,
Thank you for your reply.
I've already implemented that, and I've also checked that PropertyChanged is actually called, but the graph only redraws when a value is added.
Is there anything else I should check, such as how to call ResumeNotification() or SuspendNotification()?
Regards,
Akina
There was a problem to update data.
It works good now.
Thank you for your advice.
|
public class ViewModel
{
….
public Action BeginDataUpdate;
public Action EndDataUpdate;
}
public partial class MainPage : ContentPage
{
ViewModel viewModel;
….
public MainPage()
{
InitializeComponent();
viewModel = new ViewModel();
this.BindingContext = viewModel;
viewModel.BeginDataUpdate = () => chart.SuspendSeriesNotification();
viewModel.EndDataUpdate = () => chart.ResumeSeriesNotification();
}
….
public async void LoadData()
{
await Task.Delay(500);
//dynamic change data using suspend and resume
Device.BeginInvokeOnMainThread(() =>
{
//chart.SuspendSeriesNotification() is called when using below code.
viewModel.BeginDataUpdate();
for (int i = 0; i < 100; i++)
{
//Add Data
}
//chart.ResumeSeriesNotification() is called when using below code.
viewModel.EndDataUpdate();
});
….
}
} |
hi,
thanks for this post!
one question, i have animations in my chart but works just on add and not when i update a value. How can i solve it?
thanks
Hi Luca,
You can enable the animation when updating the value of a data point by invoking the Animate method of the series as shown in the below code snippet.
private void Button_Clicked(object sender, EventArgs e) { Random random = new Random(); viewModel.Data[5].YValue = random.Next(50, 60); series.Animate(); } |
Please let us know if you have any other queries.
Thanks,
Manivannan E