SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components
Hi there, How can i use ZoomExtents() on MVVM ?
Hi there,
We have an article on various MVVM API’s here.
In order to ZoomExtents() in MVVM the simplest way is to call DataSeries.InvalidateParentSurface(RangeMode.ZoomToFit).
Best regards,
Andrew
che1024 (and Andrew):
I’d like to offer an alternative, although I’m unsure if it’s appropriate since it violates the view-viewmodel decoupling.
In your ViewModel, make a static delegate, or an action
public class YourViewModel
{
// This can be an event, if you prefer
public static Action UpdateChartElements;
public ObservableCollection<IRenderableSeriesViewModel> renderableSeriesList { get; set; }
// Rest of class
// method that plots data
public void plotData()
{
// if you are creating multiple renderable series, make sure you invoke
// this action OUTSIDE a for or foreach loop
foreach //...
{
renderableSeriesList.Add(...) // your renderable series
}
// This is C# 6.0 code. If you're using anything before that, make sure you're
// checking that your delegate handlers are not null before you invoke this action
UpdateChartElements?.Invoke();
}
}
In your codebehind, you can assign a method to the action:
public class YourViewCodeBehind
{
// Assigning the method to the delegate handler in the constructor
public YourViewCodeBehind()
{
InitializeComponent();
// Note that because your action is static, it assigns the method on the class level
ICPComparisonViewModel.UpdateChartElements += zoomExtentsMethod;
}
public void zoomExtentsMethod()
{
Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>
{
// You can add more UI stuff you want to update here
// Make sure you label your chart in XAML
yourChart.ZoomExtents();
}));
}
}
By calling the dispatcher, which is the UI thread, you are awaiting everything before rendering to be completed before the chart refreshes, which is why I would say be careful and make sure you’re not putting the dispatch invoke in the middle of a loop or between any possible CPU-intensive tasks.
Hope this helps!
— Ari
Please login first to submit.