diff --git a/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue159.java b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue159.java index 04c1a43f..5f25dc8b 100644 --- a/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue159.java +++ b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue159.java @@ -41,7 +41,7 @@ public static void main(String[] args) throws Exception { styler.setAxisTickLabelsColor(Color.darkGray); styler.setDatePattern("MM-dd"); - styler.setYAxisMin(1.0); + styler.setYAxisMin(2.0); styler.setYAxisMax(10.0); styler.setXAxisTickMarkSpacingHint(200); diff --git a/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue653.java b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue653.java index ab9e4f2c..2737e6de 100644 --- a/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue653.java +++ b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue653.java @@ -24,7 +24,7 @@ public static CategoryChart getCategoryChart() { builder.title("Sample Chart").xAxisTitle("X").yAxisTitle("Y").theme(Styler.ChartTheme.Matlab); CategoryChart chart = builder.build(); chart.getStyler().setDefaultSeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Line); - // .setYAxisMin(0.0); + // .setYAxisMin(0.0); chart.addSeries("y(x)", xData, yData); diff --git a/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue826.java b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue826.java new file mode 100644 index 00000000..d40709b2 --- /dev/null +++ b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue826.java @@ -0,0 +1,57 @@ +package org.knowm.xchart.standalone.issues; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Arrays; +import org.knowm.xchart.CategoryChart; +import org.knowm.xchart.CategoryChartBuilder; +import org.knowm.xchart.SwingWrapper; + +public class TestForIssue826 { + + public static void main(String[] args) throws ParseException { + + CategoryChart chart = getChart(); + new SwingWrapper(chart).displayChart(); + } + + public static CategoryChart getChart() { + final CategoryChart chart = + new CategoryChartBuilder().width(600).height(400).xAxisTitle("X").yAxisTitle("Y").build(); + + ArrayList years = + new ArrayList( + Arrays.asList( + new String[] { + "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", + "2022" + })); + + ArrayList yAData = + new ArrayList( + Arrays.asList( + new Number[] { + 4438887, 4365843, 4050498, 4757380, 4429130, 4692889, 4354087, 4530343, 4572770, + 4150489, 4487793 + })); + + ArrayList yBData = + new ArrayList( + Arrays.asList( + new Number[] { + 3198714, 3144079, 2859215, 3430605, 3839149, 4042579, 3741823, 3890162, 3731367, + 3751216, 4008249 + })); + + chart.getStyler().setOverlapped(true); + chart.getStyler().setYAxisDecimalPattern("###,###.##"); + + chart.addSeries("A", years, yAData); + chart.addSeries("B", years, yBData); + + // chart.getStyler().setYAxisMin(2600000.0); + chart.setTitle(Double.toString(2600000.0)); + + return chart; + } +} diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisPair.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisPair.java index 7db11d60..4f9bd3ec 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisPair.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisPair.java @@ -471,20 +471,21 @@ private void overrideMinMaxForYAxis(Axis yAxis) { } // override min and maxValue if specified - if (chart.getStyler().getYAxisMin(yAxis.getYIndex()) != null - && !(chart.getStyler() instanceof BoxStyler)) { - overrideYAxisMinValue = chart.getStyler().getYAxisMin(yAxis.getYIndex()); - } else if (chart.getStyler().getYAxisMin() != null - && !(chart.getStyler() instanceof BoxStyler)) { - overrideYAxisMinValue = chart.getStyler().getYAxisMin(); - } - if (chart.getStyler().getYAxisMax(yAxis.getYIndex()) != null - && !(chart.getStyler() instanceof BoxStyler)) { - overrideYAxisMaxValue = chart.getStyler().getYAxisMax(yAxis.getYIndex()); - } else if (chart.getStyler().getYAxisMax() != null - && !(chart.getStyler() instanceof BoxStyler)) { - overrideYAxisMaxValue = chart.getStyler().getYAxisMax(); + if (!(chart.getStyler() instanceof BoxStyler)) { + + // min + if (chart.getStyler().getYAxisMin(yAxis.getYIndex()) != null) { + overrideYAxisMinValue = chart.getStyler().getYAxisMin(yAxis.getYIndex()); + } else if (chart.getStyler().getYAxisMin() != null) { + overrideYAxisMinValue = chart.getStyler().getYAxisMin(); + } + // max + if (chart.getStyler().getYAxisMax(yAxis.getYIndex()) != null) { + overrideYAxisMaxValue = chart.getStyler().getYAxisMax(yAxis.getYIndex()); + } else if (chart.getStyler().getYAxisMax() != null) { + overrideYAxisMaxValue = chart.getStyler().getYAxisMax(); + } } yAxis.setMin(overrideYAxisMinValue); diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_.java index 05d013cc..6cc66e15 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/AxisTickCalculator_.java @@ -60,8 +60,8 @@ public abstract class AxisTickCalculator_ implements AxisTickCalculator { this.axisDirection = axisDirection; this.workingSpace = workingSpace; - this.minValue = getAxisMinValue(styler, axisDirection, minValue); - this.maxValue = getAxisMaxValue(styler, axisDirection, maxValue); + this.minValue = minValue; + this.maxValue = maxValue; this.styler = styler; } @@ -79,8 +79,8 @@ public abstract class AxisTickCalculator_ implements AxisTickCalculator { axisValuesWithMinMax.addAll(axisValues); axisValuesWithMinMax.add(maxValue); this.axisValues = new ArrayList<>(axisValuesWithMinMax); - this.minValue = getAxisMinValue(styler, axisDirection, minValue); - this.maxValue = getAxisMaxValue(styler, axisDirection, maxValue); + this.minValue = minValue; + this.maxValue = maxValue; this.styler = styler; } @@ -192,7 +192,7 @@ protected void calculate() { // same value i.e. "#0.00" for 0.0, 0.0001, 0.0002 // issue #582 if (isNumberFormatChoppingDecimals(maxValue, minValue)) { - System.out.println("returning"); + // System.out.println("returning"); return; } @@ -421,48 +421,6 @@ boolean areAllTickLabelsUnique(List tickLabels) { return new LinkedHashSet<>(tickLabels).size() == tickLabels.size(); } - /** - * Determines the axis min value, which may differ from the min value of the respective data (e.g. - * for bar charts). - * - * @param styler the chart {@link Styler} - * @param axisDirection the axis {@link Direction} - * @param dataMinValue the minimum value of the data corresponding with the axis. - * @return the axis min value - */ - private static double getAxisMinValue( - Styler styler, Direction axisDirection, double dataMinValue) { - - // special case for category charts - if (styler instanceof CategoryStyler) { - CategoryStyler categoryStyler = (CategoryStyler) styler; - if ((categoryStyler.getDefaultSeriesRenderStyle() - == CategorySeries.CategorySeriesRenderStyle.Bar - || categoryStyler.getDefaultSeriesRenderStyle() - == CategorySeries.CategorySeriesRenderStyle.Stick) - && dataMinValue > 0) { - return 0; - } - } - return dataMinValue; - } - - /** - * Determines the axis max value, which may differ from the max value of the respective data (e.g. - * for bar charts). - * - * @param styler the chart {@link Styler} - * @param axisDirection the axis {@link Direction} - * @param dataMaxValue the maximum value of the data corresponding with the axis. - * @return the axis max value - */ - private static double getAxisMaxValue( - Styler styler, Direction axisDirection, double dataMaxValue) { - if (Direction.Y.equals(axisDirection) && styler instanceof CategoryStyler && dataMaxValue < 0) - return 0; - return dataMaxValue; - } - private boolean isNumberFormatChoppingDecimals(double axisMax, double axisMin) { // System.out.println("axisMax = " + axisMax);