Skip to content

Commit ccdd3d5

Browse files
Fixed legend layout.
1 parent 2dc8cd6 commit ccdd3d5

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

charts/src/main/java/com/puresoltechnologies/javafx/charts/ChartView.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import javafx.collections.ObservableList;
2222
import javafx.geometry.HPos;
2323
import javafx.geometry.Insets;
24+
import javafx.geometry.Orientation;
2425
import javafx.geometry.Pos;
2526
import javafx.geometry.VPos;
2627
import javafx.scene.control.ContextMenu;
@@ -71,7 +72,7 @@ public enum LegendPosition {
7172
private final BorderPane plotBorderPane = new BorderPane();
7273
private final Label titleLabel = new Label();
7374
private final Label subTitleLabel = new Label();
74-
private final LegendPane legendPane = new LegendPane(plotCanvas.getPlots());
75+
private final LegendPane legendPane = new LegendPane(this, plotCanvas.getPlots());
7576

7677
public ChartView(String title) {
7778
this();
@@ -107,7 +108,7 @@ public ChartView() {
107108
getChildren().addAll(titleLabel, subTitleLabel, plotBorderPane);
108109
setMaxHeight(Double.MAX_VALUE);
109110
setMaxWidth(Double.MAX_VALUE);
110-
setGridLinesVisible(true);
111+
setGridLinesVisible(false);
111112
}
112113

113114
private void configureLegendTable() {
@@ -120,15 +121,19 @@ private void applyLegend() {
120121
switch (legendPosition.get()) {
121122
case LEFT:
122123
plotBorderPane.setLeft(legendPane);
124+
legendPane.setOrientation(Orientation.VERTICAL);
123125
break;
124126
case RIGHT:
125127
plotBorderPane.setRight(legendPane);
128+
legendPane.setOrientation(Orientation.VERTICAL);
126129
break;
127130
case TOP:
128131
plotBorderPane.setTop(legendPane);
132+
legendPane.setOrientation(Orientation.HORIZONTAL);
129133
break;
130134
case BOTTOM:
131135
plotBorderPane.setBottom(legendPane);
136+
legendPane.setOrientation(Orientation.HORIZONTAL);
132137
break;
133138
default:
134139
// intentionally left empty

charts/src/main/java/com/puresoltechnologies/javafx/charts/LegendPane.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,38 @@
55
import javafx.collections.ListChangeListener;
66
import javafx.collections.ObservableList;
77
import javafx.geometry.HPos;
8+
import javafx.geometry.Insets;
89
import javafx.geometry.VPos;
910
import javafx.scene.control.Label;
10-
import javafx.scene.layout.FlowPane;
11+
import javafx.scene.layout.Background;
12+
import javafx.scene.layout.BackgroundFill;
13+
import javafx.scene.layout.CornerRadii;
1114
import javafx.scene.layout.GridPane;
1215
import javafx.scene.layout.Pane;
1316
import javafx.scene.layout.Priority;
17+
import javafx.scene.layout.TilePane;
1418

15-
public class LegendPane extends FlowPane {
19+
public class LegendPane extends TilePane {
1620

17-
public LegendPane(ObservableList<Plot<?, ?, ?>> plots) {
21+
private final ChartView chartView;
22+
23+
public LegendPane(ChartView chartView, ObservableList<Plot<?, ?, ?>> plots) {
24+
this.chartView = chartView;
25+
setPadding(new Insets(5.0));
26+
setHgap(5.0);
27+
setVgap(5.0);
1828
plots.addListener((ListChangeListener<Plot<?, ?, ?>>) (change) -> {
1929
showSymbols(change.getList());
2030
});
31+
chartView.backgroundColorProperty().addListener((observable, oldValue, newValue) -> {
32+
updateBackground();
33+
});
34+
updateBackground();
35+
}
36+
37+
private void updateBackground() {
38+
setBackground(
39+
new Background(new BackgroundFill(chartView.getBackgroundColor(), CornerRadii.EMPTY, Insets.EMPTY)));
2140
}
2241

2342
private void showSymbols(ObservableList<? extends Plot<?, ?, ?>> plots) {

0 commit comments

Comments
 (0)