From f779157d409e5215833fec5a005ff10339fdda45 Mon Sep 17 00:00:00 2001
From: Tim Molter <tim@knowm.org>
Date: Mon, 5 Oct 2015 00:01:27 +0200
Subject: [PATCH] avoid calculating the ticks an extra time

---
 .../xeiam/xchart/internal/chartpart/Axis.java | 44 +++++++++++++--
 .../xchart/internal/chartpart/AxisTick.java   | 54 ++++---------------
 .../chartpart/AxisTickDateCalculator.java     |  2 +
 .../internal/chartpart/AxisTickLabels.java    | 12 ++---
 .../internal/chartpart/AxisTickMarks.java     | 26 +++++----
 .../internal/chartpart/PlotSurface.java       | 10 ++--
 6 files changed, 75 insertions(+), 73 deletions(-)

diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java
index 6dc4f5dc..0a2b32d3 100644
--- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java
+++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java
@@ -22,6 +22,7 @@ import java.awt.font.TextLayout;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Rectangle2D;
 
+import com.xeiam.xchart.StyleManager.ChartType;
 import com.xeiam.xchart.StyleManager.LegendPosition;
 
 /**
@@ -48,6 +49,9 @@ public class Axis implements ChartPart {
   /** the axis tick */
   private AxisTick axisTick;
 
+  /** the axis tick calculator */
+  private AxisTickCalculator axisTickCalculator;
+
   /** the axis direction */
   private Direction direction;
 
