Skip to content

Commit

Permalink
minor: made drawing of grid on top/bottom CSS more easily configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
RalphSteinhagen committed Sep 14, 2019
1 parent 6bb9276 commit 4d8ebc3
Showing 1 changed file with 16 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,18 @@ public class GridRenderer extends Pane implements Renderer {
private static final String STYLE_CLASS_MINOR_GRID_LINE = "chart-minor-grid-lines";
private static final String STYLE_CLASS_MINOR_GRID_LINE_H = "chart-minor-horizontal-lines";
private static final String STYLE_CLASS_MINOR_GRID_LINE_V = "chart-minor-vertical-lines";
private static final String STYLE_CLASS_GRID_ON_TOP = "chart-grid-line-on-top";
private static final PseudoClass SELECTED_PSEUDO_CLASS = PseudoClass.getPseudoClass("withMinor");
private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
static {
final List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<>(Region.getClassCssMetaData());
styleables.add(StyleableProperties.GRID_ON_TOP);
STYLEABLES = Collections.unmodifiableList(styleables);
}

private static final double[] DEFAULT_GRID_DASH_PATTERM = { 4.5, 2.5 };
protected final Chart baseChart;
protected final BooleanProperty drawGridOnTop = new SimpleStyleableBooleanProperty(StyleableProperties.GRID_ON_TOP,
this, "drawGridOnTop", true);
// protected final BooleanProperty drawGridOnTop = new SimpleStyleableBooleanProperty(StyleableProperties.GRID_ON_TOP,
// this, "drawGridOnTop", true);
private final Line horMajorGridStyleNode;
private final Line verMajorGridStyleNode;
private final Line horMinorGridStyleNode;
private final Line verMinorGridStyleNode;
private final Line drawGridOnTopNode;
private final Group gridStyleNodes = new Group();
protected final ObservableList<Axis> axesList = FXCollections.observableArrayList();

Expand Down Expand Up @@ -92,8 +88,13 @@ public GridRenderer(final XYChart chart) {
verMinorGridStyleNode.getStyleClass().add(GridRenderer.STYLE_CLASS_MINOR_GRID_LINE_V);
verMinorGridStyleNode.setVisible(false);

drawGridOnTopNode = new Line();
drawGridOnTopNode.getStyleClass().add(GridRenderer.STYLE_CLASS_GRID_ON_TOP);
drawGridOnTopNode.getStyleClass().add(GridRenderer.STYLE_CLASS_GRID_ON_TOP);
drawGridOnTopNode.setVisible(true);

gridStyleNodes.getChildren().addAll(horMajorGridStyleNode, verMajorGridStyleNode, horMinorGridStyleNode,
verMinorGridStyleNode);
verMinorGridStyleNode, drawGridOnTopNode);

getChildren().add(gridStyleNodes);
final Scene scene = new Scene(this);
Expand All @@ -104,12 +105,15 @@ public GridRenderer(final XYChart chart) {
verMajorGridStyleNode.getPseudoClassStates().addListener(listener);
horMinorGridStyleNode.getPseudoClassStates().addListener(listener);
verMinorGridStyleNode.getPseudoClassStates().addListener(listener);
drawGridOnTopNode.getPseudoClassStates().addListener(listener);

ChangeListener<? super Boolean> change = (ob, o, n) -> {
horMajorGridStyleNode.pseudoClassStateChanged(GridRenderer.SELECTED_PSEUDO_CLASS,
horMinorGridStyleNode.isVisible());
verMajorGridStyleNode.pseudoClassStateChanged(GridRenderer.SELECTED_PSEUDO_CLASS,
verMinorGridStyleNode.isVisible());
drawGridOnTopNode.pseudoClassStateChanged(GridRenderer.SELECTED_PSEUDO_CLASS,
drawGridOnTopNode.isVisible());
chart.requestLayout();
};

Expand All @@ -118,10 +122,6 @@ public GridRenderer(final XYChart chart) {
drawOnTopProperty().addListener(change);
}

public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
return GridRenderer.STYLEABLES;
}

private static double snap(final double value) {
return (int) value + 0.5;
}
Expand Down Expand Up @@ -149,7 +149,7 @@ public Canvas drawLegendSymbol(DataSet dataSet, int dsIndex, int width, int heig
* @return drawOnTop property
*/
public final BooleanProperty drawOnTopProperty() {
return drawGridOnTop;
return drawGridOnTopNode.visibleProperty();
}

/**
Expand Down Expand Up @@ -235,7 +235,7 @@ public final BooleanProperty horizontalMinorGridLinesVisibleProperty() {
* @return drawOnTop state
*/
public final boolean isDrawOnTop() {
return drawGridOnTop.get();
return drawGridOnTopNode.isVisible();
}

@Override
Expand All @@ -261,7 +261,7 @@ public void render(final GraphicsContext gc, final Chart chart, final int dataSe
* @param state true: draw on top
*/
public final void setDrawOnTop(boolean state) {
drawGridOnTop.set(state);
drawGridOnTopNode.setVisible(state);
}

@Override
Expand Down Expand Up @@ -489,21 +489,4 @@ protected void drawVerticalMinorGridLines(final GraphicsContext gc, final Axis x
}
}

private static class StyleableProperties {

private static final CssMetaData<GridRenderer, Boolean> GRID_ON_TOP = new CssMetaData<GridRenderer, Boolean>(
"-fx-grid-on-top", BooleanConverter.getInstance(), Boolean.TRUE, false) {

@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Boolean> getStyleableProperty(final GridRenderer node) {
return (StyleableProperty<Boolean>) node.drawGridOnTop;
}

@Override
public boolean isSettable(final GridRenderer node) {
return node.drawGridOnTop == null || !node.drawGridOnTop.isBound();
}
};
}
}

0 comments on commit 4d8ebc3

Please sign in to comment.