Skip to content
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

Refactoring Code #354

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

package org.jfree.chart.annotations;

import java.awt.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
Expand All @@ -56,6 +57,8 @@
public abstract class AbstractAnnotation implements Annotation, Cloneable,
Serializable {

/** The default paint. */
public static final Paint DEFAULT_PAINT = Color.BLACK;
/** Storage for registered change listeners. */
private transient EventListenerList listenerList;

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jfree/chart/annotations/TextAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public class TextAnnotation extends AbstractAnnotation implements Serializable {
/** The default font. */
public static final Font DEFAULT_FONT
= new Font("SansSerif", Font.PLAIN, 10);

/** The default paint. */
public static final Paint DEFAULT_PAINT = Color.BLACK;
//
// /** The default paint. */
// public static final Paint DEFAULT_PAINT = AbstractAnnotation.DEFAULT_PAINT;

/** The default text anchor. */
public static final TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.CENTER;
Expand Down Expand Up @@ -106,7 +106,7 @@ protected TextAnnotation(String text) {
Args.nullNotPermitted(text, "text");
this.text = text;
this.font = DEFAULT_FONT;
this.paint = DEFAULT_PAINT;
this.paint = AbstractAnnotation.DEFAULT_PAINT;
this.textAnchor = DEFAULT_TEXT_ANCHOR;
this.rotationAnchor = DEFAULT_ROTATION_ANCHOR;
this.rotationAngle = DEFAULT_ROTATION_ANGLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public class XYTextAnnotation extends AbstractXYAnnotation
public static final Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN,
10);

/** The default paint. */
public static final Paint DEFAULT_PAINT = Color.BLACK;
// /** The default paint. */
// public static final Paint DEFAULT_PAINT = AbstractAnnotation.DEFAULT_PAINT;

/** The default text anchor. */
public static final TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.CENTER;
Expand Down Expand Up @@ -150,7 +150,7 @@ public XYTextAnnotation(String text, double x, double y) {
Args.requireFinite(y, "y");
this.text = text;
this.font = DEFAULT_FONT;
this.paint = DEFAULT_PAINT;
this.paint = AbstractAnnotation.DEFAULT_PAINT;
this.x = x;
this.y = y;
this.textAnchor = DEFAULT_TEXT_ANCHOR;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jfree/chart/axis/CategoryAxis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1207,9 +1207,9 @@ protected double calculateCategoryLabelWidth(TextBlock label,
size.getHeight());
Shape rotatedBox = ShapeUtils.rotateShape(box, position.getAngle(),
0.0f, 0.0f);
double w = rotatedBox.getBounds2D().getWidth() + insets.getLeft()
double width = rotatedBox.getBounds2D().getWidth() + insets.getLeft()
+ insets.getRight();
return w;
return width;
}

/**
Expand Down
48 changes: 24 additions & 24 deletions src/main/java/org/jfree/chart/axis/CyclicNumberAxis.java
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,7 @@ else if (RectangleEdge.isLeftOrRight(edge)) {
* @return The Java 2D value.
*/
@Override
public double valueToJava2D(double value, Rectangle2D dataArea,
RectangleEdge edge) {
public double valueToJava2D(double value, Rectangle2D dataArea, RectangleEdge edge) {
Range range = getRange();

double vmin = range.getLowerBound();
Expand All @@ -776,39 +775,40 @@ public double valueToJava2D(double value, Rectangle2D dataArea,
return Double.NaN;
}


double jmin = 0.0;
double jmax = 0.0;
if (RectangleEdge.isTopOrBottom(edge)) {
jmin = dataArea.getMinX();
jmax = dataArea.getMaxX();
}
else if (RectangleEdge.isLeftOrRight(edge)) {
} else if (RectangleEdge.isLeftOrRight(edge)) {
jmax = dataArea.getMinY();
jmin = dataArea.getMaxY();
}

if (isInverted()) {
if (value == vp) {
return this.boundMappedToLastCycle ? jmin : jmax;
}
else if (value > vp) {
return jmax - (value - vp) * (jmax - jmin) / this.period;
}
else {
return jmin + (vp - value) * (jmax - jmin) / this.period;
}
return valueToJava2DInverted(value, vp, jmin, jmax);
} else {
return valueToJava2DNormal(value, vp, jmin, jmax);
}
else {
if (value == vp) {
return this.boundMappedToLastCycle ? jmax : jmin;
}
else if (value >= vp) {
return jmin + (value - vp) * (jmax - jmin) / this.period;
}
else {
return jmax - (vp - value) * (jmax - jmin) / this.period;
}
}

private double valueToJava2DInverted(double value, double vp, double jmin, double jmax) {
if (value == vp) {
return this.boundMappedToLastCycle ? jmin : jmax;
} else if (value > vp) {
return jmax - (value - vp) * (jmax - jmin) / this.period;
} else {
return jmin + (vp - value) * (jmax - jmin) / this.period;
}
}

private double valueToJava2DNormal(double value, double vp, double jmin, double jmax) {
if (value == vp) {
return this.boundMappedToLastCycle ? jmax : jmin;
} else if (value >= vp) {
return jmin + (value - vp) * (jmax - jmin) / this.period;
} else {
return jmax - (vp - value) * (jmax - jmin) / this.period;
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/jfree/chart/axis/LogarithmicAxis.java
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ public void zoomRange(double lowerPercent, double upperPercent) {
setRange(adjusted);
}


/**
* Calculates the positions of the tick labels for the axis, storing the
* results in the tick label list (ready for drawing).
Expand Down Expand Up @@ -714,9 +715,7 @@ protected List refreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea,
if (this.smallLogFlag) {
//small log values in use; create numeric value for tick
currentTickValue = Math.pow(10, i) + (Math.pow(10, i) * j);
if (this.expTickLabelsFlag
|| (i < 0 && currentTickValue > 0.0
&& currentTickValue < 1.0)) {
if (shouldShowTickLabel(i, currentTickValue)) {
//showing "1e#"-style ticks or negative exponent
// generating tick value between 0 & 1; show fewer
if (j == 0 || (i > -4 && j < 2)
Expand Down Expand Up @@ -812,7 +811,10 @@ protected List refreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea,
return ticks;

}

public boolean shouldShowTickLabel(int i, double currentTickValue) {
return this.expTickLabelsFlag
|| (i < 0 && currentTickValue > 0.0 && currentTickValue < 1.0);
}
/**
* Calculates the positions of the tick labels for the axis, storing the
* results in the tick label list (ready for drawing).
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/jfree/chart/legend/FixedWidthConstraint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jfree.chart.legend;

import org.jfree.chart.block.LengthConstraintType;
import org.jfree.chart.block.RectangleConstraint;
import org.jfree.chart.block.Size2D;

import java.awt.*;

public class FixedWidthConstraint implements WidthConstraint {
/**
* @param g2
* @param constraint
* @return
*/
@Override
public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
if (constraint.getHeightConstraintType() == LengthConstraintType.NONE) {
throw new RuntimeException("Not yet implemented.");
}
else if (constraint.getHeightConstraintType() == LengthConstraintType.RANGE) {
throw new RuntimeException("Not yet implemented.");
}
else if (constraint.getHeightConstraintType() == LengthConstraintType.FIXED) {
return new Size2D(constraint.getWidth(), constraint.getHeight());
}
return null;
}
}
58 changes: 10 additions & 48 deletions src/main/java/org/jfree/chart/legend/LegendGraphic.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,66 +485,28 @@ public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
LengthConstraintType w = contentConstraint.getWidthConstraintType();
LengthConstraintType h = contentConstraint.getHeightConstraintType();
Size2D contentSize = null;
WidthConstraint widthConstraint;

switch (w) {
case NONE:
if (h == LengthConstraintType.NONE) {
contentSize = arrangeNN(g2);
}
else if (h == LengthConstraintType.RANGE) {
throw new RuntimeException("Not yet implemented.");
}
else if (h == LengthConstraintType.FIXED) {
throw new RuntimeException("Not yet implemented.");
} break;
widthConstraint = new NoneWidthConstraint();
break;
case RANGE:
if (h == LengthConstraintType.NONE) {
throw new RuntimeException("Not yet implemented.");
}
else if (h == LengthConstraintType.RANGE) {
throw new RuntimeException("Not yet implemented.");
}
else if (h == LengthConstraintType.FIXED) {
throw new RuntimeException("Not yet implemented.");
} break;
widthConstraint = new RangeWidthConstraint();
break;
case FIXED:
if (h == LengthConstraintType.NONE) {
throw new RuntimeException("Not yet implemented.");
}
else if (h == LengthConstraintType.RANGE) {
throw new RuntimeException("Not yet implemented.");
}
else if (h == LengthConstraintType.FIXED) {
contentSize = new Size2D(contentConstraint.getWidth(),
contentConstraint.getHeight());
} break;
widthConstraint = new FixedWidthConstraint();
break;
default:
throw new IllegalStateException("Unrecognised widthConstraintType.");
}

contentSize = widthConstraint.arrange(g2, contentConstraint);
assert contentSize != null;
return new Size2D(calculateTotalWidth(contentSize.getWidth()),
calculateTotalHeight(contentSize.getHeight()));
}

/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
protected Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}

/**
* Draws the graphic item within the specified area.
*
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/org/jfree/chart/legend/NoneWidthConstraint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.jfree.chart.legend;

import org.jfree.chart.block.LengthConstraintType;
import org.jfree.chart.block.RectangleConstraint;
import org.jfree.chart.block.Size2D;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class NoneWidthConstraint implements WidthConstraint {
private transient Shape line;
private transient Shape shape;

/**
* @param g2
* @param constraint
* @return
*/
@Override
public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
if (constraint.getHeightConstraintType() == LengthConstraintType.NONE) {
return arrangeNN(g2);
}
else if (constraint.getHeightConstraintType() == LengthConstraintType.RANGE) {
throw new RuntimeException("Not yet implemented.");
}
else if (constraint.getHeightConstraintType() == LengthConstraintType.FIXED) {
throw new RuntimeException("Not yet implemented.");
}
return null;
}
/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
public Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}

}
28 changes: 28 additions & 0 deletions src/main/java/org/jfree/chart/legend/RangeWidthConstraint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jfree.chart.legend;

import org.jfree.chart.block.LengthConstraintType;
import org.jfree.chart.block.RectangleConstraint;
import org.jfree.chart.block.Size2D;

import java.awt.*;

public class RangeWidthConstraint implements WidthConstraint {
/**
* @param g2
* @param constraint
* @return
*/
@Override
public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
if (constraint.getHeightConstraintType() == LengthConstraintType.NONE) {
throw new RuntimeException("Not yet implemented.");
}
else if (constraint.getHeightConstraintType() == LengthConstraintType.RANGE) {
throw new RuntimeException("Not yet implemented.");
}
else if (constraint.getHeightConstraintType() == LengthConstraintType.FIXED) {
throw new RuntimeException("Not yet implemented.");
}
return null;
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/jfree/chart/legend/WidthConstraint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.jfree.chart.legend;

import org.jfree.chart.block.RectangleConstraint;
import org.jfree.chart.block.Size2D;

import java.awt.*;

public interface WidthConstraint {
Size2D arrange(Graphics2D g2, RectangleConstraint constraint);
}
Loading