question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create a ScottPlot WPF control using MVVM pattern

See original GitHub issue

Feature Suggestion

Feature description: IPlottable to implement the INotifyPropertyChanged interface

Code example: If applicable, use this space to demonstrate how you wish the new feature could be used in code.


    /// IPlottable interface implementing INotifyPropertyChanged
    public interface IPlottable : INotifyPropertyChanged
    {
        bool IsVisible { get; set; }
        void Render(PlotDimensions dims, System.Drawing.Bitmap bmp, bool lowQuality = false);

        int XAxisIndex { get; set; }
        int YAxisIndex { get; set; }

        /// <summary>
        /// Returns items to show in the legend. Most plottables return a single item. in this array will appear in the legend.
        /// Plottables which never appear in the legend can return null.
        /// </summary>
        LegendItem[] GetLegendItems();

        /// <summary>
        /// Return min and max of the horizontal and vertical data contained in this plottable.
        /// Double.NaN is used for axes not containing data.
        /// </summary>
        /// <returns></returns>
        AxisLimits GetAxisLimits();

        /// <summary>
        /// Throw InvalidOperationException if ciritical variables are null or have incorrect sizes. 
        /// Deep validation is slower but also checks every value for NaN and Infinity.
        /// </summary>
        void ValidateData(bool deep = false);
    }

    /// avoid redundant code
    public abstract class PropertyNotifier : INotifyPropertyChanged
    {
        public PropertyNotifier() : base() { }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    /// <summary>
    /// The scatter plot renders X/Y pairs as points and/or connected lines.
    /// Scatter plots can be extremely slow for large datasets, so use Signal plots in these situations.
    /// </summary>
    public class ScatterPlot : PropertyNotifier, IPlottable, IHasPoints, IHasLine, IHasMarker, IHighlightable, IHasColor
    {
        // data
        private double[] xs;
        public double[] Xs { get => xs; private set { xs = value; OnPropertyChanged(); } }
        private double[] ys;
        public double[] Ys { get => ys; private set { ys = value; OnPropertyChanged(); } }
        private double[] xError;
        public double[] XError { get => xError; private set { xError = value; OnPropertyChanged(); } }
        private double[] yError;
        public double[] YError { get => yError; private set { yError = value; OnPropertyChanged(); } }

       //... 
    }

The extension of the IPlottable interface brings the option to write an UserControl supporting MVVM at least for the single plottables (currently doing this & willing to share the finished UC). For axes and plot MVVM, I need to check where the PropertyNotifier is needed.

Best Regards, Chris

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ChrisCC6commented, Apr 13, 2022

Hi Scott,

thank you for your quick reply. Give me a little time, I’m working on it… 😉

Best Regards Chris

1reaction
ChrisCC6commented, Apr 20, 2022

Hi @swharden,

I implemented the INotifyPropertyChanged interface, extended the WPF control and made a couple of changes to fit the MVVM pattern. Do you want to take a look:https://github.com/ChrisCC6/ScottPlot.

Maybe just download the code and try the MVVM example…

Best Regards, Chris

Read more comments on GitHub >

github_iconTop Results From Across the Web

MVVM and Data Binding - ScottPlot FAQ
MVVM and data binding patterns can be used to create graphical controls that wrap ScottPlot plots. Users who strongly desire to use data...
Read more >
MVVM pattern. · ScottPlot ScottPlot · Discussion #633
MVVM pattern. ... It is possible to create a MVVM control for ScottPlot (and not use the control provided by the ScottPlot.WPF package)....
Read more >
ScottPlot does not plot when data template is used
1 Answer. Scott Plot is not implemented as a proper WPF control that supports data-binding and MVVM.
Read more >
[Solved]-ScottPlot does not plot when data template is used-wpf
it's bad pattern bring control to vm, but it should work. as the author already states, this is a bad pattern as your...
Read more >
Patterns - WPF Apps With The Model-View-ViewModel ...
By the end of this article, it will be clear how data templates, commands, data binding, the resource system, and the MVVM pattern...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found