From d4bd17e15c61fb1c25de31eccdf3d239f565d5b1 Mon Sep 17 00:00:00 2001
From: Tim Molter <tim@knowm.org>
Date: Sat, 2 Jan 2016 22:18:10 +0100
Subject: [PATCH] moved axis min and max back to Axis

---
 .../knowm/xchart/internal/chartpart/Axis.java | 55 ++++++++++-
 .../internal/chartpart/ChartInternal.java     | 94 +++----------------
 .../PlotContentCategoricalChart.java          |  4 +-
 .../chartpart/PlotContentNumericalChart.java  |  8 +-
 4 files changed, 67 insertions(+), 94 deletions(-)

diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/Axis.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/Axis.java
index 4272173a..c16af11a 100644
--- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/Axis.java
+++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/Axis.java
@@ -24,6 +24,7 @@ import java.awt.geom.AffineTransform;
 import java.awt.geom.Rectangle2D;
 import java.util.List;
 
+import org.knowm.xchart.StyleManager.ChartType;
 import org.knowm.xchart.StyleManager.LegendPosition;
 import org.knowm.xchart.internal.chartpart.ChartInternal.ChartInternalType;
 
@@ -356,14 +357,14 @@ public class Axis implements ChartPart {
       }
       else if (getChartInternal().getChartInternalType() == ChartInternalType.XY && getAxisType() == AxisType.Date) {
 
-        return new AxisTickCalculator_Date(getDirection(), workingSpace, getChartInternal().getxAxisMin(), getChartInternal().getxAxisMax(), getChartInternal().getStyleManager());
+        return new AxisTickCalculator_Date(getDirection(), workingSpace, min, max, getChartInternal().getStyleManager());
       }
       else if (getChartInternal().getStyleManager().isXAxisLogarithmic()) {
 
-        return new AxisTickCalculator_Logarithmic(getDirection(), workingSpace, getChartInternal().getxAxisMin(), getChartInternal().getxAxisMax(), getChartInternal().getStyleManager());
+        return new AxisTickCalculator_Logarithmic(getDirection(), workingSpace, min, max, getChartInternal().getStyleManager());
       }
       else {
-        return new AxisTickCalculator_Number(getDirection(), workingSpace, getChartInternal().getxAxisMin(), getChartInternal().getxAxisMax(), getChartInternal().getStyleManager());
+        return new AxisTickCalculator_Number(getDirection(), workingSpace, min, max, getChartInternal().getStyleManager());
 
       }
     }
@@ -373,10 +374,10 @@ public class Axis implements ChartPart {
 
       if (getChartInternal().getStyleManager().isYAxisLogarithmic() && getAxisType() != AxisType.Date) {
 
-        return new AxisTickCalculator_Logarithmic(getDirection(), workingSpace, getChartInternal().getyAxisMin(), getChartInternal().getyAxisMax(), getChartInternal().getStyleManager());
+        return new AxisTickCalculator_Logarithmic(getDirection(), workingSpace, min, max, getChartInternal().getStyleManager());
       }
       else {
-        return new AxisTickCalculator_Number(getDirection(), workingSpace, getChartInternal().getyAxisMin(), getChartInternal().getyAxisMax(), getChartInternal().getStyleManager());
+        return new AxisTickCalculator_Number(getDirection(), workingSpace, min, max, getChartInternal().getStyleManager());
 
       }
     }
@@ -436,4 +437,48 @@ public class Axis implements ChartPart {
     return this.axisTickCalculator;
   }
 
