Skip to content

Commit

Permalink
Chart: position plugins using Panes instead of Groups
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wirew0rm committed Aug 17, 2023
1 parent a6d1596 commit e63d771
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
33 changes: 11 additions & 22 deletions chartfx-chart/src/main/java/io/fair_acc/chartfx/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,17 +98,16 @@ public abstract class Chart extends Region implements EventSource {
protected final ChartLayoutAnimator animator = new ChartLayoutAnimator(this);

protected final ObservableList<Axis> axesList = FXCollections.observableList(new NoDuplicatesList<>());
private final Map<ChartPlugin, Group> pluginGroups = new HashMap<>();
private final Map<ChartPlugin, Pane> pluginPanes = new HashMap<>();
private final ObservableList<ChartPlugin> plugins = FXCollections.observableList(new LinkedList<>());
private final ObservableList<DataSet> datasets = FXCollections.observableArrayList();
protected final ObservableList<DataSet> allDataSets = FXCollections.observableArrayList();
private final ObservableList<Renderer> 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");
Expand Down Expand Up @@ -814,20 +812,20 @@ 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
// ------------------------------------------------------------------------------

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<? extends ChartPlugin> change) {
Expand Down Expand Up @@ -905,7 +903,7 @@ protected void updateLegend(final List<DataSet> dataSets, final List<Renderer> 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);
}
Expand All @@ -927,13 +925,4 @@ private void ensureJavaFxPulse() {
public static List<CssMetaData<? extends Styleable, ?>> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit e63d771

Please sign in to comment.