From 7e62da24718d90f9948e1e892cf024b3e41189a6 Mon Sep 17 00:00:00 2001 From: glopesdev Date: Mon, 12 Jun 2023 15:41:57 +0100 Subject: [PATCH] Make all graph visualizers buffered --- Bonsai.Design.Visualizers/BarGraphVisualizer.cs | 2 +- Bonsai.Design.Visualizers/GraphHelper.cs | 9 +++++++-- Bonsai.Design.Visualizers/LineGraphVisualizer.cs | 2 +- Bonsai.Design.Visualizers/RollingGraphBuilder.cs | 12 +++++++++--- Bonsai.Design.Visualizers/RollingGraphVisualizer.cs | 11 ++++++++--- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Bonsai.Design.Visualizers/BarGraphVisualizer.cs b/Bonsai.Design.Visualizers/BarGraphVisualizer.cs index b56df3026..04a4f9d4a 100644 --- a/Bonsai.Design.Visualizers/BarGraphVisualizer.cs +++ b/Bonsai.Design.Visualizers/BarGraphVisualizer.cs @@ -8,7 +8,7 @@ namespace Bonsai.Design.Visualizers /// /// Provides a type visualizer to display an object as a bar graph. /// - public class BarGraphVisualizer : DialogTypeVisualizer + public class BarGraphVisualizer : BufferedVisualizer { BarGraphBuilder.VisualizerController controller; BarGraphView view; diff --git a/Bonsai.Design.Visualizers/GraphHelper.cs b/Bonsai.Design.Visualizers/GraphHelper.cs index bc1d677ca..e7c8b5476 100644 --- a/Bonsai.Design.Visualizers/GraphHelper.cs +++ b/Bonsai.Design.Visualizers/GraphHelper.cs @@ -79,6 +79,11 @@ static bool IsIntegralType(Type type) } internal static Expression SelectIndexMember(Expression expression, string indexSelector, out string indexLabel) + { + return SelectIndexMember(time: null, expression, indexSelector, out indexLabel); + } + + internal static Expression SelectIndexMember(Expression time, Expression expression, string indexSelector, out string indexLabel) { Expression selectedIndex; if (!string.IsNullOrEmpty(indexSelector)) @@ -88,14 +93,14 @@ internal static Expression SelectIndexMember(Expression expression, string index } else { - selectedIndex = Expression.Property(null, typeof(DateTime), nameof(DateTime.Now)); + selectedIndex = time ?? Expression.Property(null, typeof(DateTime), nameof(DateTime.Now)); indexLabel = "Time"; } if (selectedIndex.Type == typeof(DateTimeOffset)) selectedIndex = Expression.Property(selectedIndex, nameof(DateTimeOffset.DateTime)); if (selectedIndex.Type == typeof(DateTime)) { - selectedIndex = Expression.Convert(selectedIndex, typeof(ZedGraph.XDate)); + selectedIndex = Expression.Convert(selectedIndex, typeof(XDate)); } return selectedIndex; diff --git a/Bonsai.Design.Visualizers/LineGraphVisualizer.cs b/Bonsai.Design.Visualizers/LineGraphVisualizer.cs index dcd95bca5..9d9c8ac8b 100644 --- a/Bonsai.Design.Visualizers/LineGraphVisualizer.cs +++ b/Bonsai.Design.Visualizers/LineGraphVisualizer.cs @@ -8,7 +8,7 @@ namespace Bonsai.Design.Visualizers /// /// Provides a type visualizer to display an object as a line graph. /// - public class LineGraphVisualizer : DialogTypeVisualizer + public class LineGraphVisualizer : BufferedVisualizer { LineGraphBuilder.VisualizerController controller; LineGraphView view; diff --git a/Bonsai.Design.Visualizers/RollingGraphBuilder.cs b/Bonsai.Design.Visualizers/RollingGraphBuilder.cs index 7cd6e95c7..21a101f19 100644 --- a/Bonsai.Design.Visualizers/RollingGraphBuilder.cs +++ b/Bonsai.Design.Visualizers/RollingGraphBuilder.cs @@ -101,7 +101,7 @@ internal class VisualizerController internal string[] ValueLabels; internal SymbolType SymbolType; internal float LineWidth; - internal Action AddValues; + internal Action AddValues; } /// @@ -113,6 +113,7 @@ public override Expression Build(IEnumerable arguments) { var source = arguments.First(); var parameterType = source.Type.GetGenericArguments()[0]; + var timeParameter = Expression.Parameter(typeof(DateTime)); var valueParameter = Expression.Parameter(typeof(object)); var viewParameter = Expression.Parameter(typeof(RollingGraphVisualizer)); var elementVariable = Expression.Variable(parameterType); @@ -125,7 +126,7 @@ public override Expression Build(IEnumerable arguments) LineWidth = LineWidth }; - var selectedIndex = GraphHelper.SelectIndexMember(elementVariable, IndexSelector, out Controller.IndexLabel); + var selectedIndex = GraphHelper.SelectIndexMember(timeParameter, elementVariable, IndexSelector, out Controller.IndexLabel); Controller.IndexType = selectedIndex.Type; if (selectedIndex.Type != typeof(double) && selectedIndex.Type != typeof(string)) { @@ -136,7 +137,12 @@ public override Expression Build(IEnumerable arguments) var showBody = Expression.Block(new[] { elementVariable }, Expression.Assign(elementVariable, Expression.Convert(valueParameter, parameterType)), Expression.Call(viewParameter, nameof(RollingGraphVisualizer.AddValues), null, selectedIndex, selectedValues)); - Controller.AddValues = Expression.Lambda>(showBody, valueParameter, viewParameter).Compile(); + Controller.AddValues = Expression.Lambda>( + showBody, + timeParameter, + valueParameter, + viewParameter) + .Compile(); return Expression.Call(typeof(RollingGraphBuilder), nameof(Process), new[] { parameterType }, source); } diff --git a/Bonsai.Design.Visualizers/RollingGraphVisualizer.cs b/Bonsai.Design.Visualizers/RollingGraphVisualizer.cs index 7fc373eca..009fb324e 100644 --- a/Bonsai.Design.Visualizers/RollingGraphVisualizer.cs +++ b/Bonsai.Design.Visualizers/RollingGraphVisualizer.cs @@ -8,7 +8,7 @@ namespace Bonsai.Design.Visualizers /// /// Provides a type visualizer to display an object as a rolling graph. /// - public class RollingGraphVisualizer : DialogTypeVisualizer + public class RollingGraphVisualizer : BufferedVisualizer { static readonly TimeSpan TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 30); RollingGraphBuilder.VisualizerController controller; @@ -127,8 +127,13 @@ public override void Load(IServiceProvider provider) /// public override void Show(object value) { - var time = DateTime.Now; - controller.AddValues(value, this); + Show(DateTime.Now, value); + } + + /// + protected override void Show(DateTime time, object value) + { + controller.AddValues(time, value, this); if ((time - updateTime) > TargetElapsedTime) { view.Graph.Invalidate();