+  public void overrideMinMax() {
+
+    if (direction == Direction.X) { // X-Axis
+
+      double overrideXAxisMinValue = min;
+      double overrideXAxisMaxValue = max;
+
+      // override min and maxValue if specified
+      if (getChartInternal().getStyleManager().getXAxisMin() != null && getChartInternal().getStyleManager().getChartType() != ChartType.Bar) { // bar chart cannot have a max or min TODO is this true?
+        overrideXAxisMinValue = getChartInternal().getStyleManager().getXAxisMin();
+      }
+      if (getChartInternal().getStyleManager().getXAxisMax() != null && getChartInternal().getStyleManager().getChartType() != ChartType.Bar) { // bar chart cannot have a max or min
+        overrideXAxisMaxValue = getChartInternal().getStyleManager().getXAxisMax();
+      }
+      min = overrideXAxisMinValue;
+      max = overrideXAxisMaxValue;
+    }
+    else {
+
+      double overrideYAxisMinValue = min;
+      double overrideYAxisMaxValue = max;
+      // override min/max value for bar charts' Y-Axis
+      if (getChartInternal().getStyleManager().getChartType() == ChartType.Bar) { // this is the Y-Axis for a bar chart
+        if (min > 0.0 && max > 0.0) {
+          overrideYAxisMinValue = 0.0;
+        }
+        if (min < 0.0 && max < 0.0) {
+          overrideYAxisMaxValue = 0.0;
+        }
+      }
+
+      // override min and maxValue if specified
+      if (getChartInternal().getStyleManager().getYAxisMin() != null) {
+        overrideYAxisMinValue = getChartInternal().getStyleManager().getYAxisMin();
+      }
+      if (getChartInternal().getStyleManager().getYAxisMax() != null) {
+        overrideYAxisMaxValue = getChartInternal().getStyleManager().getYAxisMax();
+      }
+      min = overrideYAxisMinValue;
+      max = overrideYAxisMaxValue;
+    }
+
+  }
+
 }
diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartInternal.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartInternal.java
index 332d7ab8..c3969dd9 100644
--- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartInternal.java
+++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartInternal.java
@@ -29,7 +29,6 @@ import java.util.Map;
 
 import org.knowm.xchart.Series;
 import org.knowm.xchart.StyleManager;
-import org.knowm.xchart.StyleManager.ChartType;
 import org.knowm.xchart.internal.chartpart.Axis.AxisType;
 import org.knowm.xchart.internal.style.SeriesColorMarkerLineStyleCycler;
 
