Repository navigation
Allow tick spacing hints per Y-axis group #1035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
timmolter
merged 2 commits into
knowm:develop
from
PHJ2000:feature-yaxis-group-tick-spacing
Oct 8, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue797.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package org.knowm.xchart.standalone.issues; | ||
|
|
||
| import org.knowm.xchart.SwingWrapper; | ||
| import org.knowm.xchart.XYChart; | ||
| import org.knowm.xchart.XYChartBuilder; | ||
| import org.knowm.xchart.style.Styler; | ||
|
|
||
| /** Demonstrates independent tick spacing for two Y-axis groups. */ | ||
| public class TestForIssue797 { | ||
|
|
||
| public static void main(String[] args) { | ||
| new SwingWrapper<>(getChart()).displayChart(); | ||
| } | ||
|
|
||
| /** Constructs the chart without opening a window. */ | ||
| public static XYChart getChart() { | ||
| XYChart chart = | ||
| new XYChartBuilder() | ||
| .width(900) | ||
| .height(600) | ||
| .title("Y-axis group tick spacing") | ||
| .xAxisTitle("Sample") | ||
| .yAxisTitle("Measurement") | ||
| .build(); | ||
| double[] x = {0, 1, 2, 3, 4, 5}; | ||
| chart.addSeries("Measurement", x, new double[] {0, 20, 40, 60, 80, 100}); | ||
| chart.addSeries("Enabled", x, new double[] {0, 0, 1, 1, 0, 1}).setYAxisGroup(1); | ||
| chart.setYAxisGroupTitle(1, "Enabled"); | ||
| chart.getStyler().setYAxisGroupPosition(1, Styler.YAxisPosition.Right); | ||
| chart.getStyler().setYAxisMin(1, 0.0); | ||
| chart.getStyler().setYAxisMax(1, 1.0); | ||
| chart.getStyler().setYAxisTickMarkSpacingHint(40); | ||
| chart.getStyler().setYAxisGroupTickMarkSpacingHint(1, 200); | ||
| return chart; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,20 @@ | ||
| package org.knowm.xchart.internal.chartpart; | ||
|
|
||
| import java.awt.*; | ||
| import java.awt.Font; | ||
| import java.awt.Shape; | ||
| import java.awt.font.FontRenderContext; | ||
| import java.awt.font.TextLayout; | ||
| import java.awt.geom.AffineTransform; | ||
| import java.awt.geom.Rectangle2D; | ||
| import java.math.BigDecimal; | ||
| import java.math.MathContext; | ||
| import java.math.RoundingMode; | ||
| import java.text.*; | ||
| import java.util.*; | ||
| import java.text.Format; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
| import org.knowm.xchart.internal.Utils; | ||
|
|
@@ -39,6 +43,8 @@ public abstract class AxisTickCalculator_ implements AxisTickCalculator { | |
|
|
||
| Format axisFormat; | ||
|
|
||
| int yIndex; | ||
|
|
||
| private static final int MAX_LABEL_LENGTH = 20; | ||
|
|
||
| /** | ||
|
|
@@ -157,14 +163,13 @@ boolean willLabelsFitInTickSpaceHint(List<String> tickLabels, int tickSpacingHin | |
|
|
||
| AffineTransform rot = null; | ||
| if (styler.getXAxisLabelRotation() != 0) { | ||
| rot = AffineTransform.getRotateInstance( | ||
| -1 * Math.toRadians(styler.getXAxisLabelRotation())); | ||
| rot = AffineTransform.getRotateInstance(-1 * Math.toRadians(styler.getXAxisLabelRotation())); | ||
| } | ||
|
|
||
| Shape shape = textLayout.getOutline(rot); | ||
| Rectangle2D rectangle = shape.getBounds(); | ||
| double largestLabelWidth = Direction.X.equals(this.axisDirection) | ||
| ? rectangle.getWidth() : rectangle.getHeight(); | ||
| double largestLabelWidth = | ||
| Direction.X.equals(this.axisDirection) ? rectangle.getWidth() : rectangle.getHeight(); | ||
|
|
||
| return (largestLabelWidth * 1.1 < tickSpacingHint); | ||
| } | ||
|
|
@@ -174,6 +179,18 @@ public Format getAxisFormat() { | |
| return axisFormat; | ||
| } | ||
|
|
||
| private boolean hasYAxisGroupSpacingHint() { | ||
| return axisDirection == Direction.Y && styler.getYAxisGroupTickMarkSpacingHint(yIndex) != null; | ||
| } | ||
|
|
||
| private int getTickMarkSpacingHint() { | ||
| if (axisDirection == Direction.X) { | ||
| return styler.getXAxisTickMarkSpacingHint(); | ||
| } | ||
| Integer groupHint = styler.getYAxisGroupTickMarkSpacingHint(yIndex); | ||
| return groupHint != null ? groupHint : styler.getYAxisTickMarkSpacingHint(); | ||
| } | ||
|
|
||
| protected void calculate() { | ||
|
|
||
| // System.out.println("calculate"); | ||
|
|
@@ -199,7 +216,17 @@ protected void calculate() { | |
| if (axisDirection == Direction.X && tickSpace < styler.getXAxisTickMarkSpacingHint()) { | ||
| return; | ||
| } | ||
| if (axisDirection == Direction.Y && tickSpace < styler.getYAxisTickMarkSpacingHint()) { | ||
| if (axisDirection == Direction.Y && tickSpace < getTickMarkSpacingHint()) { | ||
| // An explicit group hint requests sparse ticks, not an empty axis after resizing. | ||
| if (hasYAxisGroupSpacingHint() | ||
| && tickSpace > 0 | ||
| && !isNumberFormatChoppingDecimals(maxValue, minValue)) { | ||
| double margin = Utils.getTickStartOffset(workingSpace, tickSpace); | ||
| tickLabels.add(getAxisFormat().format(minValue)); | ||
| tickLocations.add(margin); | ||
| tickLabels.add(getAxisFormat().format(maxValue)); | ||
| tickLocations.add(margin + tickSpace); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -212,28 +239,22 @@ protected void calculate() { | |
| } | ||
|
|
||
| // where the tick should begin in the working space in pixels | ||
| double margin = | ||
| Utils.getTickStartOffset( | ||
| workingSpace, | ||
| tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace); | ||
| double margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space | ||
| // the span of the data | ||
| double span = Math.abs(Math.min((maxValue - minValue), Double.MAX_VALUE - 1)); // in data space | ||
|
|
||
| if (axisValues != null && areValuesEquallySpaced(axisValues)) { | ||
| // Explicit group spacing takes precedence over data-driven, equally spaced ticks. | ||
| if (axisValues != null && areValuesEquallySpaced(axisValues) && !hasYAxisGroupSpacingHint()) { | ||
| calculateForEquallySpacedAxisValues(tickSpace, margin); | ||
| return; | ||
| } | ||
|
|
||
| ////////////////////////// | ||
|
|
||
| int tickSpacingHint = | ||
| (axisDirection == Direction.X | ||
| ? styler.getXAxisTickMarkSpacingHint() | ||
| : styler.getYAxisTickMarkSpacingHint()) | ||
| - 5; | ||
| int tickSpacingHint = getTickMarkSpacingHint() - 5; | ||
|
|
||
| // for very short plots, squeeze some more ticks in than normal into the Y-Axis | ||
| if (axisDirection == Direction.Y && tickSpace < 160) { | ||
| // Squeeze more ticks into short Y-axes unless the caller explicitly set group spacing. | ||
| if (axisDirection == Direction.Y && tickSpace < 160 && !hasYAxisGroupSpacingHint()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree that an explicit group hint should win over the small-plot squeeze. A one-line comment here saying so would help the next reader, since both this and the equally-spaced bypass above are silent behavior changes that only kick in when a group hint exists. |
||
| tickSpacingHint = 25 - 5; | ||
| } | ||
|
|
||
|
|
@@ -367,14 +388,10 @@ protected void calculate() { | |
| } while (!areAllTickLabelsUnique(tickLabels) | ||
| || !willLabelsFitInTickSpaceHint(tickLabels, gridStepInChartSpace)); | ||
|
|
||
| // Prune ticks that fall outside the actual data band [minValue, maxValue]. The generation loop | ||
| // above intentionally overshoots by up to a grid step on each end (getFirstPosition starts one | ||
| // grid step below minValue and the loop bound runs two grid steps past maxValue); those overshoot | ||
| // ticks must not become visible labels. Gridlines and tick marks already clip to the plot bounds, | ||
| // but the tick-label renderer clips to the taller axis-column bounds, so an out-of-range label -- | ||
| // e.g. a negative label when all data is positive -- can leak into the plot margin. Issue #634. | ||
| // A tick value v maps to pixel margin + (v - minValue) / (maxValue - minValue) * tickSpace, so | ||
| // v is within [minValue, maxValue] exactly when its pixel is within [margin, margin + tickSpace]. | ||
| // Prune ticks outside [minValue, maxValue]. Generation intentionally overshoots the | ||
| // data range by a grid step. Tick marks and gridlines clip to the plot, but labels clip | ||
| // to the taller axis column and can leak into the margin (issue #634). | ||
| // Data values in this range map to pixels in [margin, margin + tickSpace]. | ||
| double bandLow = margin - 1e-6; | ||
| double bandHigh = margin + tickSpace + 1e-6; | ||
| List<String> keptLabels = new ArrayList<>(tickLabels.size()); | ||
|
|
@@ -386,8 +403,7 @@ protected void calculate() { | |
| keptLocations.add(loc); | ||
| } | ||
| } | ||
| // Only apply the pruning if at least one tick survives, so a degenerate range can never blank the | ||
| // axis entirely (falls back to the pre-prune behavior). | ||
| // Keep the original ticks if pruning a degenerate range would empty the axis. | ||
| if (!keptLocations.isEmpty() && keptLocations.size() < tickLocations.size()) { | ||
| tickLabels.clear(); | ||
| tickLabels.addAll(keptLabels); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With a per-group hint this early return becomes much easier to hit. The whole point of a group hint for a 0/1 axis is to set it large, and as soon as the hint exceeds the available tick space the axis renders with no ticks and no labels at all.
I rendered the chart from
TestForIssue797at a few heights:0, 10, 1So the demo works at its built-in size and goes blank the moment the window is shrunk a little. That is pre-existing behavior for the global hint, but a user setting a group hint has explicitly asked for sparse ticks, not for none. When a group hint is set and
tickSpace < hint, could we fall back to ticks atminValueandmaxValueinstead of returning empty? Alternatively clamp the effective hint totickSpacebefore the gridStep calculation. Either way, a test for the "hint larger than plot" case would be good.