From e11d86f80557e55473374bac5538d07f4c0542ca Mon Sep 17 00:00:00 2001 From: lordmortis Date: Tue, 7 Apr 2020 17:09:25 +0800 Subject: [PATCH] Fixed plotting (#226) Thanks to @lordmortis --- LibreHardwareMonitor/UI/PlotPanel.cs | 60 +++++++++++++++---- .../Utilities/PersistentSettings.cs | 16 +++-- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/LibreHardwareMonitor/UI/PlotPanel.cs b/LibreHardwareMonitor/UI/PlotPanel.cs index a26c94f..9b5bb67 100644 --- a/LibreHardwareMonitor/UI/PlotPanel.cs +++ b/LibreHardwareMonitor/UI/PlotPanel.cs @@ -12,6 +12,7 @@ using System.Windows.Forms; using LibreHardwareMonitor.Hardware; using LibreHardwareMonitor.Utilities; using OxyPlot; +using OxyPlot.Annotations; using OxyPlot.Axes; using OxyPlot.WindowsForms; using OxyPlot.Series; @@ -26,6 +27,7 @@ namespace LibreHardwareMonitor.UI private readonly PlotModel _model; private readonly TimeSpanAxis _timeAxis = new TimeSpanAxis(); private readonly SortedDictionary _axes = new SortedDictionary(); + private readonly Dictionary _annotations = new Dictionary(); private UserOption _stackedAxes; private UserOption _showAxesLabels; private UserOption _timeAxisEnableZoom; @@ -45,7 +47,7 @@ namespace LibreHardwareMonitor.UI _model = CreatePlotModel(); _plot = new PlotView { Dock = DockStyle.Fill, Model = _model, BackColor = Color.White, ContextMenu = CreateMenu() }; - + UpdateAxesPosition(); SuspendLayout(); @@ -173,6 +175,7 @@ namespace LibreHardwareMonitor.UI foreach (SensorType type in Enum.GetValues(typeof(SensorType))) { + string typeName = type.ToString(); var axis = new LinearAxis { Position = AxisPosition.Left, @@ -183,15 +186,31 @@ namespace LibreHardwareMonitor.UI MinorGridlineThickness = 1, MinorGridlineColor = _timeAxis.MinorGridlineColor, AxislineStyle = LineStyle.Solid, - Title = type.ToString(), - Key = type.ToString() + Title = typeName, + Key = typeName, }; + var annotation = new LineAnnotation + { + Type = LineAnnotationType.Horizontal, + ClipByXAxis = false, + ClipByYAxis = false, + LineStyle = LineStyle.Solid, + Color = OxyColors.Black, + YAxisKey = typeName, + StrokeThickness = 2, + }; + + axis.AxisChanged += (sender, args) => annotation.Y = axis.ActualMinimum; + axis.TransformChanged += (sender, args) => annotation.Y = axis.ActualMinimum; + axis.Zoom(_settings.GetValue("plotPanel.Min" + axis.Key, float.NaN), _settings.GetValue("plotPanel.Max" + axis.Key, float.NaN)); if (units.ContainsKey(type)) axis.Unit = units[type]; + _axes.Add(type, axis); + _annotations.Add(type, annotation); } var model = new ScaledPlotModel(_dpiXScale, _dpiYScale); @@ -228,10 +247,10 @@ namespace LibreHardwareMonitor.UI public void SetSensors(List sensors, IDictionary colors) { _model.Series.Clear(); - var types = new System.Collections.Generic.HashSet(); + var types = new HashSet(); - Func createDataPoint = (SensorType type, SensorValue value) => + DataPoint CreateDataPoint(SensorType type, SensorValue value) { float displayedValue; @@ -243,18 +262,22 @@ namespace LibreHardwareMonitor.UI { displayedValue = value.Value; } + return new DataPoint((_now - value.Time).TotalSeconds, displayedValue); - }; + } + foreach (ISensor sensor in sensors) { - var series = new LineSeries(); + var series = new LineSeries + { + ItemsSource = sensor.Values.Select(value => CreateDataPoint(sensor.SensorType, value)), + Color = colors[sensor].ToOxyColor(), + StrokeThickness = 1, + YAxisKey = _axes[sensor.SensorType].Key, + Title = sensor.Hardware.Name + " " + sensor.Name + }; - series.ItemsSource = sensor.Values.Select(value => createDataPoint(sensor.SensorType, value)); - series.Color = colors[sensor].ToOxyColor(); - series.StrokeThickness = 1; - series.YAxisKey = _axes[sensor.SensorType].Key; - series.Title = sensor.Hardware.Name + " " + sensor.Name; _model.Series.Add(series); types.Add(sensor.SensorType); @@ -287,6 +310,10 @@ namespace LibreHardwareMonitor.UI axis.PositionTier = 0; axis.MajorGridlineStyle = LineStyle.Solid; axis.MinorGridlineStyle = LineStyle.Solid; + LineAnnotation annotation = _annotations[pair.Key]; + annotation.Y = axis.ActualMinimum; + if (!_model.Annotations.Contains(annotation)) + _model.Annotations.Add(annotation); } } else @@ -312,6 +339,9 @@ namespace LibreHardwareMonitor.UI } axis.MajorGridlineStyle = LineStyle.None; axis.MinorGridlineStyle = LineStyle.None; + LineAnnotation annotation = _annotations[pair.Key]; + if (_model.Annotations.Contains(annotation)) + _model.Annotations.Remove(_annotations[pair.Key]); } } } @@ -328,6 +358,12 @@ namespace LibreHardwareMonitor.UI SensorType type = pair.Key; if (type == SensorType.Temperature) axis.Unit = _unitManager.TemperatureUnit == TemperatureUnit.Celsius ? "°C" : "°F"; + + if (!_stackedAxes.Value) + continue; + + var annotation = _annotations[pair.Key]; + annotation.Y = axis.ActualMaximum; } } diff --git a/LibreHardwareMonitor/Utilities/PersistentSettings.cs b/LibreHardwareMonitor/Utilities/PersistentSettings.cs index 16737aa..7be9545 100644 --- a/LibreHardwareMonitor/Utilities/PersistentSettings.cs +++ b/LibreHardwareMonitor/Utilities/PersistentSettings.cs @@ -180,14 +180,23 @@ namespace LibreHardwareMonitor.Utilities { if (float.TryParse(str, NumberStyles.Float, CultureInfo.InvariantCulture, out float parsedValue)) return parsedValue; + } + return value; - return value; + } + + public double GetValue(string name, double value) + { + if (_settings.TryGetValue(name, out string str)) + { + if (double.TryParse(str, NumberStyles.Float, CultureInfo.InvariantCulture, out double parsedValue)) + return parsedValue; } return value; } - + public void SetValue(string name, bool value) { _settings[name] = value ? "true" : "false"; @@ -214,9 +223,6 @@ namespace LibreHardwareMonitor.Utilities { if (int.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int parsedValue)) return Color.FromArgb(parsedValue); - - - return value; } return value;