diff --git a/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue815.java b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue815.java new file mode 100644 index 000000000..f027fd6c4 --- /dev/null +++ b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue815.java @@ -0,0 +1,87 @@ +package org.knowm.xchart.standalone.issues; + +import java.util.Arrays; +import java.util.List; +import org.knowm.xchart.SwingWrapper; +import org.knowm.xchart.XYChart; +import org.knowm.xchart.XYChartBuilder; +import org.knowm.xchart.XYSeries.XYSeriesRenderStyle; +import org.knowm.xchart.style.Styler.ChartTheme; + +/** + * Issue #815 - Extra space below min Y-axis value when calling setYAxisMin(0.0). + * + *
Root cause: plotContentSize (default ~0.95) adds a symmetric margin on both sides of the axis + * via Utils.getTickStartOffset(). Even with setYAxisMin(0.0) the bottom margin is still rendered. + * + *
Workaround: call setPlotContentSize(1.0) to remove the symmetric margin, then use
+ * setYAxisMax() to preserve a small breathing room at the top.
+ */
+public class TestForIssue815 {
+
+ public static void main(String[] args) {
+
+ new SwingWrapper<>(getChartWithExtraSpace())
+ .setTitle("Before fix – extra space below 0")
+ .displayChart();
+
+ new SwingWrapper<>(getChartFixed())
+ .setTitle("After fix – 0 anchored at bottom")
+ .displayChart();
+ }
+
+ /** Demonstrates the problem: setYAxisMin(0.0) leaves extra space below zero. */
+ public static XYChart getChartWithExtraSpace() {
+
+ XYChart chart =
+ new XYChartBuilder()
+ .width(600)
+ .height(400)
+ .title("Extra space below 0 (before fix)")
+ .xAxisTitle("X")
+ .yAxisTitle("Y")
+ .theme(ChartTheme.Matlab)
+ .build();
+
+ chart.addSeries("series1", xData(), yData()).setXYSeriesRenderStyle(XYSeriesRenderStyle.Area);
+
+ chart.getStyler().setYAxisMin(0.0);
+
+ return chart;
+ }
+
+ /**
+ * Demonstrates the fix: setPlotContentSize(1.0) removes the symmetric margin so 0 sits exactly
+ * at the bottom edge. setYAxisMax(105.0) preserves a small breathing room at the top.
+ */
+ public static XYChart getChartFixed() {
+
+ XYChart chart =
+ new XYChartBuilder()
+ .width(600)
+ .height(400)
+ .title("0 anchored at bottom (after fix)")
+ .xAxisTitle("X")
+ .yAxisTitle("Y")
+ .theme(ChartTheme.Matlab)
+ .build();
+
+ chart.addSeries("series1", xData(), yData()).setXYSeriesRenderStyle(XYSeriesRenderStyle.Area);
+
+ chart.getStyler().setYAxisMin(0.0);
+ chart.getStyler().setYAxisMax(105.0);
+ chart.getStyler().setPlotContentSize(1.0);
+
+ return chart;
+ }
+
+ private static List