From e63d77117c61cadaff6d06f1e8e26cfa09e840d4 Mon Sep 17 00:00:00 2001 From: Alexander Krimm Date: Thu, 17 Aug 2023 15:00:59 +0200 Subject: [PATCH] Chart: position plugins using Panes instead of Groups It seems that groups are not well suited to positioning nodes on top of the graph. Replacing the Groups for the Plugins by Panes fixes an issue where the zoom rectangle would shift to the upper left corner of the chart area whenever there was a relayout while a rectangle zoom was ongoing. --- .../main/java/io/fair_acc/chartfx/Chart.java | 33 +++++++------------ .../io/fair_acc/chartfx/utils/FXUtils.java | 7 ++++ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/Chart.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/Chart.java index 919df80f0..862aba28a 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/Chart.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/Chart.java @@ -27,7 +27,6 @@ import javafx.css.*; import javafx.geometry.*; import javafx.scene.CacheHint; -import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.canvas.Canvas; import javafx.scene.control.Control; @@ -99,17 +98,16 @@ public abstract class Chart extends Region implements EventSource { protected final ChartLayoutAnimator animator = new ChartLayoutAnimator(this); protected final ObservableList axesList = FXCollections.observableList(new NoDuplicatesList<>()); - private final Map pluginGroups = new HashMap<>(); + private final Map pluginPanes = new HashMap<>(); private final ObservableList plugins = FXCollections.observableList(new LinkedList<>()); private final ObservableList datasets = FXCollections.observableArrayList(); protected final ObservableList allDataSets = FXCollections.observableArrayList(); private final ObservableList renderers = FXCollections.observableArrayList(); - // Inner canvas for the drawn content protected final ResizableCanvas canvas = StyleUtil.addStyles(new ResizableCanvas(), "chart-canvas"); - protected final Pane canvasForeground = new Pane(); - protected final Group pluginsArea = Chart.createChildGroup(); + protected final Pane canvasForeground = FXUtils.createUnmanagedPane(); + protected final Pane pluginsArea = FXUtils.createUnmanagedPane(); // Area where plots get drawn protected final Pane plotBackground = StyleUtil.addStyles(new Pane(), "chart-plot-background"); @@ -814,9 +812,9 @@ protected void layoutPluginsChildren() { protected void pluginAdded(final ChartPlugin plugin) { plugin.setChart(Chart.this); - final Group group = Chart.createChildGroup(); - Bindings.bindContent(group.getChildren(), plugin.getChartChildren()); - pluginGroups.put(plugin, group); + final Pane pane = FXUtils.createUnmanagedPane(); + Bindings.bindContent(pane.getChildren(), plugin.getChartChildren()); + pluginPanes.put(plugin, pane); } // -------------- STYLESHEET HANDLING @@ -824,10 +822,10 @@ protected void pluginAdded(final ChartPlugin plugin) { protected void pluginRemoved(final ChartPlugin plugin) { plugin.setChart(null); - final Group group = pluginGroups.remove(plugin); - Bindings.unbindContent(group, plugin.getChartChildren()); - group.getChildren().clear(); - pluginsArea.getChildren().remove(group); + final Pane pane = pluginPanes.remove(plugin); + Bindings.unbindContent(pane, plugin.getChartChildren()); + pane.getChildren().clear(); + pluginsArea.getChildren().remove(pane); } protected void pluginsChanged(final ListChangeListener.Change change) { @@ -905,7 +903,7 @@ protected void updateLegend(final List dataSets, final List r } protected void updatePluginsArea() { - var pluginChildren = plugins.stream().map(pluginGroups::get).collect(Collectors.toList()); + var pluginChildren = plugins.stream().map(pluginPanes::get).collect(Collectors.toList()); pluginsArea.getChildren().setAll(pluginChildren); fireInvalidated(ChartBits.ChartPlugins); } @@ -927,13 +925,4 @@ private void ensureJavaFxPulse() { public static List> getClassCssMetaData() { return CSS.getCssMetaData(); } - - protected static Group createChildGroup() { - final Group group = new Group(); - group.setManaged(false); - group.setAutoSizeChildren(false); - group.relocate(0, 0); - return group; - } - } diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/utils/FXUtils.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/utils/FXUtils.java index 588989a9d..b1cf5d9bd 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/utils/FXUtils.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/utils/FXUtils.java @@ -21,6 +21,7 @@ import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.layout.Pane; import javafx.stage.Window; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -390,4 +391,10 @@ public static void registerLayoutHooks(Node node, Runnable preLayoutAction, Runn }); } + public static Pane createUnmanagedPane() { + final Pane pane = new Pane(); + pane.setManaged(false); + pane.relocate(0, 0); + return pane; + } }