This is a continuation of this question.
I am trying to cover chart plot with an AnchorPane, i called it buffer.
Here is a code example.
package launcher;
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
NumberAxis numberAxis = new NumberAxis();
LineChart<Number, Number> chart = new LineChart<>(numberAxis, new NumberAxis());
chart.getYAxis().setSide(Side.RIGHT);
AnchorPane buffer = new AnchorPane();
buffer.setStyle("-fx-background-color: gray;" +
"-fx-opacity: 0.5");
AnchorPane anchorPane = new AnchorPane();
AnchorPane.setTopAnchor(chart, 0.0);
AnchorPane.setRightAnchor(chart, 0.0);
AnchorPane.setBottomAnchor(chart, 0.0);
AnchorPane.setLeftAnchor(chart, 0.0);
anchorPane.getChildren().addAll(chart, buffer);
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.show();
resetBuffer(chart, buffer, anchorPane);
anchorPane.widthProperty().addListener((observable, oldValue, newValue) -> {
resetBuffer(chart, buffer, anchorPane);
});
}
private void resetBuffer(LineChart<Number, Number> chart, AnchorPane buffer,
AnchorPane anchorPane) {
Node chartPlot = chart.lookup(".chart-plot-background");
Bounds bounds = anchorPane.sceneToLocal(
chartPlot.localToScene(chartPlot.getBoundsInLocal()));
buffer.setPrefWidth(bounds.getWidth());
buffer.setPrefHeight(bounds.getHeight());
AnchorPane.setLeftAnchor(buffer, bounds.getMinX());
AnchorPane.setTopAnchor(buffer, bounds.getMinY());
}
}
Everything works fine until I resize the stage. When I resize the AnchorPane (buffer) is not covering a chart plot properly. i.e. it is not fitting the size.
How to fix it or do it in proper way?
The JavaFX Anchor Pane is used for anchoring to an offset from an anchor pane edges by its child node edges. If the anchor pane has a border or padding set, the off sets is measuring from the inside corner edges of the insets.
A JavaFX Chart is a node designed to display data graphically. Charts display data as symbols such as bars (the BarChart ), lines ( LineChart ), slices ( PieChart ), points ( ScatterChart) or a combination of these.
These all extend the XYChart object, a sub-class of Chart, which is responsible for displaying the axes. Each of the chart instances is responsible for placing data points, connecting lines, bars and boxes. Charts without axes: the PieChart is the only chart that comes with JavaFX that doesn’t use axes.
setTopAnchor (Node t, Double value): setRightAnchor () method is used to set top anchor. getTopAnchor (Node t): getTopAnchor () method returns top anchor. How does AnchorPane work in JavaFX?
Hope this will solve it:
First solution:
double sideSizeWidth = (anchorPane.getWidth()-bounds.getWidth());
double sideSizeHeight= (anchorPane.getHeight()-bounds.getHeight());
buffer.prefWidthProperty().bind(Bindings.add(
chart.widthProperty(),
-sideSizeWidth
));
buffer.prefHeightProperty().bind(Bindings.add(
chart.heightProperty(),
-sideSizeHeight
));
(it need to be in resetBuffer
and it needs to call only one time at the beginning of course after primaryStage.show()
)
Second solution:
scene.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double dif =oldValue.doubleValue()-newValue.doubleValue();
buffer.setPrefWidth(buffer.getWidth()-(dif));
}
});
scene.heightProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double dif =oldValue.doubleValue()-newValue.doubleValue();
buffer.setPrefHeight(buffer.getHeight()-(dif));
}
});
it base on the fact that width and hight is permanent in the axis label and legend, so we just need to calculate the diffrent on the scene
size an apply on buffer
.
the all code:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
NumberAxis numberAxis = new NumberAxis();
LineChart<Number, Number> chart = new LineChart<>(numberAxis, new NumberAxis());
chart.getYAxis().setSide(Side.RIGHT);
AnchorPane buffer = new AnchorPane();
buffer.setStyle("-fx-background-color: gray;" +
"-fx-opacity: 0.5");
AnchorPane anchorPane = new AnchorPane();
AnchorPane.setTopAnchor(chart, 0.0);
AnchorPane.setRightAnchor(chart, 0.0);
AnchorPane.setBottomAnchor(chart, 0.0);
AnchorPane.setLeftAnchor(chart, 0.0);
anchorPane.getChildren().addAll(chart, buffer);
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.show();
resetBuffer(chart, buffer, anchorPane,scene);
}
private void resetBuffer(LineChart<Number, Number> chart, AnchorPane buffer,
AnchorPane anchorPane,Scene scene) {
Node chartPlot = chart.lookup(".chart-plot-background");
Bounds bounds = anchorPane.sceneToLocal(
chartPlot.localToScene(chartPlot.getBoundsInLocal()));
buffer.setPrefWidth(bounds.getWidth());
buffer.setPrefHeight(bounds.getHeight());
AnchorPane.setLeftAnchor(buffer, bounds.getMinX());
AnchorPane.setTopAnchor(buffer, bounds.getMinY());
scene.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double dif =oldValue.doubleValue()-newValue.doubleValue();
buffer.setPrefWidth(buffer.getWidth()-(dif));
}
});
scene.heightProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double dif =oldValue.doubleValue()-newValue.doubleValue();
buffer.setPrefHeight(buffer.getHeight()-(dif));
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With