ScottPlot / ScottPlot

Interactive plotting library for .NET
https://ScottPlot.net
MIT License
5.31k stars 862 forks source link

DataLogger: improve axis management for inverted axes #4513

Open Jofstera opened 6 hours ago

Jofstera commented 6 hours ago

Hi guys, is it possible to set a limit on the Y-axis when using a Datalogger? I know I can set a rule to lock the Y-axis and manually give it tick values, but doing so means the y-axis cannot be expanded dynamically.

ScottPlot Version: WPF SP5 v5.0.45

Code Sample:

    public partial class MainWindow : Window
    {
        readonly System.Windows.Forms.Timer AddNewDataTimer = new() { Interval = 50, Enabled = true };
        ScottPlot.Plottables.DataLogger Logger1;

        public MainWindow()
        {
            InitializeComponent();
            InitializeGraph();
        }

        private void InitializeGraph()
        {
            myPlot.Plot.Axes.InvertY();

            Logger1 = myPlot.Plot.Add.DataLogger();
            Logger1.Axes.YAxis = myPlot.Plot.Axes.Left;

            // Attempting to set some kind of limit ...
            myPlot.Plot.Axes.MarginsY(0);
            myPlot.Plot.Axes.SetLimitsY(0, 50);
            Logger1.Axes.YAxis.Range.Set(0, 50);

            DateTime startOfGraphTime = DateTime.Now;
            Random numGenerator = new Random();
            AddNewDataTimer.Tick += (s, e) =>
            {
                // Attemping to set some kind of limit ...
                myPlot.Plot.Axes.Left.Min = 0;
                Logger1.Axes.YAxis.Min = 0;       

                TimeSpan timeSinceStart = DateTime.Now - startOfGraphTime;
                double seconds = timeSinceStart.TotalSeconds;
                Logger1.Add(seconds, numGenerator.Next(0, 50));
                myPlot.Refresh();
            };
        }
    }

image

  1. In this example it is impossible to get a value under 0 or over 50, yet the y-axis shows values outside of this range which are not needed.

  2. I inverted the y-axis with myPlot.Plot.Axes.InvertY();. But as soon as a DataLogger is added to the plot, the Y-axis becomes uninverted. Is there a way to fix this?

swharden commented 3 hours ago

Hi @Jofstera, thanks for these questions! I really appreciate the complete code example that helped me to get started answering these right away 😎

How to change vertical padding around Data Logger values

In this example it is impossible to get a value under 0 or over 50, yet the y-axis shows values outside of this range which are not needed.

Users may create their own classes that manage axis limits to achieve fully custom behavior. Luckily the built-in default axis manager already has an exposed property that can be used to restrict "padding" around values vertically:

Logger1.AxisManager = new ScottPlot.AxisLimitManagers.Full()
{
    ExpansionRatio = 0
};

How to use Data Logger with inverted axis limits

I inverted the y-axis with myPlot.Plot.Axes.InvertY();. But as soon as a DataLogger is added to the plot, the Y-axis becomes uninverted. Is there a way to fix this?

Oof, this sounds like it may be a bug! Thanks for reporting it. I'm running out of time to take a look at it myself today, but I'm guessing the accidental reversion is happening in one of these areas. I'll leave this issue open to track a fix, but if you're able to get to it before I can I would be happy to review a pull request within the next day or two! 🚀

https://github.com/ScottPlot/ScottPlot/blob/1f44931697efb5fcc0c8589057039256522e3ee9/src/ScottPlot5/ScottPlot5/AxisLimitManagers/Full.cs#L28-L45

https://github.com/ScottPlot/ScottPlot/blob/1f44931697efb5fcc0c8589057039256522e3ee9/src/ScottPlot5/ScottPlot5/Plottables/DataLogger.cs#L222-L242