<chart:SfDateTimeRangeNavigator Grid.Row="1" XBindingPath="XValue” ItemsSource="{Binding CurrentData}"/>
|
public class ViewModel
{
public ViewModel()
{
var date = new DateTime(2019, 1, 1);
TempData = new ObservableCollection<Model>();
CurrentData = new ObservableCollection<Model>();
CurrentData.Add(new Model() { XValue = date.AddDays(0), YValue = 20 });
CurrentData.Add(new Model() { XValue = date.AddDays(1), YValue = 10 });
CurrentData.Add(new Model() { XValue = date.AddDays(2), YValue = 40 });
CurrentData.Add(new Model() { XValue = date.AddDays(3), YValue = 30 });
}
public ObservableCollection<Model> TempData { get; set; }
public ObservableCollection<Model> CurrentData { get; set; }
}
|
public partial class MainWindow : Window
{
Random randomVariable = new Random();
DateTime lastDate = new DateTime();
// Timer to load dynamic data.
DispatcherTimer timer;
ViewModel viewModel;
public MainWindow()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromMilliseconds(100);
viewModel = this.DataContext as ViewModel;
}
private void Timer_Tick(object sender, EventArgs e)
{
var itemsSource = viewModel.TempData;
// Gets the last date from the collection.
if (itemsSource.Count > 0)
{
lastDate = itemsSource[itemsSource.Count - 1].XValue;
}
else
{
lastDate = viewModel.CurrentData[viewModel.CurrentData.Count - 1].XValue;
}
itemsSource.Add(new Model() { XValue = lastDate.AddDays(1), YValue = randomVariable.Next(10, 40) });
}
private void StartTimerButton_Click(object sender, RoutedEventArgs e)
{
timer.Start();
}
private void EndTimerButton_Click(object sender, RoutedEventArgs e)
{
timer.Stop();
}
private void UpdateButton_Click(object sender, RoutedEventArgs e)
{
var viewModel = (this.DataContext as ViewModel);
foreach (var tempData in viewModel.TempData)
{
viewModel.CurrentData.Add(tempData);
}
viewModel.TempData.Clear();
}
}
|