@@ -53,11 +52,6 @@ public class ChartInternal {
 
   private final StyleManager styleManager;
 
-  private double xAxisMin;
-  private double xAxisMax;
-  private double yAxisMin;
-  private double yAxisMax;
-
   // Chart Parts
   private Legend chartLegend;
   private AxisPair axisPair;
@@ -293,12 +287,16 @@ public class ChartInternal {
    */
   public void paint(Graphics2D g) {
 
+    // Sanity checks
+    if (getSeriesMap().isEmpty()) {
+      throw new RuntimeException("No series defined for Chart!!!");
+    }
+
     // calc axis min and max
     axisPair.getXAxis().resetMinMax();
     axisPair.getYAxis().resetMinMax();
-
     for (Series series : getSeriesMap().values()) {
-      // add min/max to axis
+      // add min/max to axes
       // System.out.println(series.getxMin());
       // System.out.println(series.getxMax());
       // System.out.println(series.getyMin());
@@ -307,71 +305,21 @@ public class ChartInternal {
       axisPair.getXAxis().addMinMax(series.getXMin(), series.getXMax());
       axisPair.getYAxis().addMinMax(series.getYMin(), series.getYMax());
     }
-    // Sanity checks
-    if (getSeriesMap().isEmpty()) {
-      throw new RuntimeException("No series defined for Chart!!!");
-    }
-
-    xAxisMin = axisPair.getXAxis().getMin();
-    xAxisMax = axisPair.getXAxis().getMax();
-    yAxisMin = axisPair.getYAxis().getMin();
-    yAxisMax = axisPair.getYAxis().getMax();
-
-    // override min/max value for bar charts' Y-Axis
-    double overrideXAxisMinValue = xAxisMin;
-    double overrideXAxisMaxValue = xAxisMax;
-    double overrideYAxisMinValue = yAxisMin;
-    double overrideYAxisMaxValue = yAxisMax;
-    if (styleManager.getChartType() == ChartType.Bar) { // this is the Y-Axis for a bar chart
-      if (yAxisMin > 0.0 && yAxisMax > 0.0) {
-        overrideYAxisMinValue = 0.0;
-      }
-      if (yAxisMin < 0.0 && yAxisMax < 0.0) {
-        overrideYAxisMaxValue = 0.0;
-      }
-    }
-
-    // override min and maxValue if specified
-    if (styleManager.getXAxisMin() != null && styleManager.getChartType() != ChartType.Bar) { // bar chart cannot have a max or min TODO is this true? Do we want this?
-      overrideXAxisMinValue = styleManager.getXAxisMin();
-    }
-    if (styleManager.getXAxisMax() != null && styleManager.getChartType() != ChartType.Bar) { // bar chart cannot have a max or min
-      overrideXAxisMaxValue = styleManager.getXAxisMax();
-    }
-    if (styleManager.getYAxisMin() != null) {
-      overrideYAxisMinValue = styleManager.getYAxisMin();
-    }
-    if (styleManager.getYAxisMax() != null) {
-      overrideYAxisMaxValue = styleManager.getYAxisMax();
-    }
 
-    this.xAxisMin = overrideXAxisMinValue;
-    this.xAxisMax = overrideXAxisMaxValue;
-    this.yAxisMin = overrideYAxisMinValue;
-    this.yAxisMax = overrideYAxisMaxValue;
+    axisPair.getXAxis().overrideMinMax();
+    axisPair.getYAxis().overrideMinMax();
 
     // logarithmic
 
-    if (getStyleManager().isXAxisLogarithmic() && xAxisMin <= 0.0) {
+    if (getStyleManager().isXAxisLogarithmic() && axisPair.getXAxis().getMin() <= 0.0) {
       throw new IllegalArgumentException("Series data (accounting for error bars too) cannot be less or equal to zero for a logarithmic X-Axis!!!");
     }
-    if (getStyleManager().isYAxisLogarithmic() && yAxisMin <= 0.0) {
+    if (getStyleManager().isYAxisLogarithmic() && axisPair.getYAxis().getMin() <= 0.0) {
       // System.out.println(axisPair.getyAxis().getMin());
       throw new IllegalArgumentException("Series data (accounting for error bars too) cannot be less or equal to zero for a logarithmic Y-Axis!!!");
     }
-    // if (styleManager.getChartType() == ChartType.Bar && styleManager.isYAxisLogarithmic()) {
-    // int logMin = (int) Math.floor(Math.log10(yAxisMin));
-    // overrideYAxisMinValue = Utils.pow(10, logMin);
-    // }
-    // if (styleManager.isXAxisLogarithmic()) {
-    // xAxisMin = Math.log10(overrideXAxisMinValue);
-    // xAxisMax = Math.log10(overrideXAxisMaxValue);
-    // }
-    // if (styleManager.isYAxisLogarithmic()) {
-    // yAxisMin = Math.log10(overrideYAxisMinValue);
-    // yAxisMax = Math.log10(overrideYAxisMaxValue);
-    // }
 
+    // paint chart main background
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // global rendering hint
     g.setColor(styleManager.getChartBackgroundColor());
     Shape rect = new Rectangle2D.Double(0, 0, width, height);
@@ -458,24 +406,4 @@ public class ChartInternal {
     return styleManager;
   }
 
-  public double getxAxisMin() {
-
-    return xAxisMin;
-  }
-
-  public double getxAxisMax() {
-
-    return xAxisMax;
-  }
-
-  public double getyAxisMin() {
-
-    return yAxisMin;
-  }
-
-  public double getyAxisMax() {
-
-    return yAxisMax;
-  }
-
 }
diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java
index 4cf70775..33bc4a10 100644
--- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java
+++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java
@@ -75,8 +75,8 @@ public class PlotContentCategoricalChart extends PlotContent {
     int numCategories = getChartInternal().getSeriesMap().values().iterator().next().getXData().size();
     double gridStep = xTickSpace / numCategories;
 
-    double yMin = getChartInternal().getyAxisMin();
-    double yMax = getChartInternal().getyAxisMax();
+    double yMin = getChartInternal().getAxisPair().getYAxis().getMin();
+    double yMax = getChartInternal().getAxisPair().getYAxis().getMax();
 
     // TODO only for bar charts necessary
     // figure out the general form of the chart
diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentNumericalChart.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentNumericalChart.java
index 215dd209..472b3181 100644
--- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentNumericalChart.java
+++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentNumericalChart.java
@@ -81,10 +81,10 @@ public class PlotContentNumericalChart extends PlotContent {
     double yTickSpace = styleManager.getAxisTickSpacePercentage() * bounds.getHeight();
     double yTopMargin = Utils.getTickStartOffset((int) bounds.getHeight(), yTickSpace);
 
-    double xMin = getChartInternal().getxAxisMin();
-    double xMax = getChartInternal().getxAxisMax();
-    double yMin = getChartInternal().getyAxisMin();
-    double yMax = getChartInternal().getyAxisMax();
+    double xMin = getChartInternal().getAxisPair().getXAxis().getMin();
+    double xMax = getChartInternal().getAxisPair().getXAxis().getMax();
+    double yMin = getChartInternal().getAxisPair().getYAxis().getMin();
+    double yMax = getChartInternal().getAxisPair().getYAxis().getMax();
 
     // logarithmic
     if (getChartInternal().getStyleManager().isXAxisLogarithmic()) {
-- 
GitLab