From f8813bda36dae4b7d282e4411f9256a4bc99ad3e Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 21 Jan 2022 20:27:46 -0500 Subject: [PATCH] Fit axes to series on vs graph double click --- src/client/spark/AW2/View/VolSurfaceView.xaml | 3 +- .../spark/AW2/View/VolSurfaceView.xaml.cs | 48 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/client/spark/AW2/View/VolSurfaceView.xaml b/src/client/spark/AW2/View/VolSurfaceView.xaml index daffe4a87..ae7a99795 100644 --- a/src/client/spark/AW2/View/VolSurfaceView.xaml +++ b/src/client/spark/AW2/View/VolSurfaceView.xaml @@ -420,7 +420,8 @@ x:Name="vol_curve_plot_view" Grid.Row="2" Grid.ColumnSpan="3" - Model="{Binding graph_vm.plot_model}" /> + Model="{Binding graph_vm.plot_model}" + MouseDoubleClick="_on_graph_mouse_double_click" /> diff --git a/src/client/spark/AW2/View/VolSurfaceView.xaml.cs b/src/client/spark/AW2/View/VolSurfaceView.xaml.cs index f50e1c93d..788f3a795 100644 --- a/src/client/spark/AW2/View/VolSurfaceView.xaml.cs +++ b/src/client/spark/AW2/View/VolSurfaceView.xaml.cs @@ -1,4 +1,4 @@ -namespace mbts.ui.shell { +namespace mbts.ui.shell { using bts.utils; using mbts.spark; @@ -47,6 +47,46 @@ namespace mbts.ui.shell { } #endregion + public void fit_axes_to_series() { + if (_vol_curve_graph_vm?.axes?.Count != 2) { + return; + } + + var vis_series = _vol_curve_graph_vm.flat_visible_series.Where(s => s.points.Any()).ToList(); + if (!vis_series.Any()) { + return; + } + + Type tx = vis_series.Select(s => s.points.First().x.GetType()).Distinct().SingleOrDefault(); + if (tx != typeof(double)) { + return; + } + Type ty = vis_series.Select(s => s.points.First().y.GetType()).Distinct().SingleOrDefault(); + if (ty != typeof(double)) { + return; + } + + if (_vol_curve_graph_vm.axes.TryGetFirst(a => a.position.is_horizontal(), out var x_axis) && + _vol_curve_graph_vm.axes.TryGetFirst(a => a.position.is_vertical(), out var y_axis)) { + var points = vis_series.SelectMany(vs => vs.points); + var x_points = points.Select(p => (double)p.x); + if (x_points.Except(double.NaN).MinMax(out var x_min, out var x_max) && x_min < x_max) { + double padding = (x_max - x_min) * 0.05; + x_axis.minimum = x_min - padding; + x_axis.maximum = x_max + padding; + } + + var y_points = points.Select(p => (double)p.y); + if (y_points.Except(double.NaN).MinMax(out var y_min, out var y_max) && y_min < y_max) { + double padding = (y_max - y_min) * 0.05; + y_axis.minimum = y_min - padding; + y_axis.maximum = y_max + padding; + } + } + + _vol_curve_graph_vm?.reset_all_axes(); + } + void _on_is_legend_visible_changed(bool value) { Disposable.dispose(ref _legend_popup); if (!value) { @@ -77,6 +117,12 @@ namespace mbts.ui.shell { _legend_popup = Disposable.Create(() => popup_adorner.detach()); } + void _on_graph_mouse_double_click(object sender, MouseButtonEventArgs e) { + fit_axes_to_series(); + vol_curve_plot_view.InvalidatePlot(); + e.Handled = true; + } + private void vol_time_popup_button_Loaded(object sender, RoutedEventArgs e) { // don't initialize more than once if (_time_editor_popup != null) return;