Skip to content

Commit

Permalink
added a check that hides all tick labels that would get cut off by th…
Browse files Browse the repository at this point in the history
…e parent container
  • Loading branch information
ennerf committed Aug 9, 2023
1 parent cf2cacc commit 80f1aeb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import javafx.scene.CacheHint;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
Expand Down Expand Up @@ -542,11 +542,18 @@ private boolean isTickLabelRendered() {
}

private void applyOverlapPolicy(List<TickMark> tickMarks) {
// Default to all visible
if (tickMarks.isEmpty()) {
return;
}

// Start with everything being visible
for (TickMark tickMark : tickMarks) {
tickMark.setVisible(true);
}

// Hide labels that would get cut off by the parent
hideLabelsOutsideParentBounds(tickMarks);

// Check whether any labels overlap.
// Note: We technically only need to compute it for cases that
// hide/modify labels, but we leave it in for diagnostics.
Expand All @@ -573,7 +580,8 @@ private void applyOverlapPolicy(List<TickMark> tickMarks) {

case SKIP_ALT:
// make every other label visible to gain a factor 2 margin
for (int i = 1; i < tickMarks.size(); i += 2) {
int firstHidden = !tickMarks.get(0).isVisible() ? 0 : 1;
for (int i = firstHidden; i < tickMarks.size(); i += 2) {
tickMarks.get(i).setVisible(false);
}
break;
Expand All @@ -590,6 +598,43 @@ private void applyOverlapPolicy(List<TickMark> tickMarks) {

}

/**
* Hides labels that can't be fully displayed due to the
* bounds of the parent container. This often manifests
* on the top of y axes when there is no title padding.
* <p>
* Note that the layout bounds may not be available
* until the bounds phase, so we use layout x/y directly.
*/
private void hideLabelsOutsideParentBounds(List<TickMark> tickMarks) {
if (getParent() == null || !(getParent() instanceof Pane)) {
return;
}
if (getSide().isHorizontal()) {
final double offset = getLayoutX();
final double parentWidth = ((Pane) getParent()).getWidth();
for (TickMark tickMark : tickMarks) {
double width = tickMark.getWidth();
double min = tickMark.getPosition() - width / 2 + offset;
double max = tickMark.getPosition() + width / 2 + offset;
if (min < 0 || max > parentWidth) {
tickMark.setVisible(false);
}
}
} else {
final double offset = getLayoutY();
final double parentHeight = ((Pane) getParent()).getHeight();
for (TickMark tickMark : tickMarks) {
double height = tickMark.getHeight();
double min = tickMark.getPosition() - height / 2 + offset;
double max = tickMark.getPosition() + height / 2 + offset;
if (min < 0 || max > parentHeight) {
tickMark.setVisible(false);
}
}
}
}

@Deprecated // for testing purposes
List<TickMark> computeTickMarks(AxisRange range, boolean major) {
if (major) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@
.chart {
-fx-tool-bar-side: TOP;
}
.chart .chart-content {
-fx-padding: 5 0 0 0px;
}
.chart .chart-title {
visibility: visible;
-fx-side: top;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,6 @@ $null: null; // null gets removed from Sass. Maybe create a placeholder and repl
// TODO: they don't seem to work and maybe we should create panes with a side property? deprecate these?
-fx-tool-bar-side: TOP;

.chart-content {
// leave some top-axis overdraw-padding if there is no title
-fx-padding: 5 0 0 0px
}

.chart-title {
visibility: visible;
-fx-side: top;
Expand Down

0 comments on commit 80f1aeb

Please sign in to comment.