This project is read-only.

Implement FitToView use MVVM mode

Feb 27, 2014 at 8:24 AM

I want to use ChartPlotter as MVVM mode, but I don't know how to implement FitToView, I try to use a button command,but it doesn't work.
This is my code:

private readonly Viewport2D viewport = new Viewport2D();
public ActionCommand FitToViewCommand { get; set; }
public ChartPlotterAdapter()
            this.FitToViewCommand = new ActionCommand(OnFitToViewCommandExecuted);
private void OnFitToViewCommandExecuted(object obj)
I hope I can get your help.
Apr 17, 2014 at 3:59 PM
I had a similar problem and figured out an easy way to handle it.

I created the following simple class:
/// <summary>
/// Used to get a handle on the Plotter that PlotterElements are attached to.
/// Provides a way to access the plotter object via MVVM.  Just add an instance
/// of this class to the list of PlotterElements that the Plotter2D is bound to,
/// then you can access the Plotter object via the Plotter property.
/// </summary>
public class PlotterReferencePlotterElement : IPlotterElement
    public void OnPlotterAttached(Plotter plotter)
        this.Plotter = plotter;

    public void OnPlotterDetaching(Plotter plotter)
        this.Plotter = null;

    public Plotter Plotter { get; private set; }

In my view model, I have an observable collection that the chart binds its ItemsSource to:
    public ObservableCollection<IPlotterElement> PlotterElements { get; private set; }
On initialisation of the view model, when you're adding your plotter elements, add a PlotterReferencePlotterElement
    private PlotterReferencePlotterElement plotterReferencePlotterElement;

    private void AddPlotterReferencePlotterElement()
        plotterReferencePlotterElement = new PlotterReferencePlotterElement();
Then, in your view model again, define the FitToView() function like so:
    public void FitToView()
        Plotter2D plotter2D = plotterReferencePlotterElement.Plotter as Plotter2D;
        if (plotter2D != null)
It can access the Plotter from the plotterReferencePlotterElement, and thus call FitToView().

It works fine for me!