@@ -158,7 +162,7 @@ public class Axis implements ChartPart {
       double width = 60; // arbitrary, final width depends on Axis tick labels
       double height = 0;
       do {
-        // System.out.println("width: " + width);
+        // System.out.println("width before: " + width);
 
         double approximateXAxisWidth =
 
@@ -181,6 +185,7 @@ public class Axis implements ChartPart {
             .getStyleManager().getChartPadding();
 
         width = getYAxisWidthHint(height);
+        // System.out.println("width after: " + width);
 
         // System.out.println("height: " + height);
 
@@ -287,7 +292,7 @@ public class Axis implements ChartPart {
       // get some real tick labels
       // System.out.println("XAxisHeightHint");
       // System.out.println("workingSpace: " + workingSpace);
-      AxisTickCalculator axisTickCalculator = axisTick.getAxisTickCalculator(workingSpace);
+      this.axisTickCalculator = getAxisTickCalculator(workingSpace);
       String sampleLabel = "";
       // find the longest String in all the labels
       for (int i = 0; i < axisTickCalculator.getTickLabels().size(); i++) {
@@ -327,7 +332,7 @@ public class Axis implements ChartPart {
       // get some real tick labels
       // System.out.println("XAxisHeightHint");
       // System.out.println("workingSpace: " + workingSpace);
-      AxisTickCalculator axisTickCalculator = axisTick.getAxisTickCalculator(workingSpace);
+      this.axisTickCalculator = getAxisTickCalculator(workingSpace);
       String sampleLabel = "";
       // find the longest String in all the labels
       for (int i = 0; i < axisTickCalculator.getTickLabels().size(); i++) {
@@ -345,6 +350,34 @@ public class Axis implements ChartPart {
     return titleHeight + axisTickLabelsHeight;
   }
 
+  private AxisTickCalculator getAxisTickCalculator(double workingSpace) {
+
+    if (getDirection() == Direction.X && getChartPainter().getStyleManager().getChartType() == ChartType.Bar) {
+
+      return new AxisTickBarChartCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartPainter());
+
+    }
+    else if (getDirection() == Direction.X && getChartPainter().getStyleManager().isXAxisLogarithmic() && getAxisType() != AxisType.Date) {
+
+      return new AxisTickLogarithmicCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartPainter().getStyleManager());
+
+    }
+    else if (getDirection() == Direction.Y && getChartPainter().getStyleManager().isYAxisLogarithmic() && getAxisType() != AxisType.Date) {
+
+      return new AxisTickLogarithmicCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartPainter().getStyleManager());
+
+    }
+    else if (getAxisType() == AxisType.Date) {
+
+      return new AxisTickDateCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartPainter().getStyleManager());
+
+    }
+    else { // number
+
+      return new AxisTickNumericalCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartPainter().getStyleManager());
+    }
+  }
+
   @Override
   public ChartPainter getChartPainter() {
 
@@ -393,4 +426,9 @@ public class Axis implements ChartPart {
     this.axisTitle = axisTitle;
   }
 
+  public AxisTickCalculator getAxisTickCalculator() {
+
+    return this.axisTickCalculator;
+  }
+
 }
diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java
index 02111b2d..f12d2f3e 100644
--- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java
+++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java
@@ -17,10 +17,6 @@ package com.xeiam.xchart.internal.chartpart;
 
 import java.awt.Graphics2D;
 import java.awt.geom.Rectangle2D;
-import java.util.List;
-
-import com.xeiam.xchart.StyleManager.ChartType;
-import com.xeiam.xchart.internal.chartpart.Axis.AxisType;
 
 /**
  * An axis tick
@@ -39,8 +35,6 @@ public class AxisTick implements ChartPart {
   /** the bounds */
   private Rectangle2D bounds = new Rectangle2D.Double();
 
-  AxisTickCalculator axisTickCalculator = null;
-
   /**
    * Constructor
    *
@@ -76,7 +70,6 @@ public class AxisTick implements ChartPart {
 
     // System.out.println("AxisTick: " + axis.getDirection());
     // System.out.println("workingSpace: " + workingSpace);
-    axisTickCalculator = getAxisTickCalculator(workingSpace);
 
     if (axis.getDirection() == Axis.Direction.Y && getChartPainter().getStyleManager().isYAxisTicksVisible()) {
 
@@ -114,35 +107,6 @@ public class AxisTick implements ChartPart {
 
   }
 
-  public AxisTickCalculator getAxisTickCalculator(double workingSpace) {
-
-    if (axis.getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().getChartType() == ChartType.Bar) {
-
-      return new AxisTickBarChartCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter());
-
-    }
-    else if (axis.getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().isXAxisLogarithmic() && axis.getAxisType() != AxisType.Date) {
-
-      return new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
-
-    }
-    else if (axis.getDirection() == Axis.Direction.Y && getChartPainter().getStyleManager().isYAxisLogarithmic() && axis.getAxisType() != AxisType.Date) {
-
-      return new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
-
-    }
-    else if (axis.getAxisType() == AxisType.Date) {
-
-      return new AxisTickDateCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
-
-    }
-    else { // number
-
-      return new AxisTickNumericalCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
-
-    }
-  }
-
   @Override
   public ChartPainter getChartPainter() {
 
@@ -161,13 +125,13 @@ public class AxisTick implements ChartPart {
     return axisTickLabels;
   }
 
-  public List<Double> getTickLocations() {
-
-    return axisTickCalculator.getTickLocations();
-  }
-
-  public List<String> getTickLabels() {
-
-    return axisTickCalculator.getTickLabels();
-  }
+  // public List<Double> getTickLocations() {
+  //
+  // return axisTickCalculator.getTickLocations();
+  // }
+  //
+  // public List<String> getTickLabels() {
+  //
+  // return axisTickCalculator.getTickLabels();
+  // }
 }
diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java
index bd2be161..8ef429ab 100644
--- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java
+++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java
@@ -110,6 +110,8 @@ public class AxisTickDateCalculator extends AxisTickCalculator {
   public AxisTickDateCalculator(Direction axisDirection, double workingSpace, double minValue, double maxValue, StyleManager styleManager) {
 
     super(axisDirection, workingSpace, minValue, maxValue, styleManager);
+
+    System.out.println("AxisTickDateCalculator Constructor");
     calculate();
   }
 
diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java
index 21b85aae..076371a2 100644
--- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java
+++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java
@@ -67,11 +67,11 @@ public class AxisTickLabels implements ChartPart {
       double maxTickLabelWidth = 0;
       Map<Double, TextLayout> axisLabelTextLayouts = new HashMap<Double, TextLayout>();
 
-      for (int i = 0; i < axisTick.getTickLabels().size(); i++) {
+      for (int i = 0; i < axisTick.getAxis().getAxisTickCalculator().getTickLabels().size(); i++) {
 
-        String tickLabel = axisTick.getTickLabels().get(i);
+        String tickLabel = axisTick.getAxis().getAxisTickCalculator().getTickLabels().get(i);
         // System.out.println("** " + tickLabel);
-        double tickLocation = axisTick.getTickLocations().get(i);
+        double tickLocation = axisTick.getAxis().getAxisTickCalculator().getTickLocations().get(i);
         double flippedTickLocation = yOffset + height - tickLocation;
 
         if (tickLabel != null && flippedTickLocation > yOffset && flippedTickLocation < yOffset + height) { // some are null for logarithmic axes
@@ -131,11 +131,11 @@ public class AxisTickLabels implements ChartPart {
       double maxTickLabelHeight = 0;
 
       // System.out.println("axisTick.getTickLabels().size(): " + axisTick.getTickLabels().size());
-      for (int i = 0; i < axisTick.getTickLabels().size(); i++) {
+      for (int i = 0; i < axisTick.getAxis().getAxisTickCalculator().getTickLabels().size(); i++) {
 
-        String tickLabel = axisTick.getTickLabels().get(i);
+        String tickLabel = axisTick.getAxis().getAxisTickCalculator().getTickLabels().get(i);
         // System.out.println("tickLabel: " + tickLabel);
-        double tickLocation = axisTick.getTickLocations().get(i);
+        double tickLocation = axisTick.getAxis().getAxisTickCalculator().getTickLocations().get(i);
         double shiftedTickLocation = xOffset + tickLocation;
 
         // discard null and out of bounds labels
diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java
index 87f966dd..59073765 100644
--- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java
+++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java
@@ -66,9 +66,9 @@ public class AxisTickMarks implements ChartPart {
       // tick marks
       if (getChartPainter().getStyleManager().isAxisTicksMarksVisible()) {
 
-        for (int i = 0; i < axisTick.getTickLabels().size(); i++) {
+        for (int i = 0; i < axisTick.getAxis().getAxisTickCalculator().getTickLabels().size(); i++) {
 
-          double tickLocation = axisTick.getTickLocations().get(i);
+          double tickLocation = axisTick.getAxis().getAxisTickCalculator().getTickLocations().get(i);
           double flippedTickLocation = yOffset + axisTick.getAxis().getPaintZone().getHeight() - tickLocation;
           if (flippedTickLocation > bounds.getY() && flippedTickLocation < bounds.getY() + bounds.getHeight()) {
 
@@ -81,9 +81,8 @@ public class AxisTickMarks implements ChartPart {
       // Line
       if (getChartPainter().getStyleManager().isAxisTicksLineVisible()) {
 
-        Shape line =
-            new Line2D.Double(xOffset + getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset, xOffset + getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset
-                + axisTick.getAxis().getPaintZone().getHeight());
+        Shape line = new Line2D.Double(xOffset + getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset, xOffset + getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset
+            + axisTick.getAxis().getPaintZone().getHeight());
         g.draw(line);
 
       }
@@ -96,18 +95,17 @@ public class AxisTickMarks implements ChartPart {
       double yOffset = axisTick.getAxisTickLabels().getBounds().getY() - getChartPainter().getStyleManager().getAxisTickPadding();
 
       // bounds
-      bounds =
-          new Rectangle2D.Double(xOffset, yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength(), axisTick.getAxis().getPaintZone().getWidth(), getChartPainter().getStyleManager()
-              .getAxisTickMarkLength());
-      // g.setColor(Color.yellow);
-      // g.draw(bounds);
+      bounds = new Rectangle2D.Double(xOffset, yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength(), axisTick.getAxis().getPaintZone().getWidth(), getChartPainter().getStyleManager()
+          .getAxisTickMarkLength());
+          // g.setColor(Color.yellow);
+          // g.draw(bounds);
 
       // tick marks
       if (getChartPainter().getStyleManager().isAxisTicksMarksVisible()) {
 
-        for (int i = 0; i < axisTick.getTickLabels().size(); i++) {
+        for (int i = 0; i < axisTick.getAxis().getAxisTickCalculator().getTickLabels().size(); i++) {
 
-          double tickLocation = axisTick.getTickLocations().get(i);
+          double tickLocation = axisTick.getAxis().getAxisTickCalculator().getTickLocations().get(i);
           double shiftedTickLocation = xOffset + tickLocation;
 
           if (shiftedTickLocation > bounds.getX() && shiftedTickLocation < bounds.getX() + bounds.getWidth()) {
@@ -122,8 +120,8 @@ public class AxisTickMarks implements ChartPart {
       if (getChartPainter().getStyleManager().isAxisTicksLineVisible()) {
 
         g.setStroke(getChartPainter().getStyleManager().getAxisTickMarksStroke());
-        g.drawLine((int) xOffset, (int) (yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength()), (int) (xOffset + axisTick.getAxis().getPaintZone().getWidth()),
-            (int) (yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength()));
+        g.drawLine((int) xOffset, (int) (yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength()), (int) (xOffset + axisTick.getAxis().getPaintZone().getWidth()), (int) (yOffset
+            - getChartPainter().getStyleManager().getAxisTickMarkLength()));
       }
 
     }
diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java
index c146cf64..1d688233 100644
--- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java
+++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java
@@ -70,7 +70,7 @@ public class PlotSurface implements ChartPart {
     if (getChartPainter().getStyleManager().isPlotGridLinesVisible() || getChartPainter().getStyleManager().isPlotTicksMarksVisible()) {
 
       // horizontal
-      List<Double> yAxisTickLocations = getChartPainter().getAxisPair().getYAxis().getAxisTick().getTickLocations();
+      List<Double> yAxisTickLocations = getChartPainter().getAxisPair().getYAxis().getAxisTickCalculator().getTickLocations();
       for (int i = 0; i < yAxisTickLocations.size(); i++) {
 
         double yOffset = bounds.getY() + bounds.getHeight() - yAxisTickLocations.get(i);
@@ -102,13 +102,13 @@ public class PlotSurface implements ChartPart {
       // vertical
       if (getChartPainter().getStyleManager().getChartType() != ChartType.Bar
 
-          && (getChartPainter().getStyleManager().isPlotGridLinesVisible()
+      && (getChartPainter().getStyleManager().isPlotGridLinesVisible()
 
-              || getChartPainter().getStyleManager().isPlotTicksMarksVisible())
+      || getChartPainter().getStyleManager().isPlotTicksMarksVisible())
 
-          ) {
+      ) {
 
-        List<Double> xAxisTickLocations = getChartPainter().getAxisPair().getXAxis().getAxisTick().getTickLocations();
+        List<Double> xAxisTickLocations = getChartPainter().getAxisPair().getXAxis().getAxisTickCalculator().getTickLocations();
         for (int i = 0; i < xAxisTickLocations.size(); i++) {
 
           double tickLocation = xAxisTickLocations.get(i);
-- 
GitLab