| Method from org.jfree.chart.axis.SymbolAxis Detail: |
protected void autoAdjustRange() {
Plot plot = getPlot();
if (plot == null) {
return; // no plot, no data
}
if (plot instanceof ValueAxisPlot) {
// ensure that all the symbols are displayed
double upper = this.symbols.size() - 1;
double lower = 0;
double range = upper - lower;
// ensure the autorange is at least < minRange > in size...
double minRange = getAutoRangeMinimumSize();
if (range < minRange) {
upper = (upper + lower + minRange) / 2;
lower = (upper + lower - minRange) / 2;
}
// this ensure that the grid bands will be displayed correctly.
double upperMargin = 0.5;
double lowerMargin = 0.5;
if (getAutoRangeIncludesZero()) {
if (getAutoRangeStickyZero()) {
if (upper < = 0.0) {
upper = 0.0;
}
else {
upper = upper + upperMargin;
}
if (lower >= 0.0) {
lower = 0.0;
}
else {
lower = lower - lowerMargin;
}
}
else {
upper = Math.max(0.0, upper + upperMargin);
lower = Math.min(0.0, lower - lowerMargin);
}
}
else {
if (getAutoRangeStickyZero()) {
if (upper < = 0.0) {
upper = Math.min(0.0, upper + upperMargin);
}
else {
upper = upper + upperMargin * range;
}
if (lower >= 0.0) {
lower = Math.max(0.0, lower - lowerMargin);
}
else {
lower = lower - lowerMargin;
}
}
else {
upper = upper + upperMargin;
lower = lower - lowerMargin;
}
}
setRange(new Range(lower, upper), false, false);
}
}
Rescales the axis to ensure that all data is visible. |
public AxisState draw(Graphics2D g2,
double cursor,
Rectangle2D plotArea,
Rectangle2D dataArea,
RectangleEdge edge,
PlotRenderingInfo plotState) {
AxisState info = new AxisState(cursor);
if (isVisible()) {
info = super.draw(g2, cursor, plotArea, dataArea, edge, plotState);
}
if (this.gridBandsVisible) {
drawGridBands(g2, plotArea, dataArea, edge, info.getTicks());
}
return info;
}
Draws the axis on a Java 2D graphics device (such as the screen or a
printer). |
protected void drawGridBands(Graphics2D g2,
Rectangle2D plotArea,
Rectangle2D dataArea,
RectangleEdge edge,
List ticks) {
Shape savedClip = g2.getClip();
g2.clip(dataArea);
if (RectangleEdge.isTopOrBottom(edge)) {
drawGridBandsHorizontal(g2, plotArea, dataArea, true, ticks);
}
else if (RectangleEdge.isLeftOrRight(edge)) {
drawGridBandsVertical(g2, plotArea, dataArea, true, ticks);
}
g2.setClip(savedClip);
}
Draws the grid bands. Alternate bands are colored using
gridBandPaint (DEFAULT_GRID_BAND_PAINT by
default). |
protected void drawGridBandsHorizontal(Graphics2D g2,
Rectangle2D plotArea,
Rectangle2D dataArea,
boolean firstGridBandIsDark,
List ticks) {
boolean currentGridBandIsDark = firstGridBandIsDark;
double yy = dataArea.getY();
double xx1, xx2;
//gets the outline stroke width of the plot
double outlineStrokeWidth;
if (getPlot().getOutlineStroke() != null) {
outlineStrokeWidth
= ((BasicStroke) getPlot().getOutlineStroke()).getLineWidth();
}
else {
outlineStrokeWidth = 1d;
}
Iterator iterator = ticks.iterator();
ValueTick tick;
Rectangle2D band;
while (iterator.hasNext()) {
tick = (ValueTick) iterator.next();
xx1 = valueToJava2D(tick.getValue() - 0.5d, dataArea,
RectangleEdge.BOTTOM);
xx2 = valueToJava2D(tick.getValue() + 0.5d, dataArea,
RectangleEdge.BOTTOM);
if (currentGridBandIsDark) {
g2.setPaint(this.gridBandPaint);
}
else {
g2.setPaint(Color.white);
}
band = new Rectangle2D.Double(xx1, yy + outlineStrokeWidth,
xx2 - xx1, dataArea.getMaxY() - yy - outlineStrokeWidth);
g2.fill(band);
currentGridBandIsDark = !currentGridBandIsDark;
}
g2.setPaintMode();
}
Draws the grid bands for the axis when it is at the top or bottom of
the plot. |
protected void drawGridBandsVertical(Graphics2D g2,
Rectangle2D drawArea,
Rectangle2D plotArea,
boolean firstGridBandIsDark,
List ticks) {
boolean currentGridBandIsDark = firstGridBandIsDark;
double xx = plotArea.getX();
double yy1, yy2;
//gets the outline stroke width of the plot
double outlineStrokeWidth;
Stroke outlineStroke = getPlot().getOutlineStroke();
if (outlineStroke != null && outlineStroke instanceof BasicStroke) {
outlineStrokeWidth = ((BasicStroke) outlineStroke).getLineWidth();
}
else {
outlineStrokeWidth = 1d;
}
Iterator iterator = ticks.iterator();
ValueTick tick;
Rectangle2D band;
while (iterator.hasNext()) {
tick = (ValueTick) iterator.next();
yy1 = valueToJava2D(tick.getValue() + 0.5d, plotArea,
RectangleEdge.LEFT);
yy2 = valueToJava2D(tick.getValue() - 0.5d, plotArea,
RectangleEdge.LEFT);
if (currentGridBandIsDark) {
g2.setPaint(this.gridBandPaint);
}
else {
g2.setPaint(Color.white);
}
band = new Rectangle2D.Double(xx + outlineStrokeWidth, yy1,
plotArea.getMaxX() - xx - outlineStrokeWidth, yy2 - yy1);
g2.fill(band);
currentGridBandIsDark = !currentGridBandIsDark;
}
g2.setPaintMode();
}
Draws the grid bands for the axis when it is at the top or bottom of
the plot. |
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof SymbolAxis)) {
return false;
}
SymbolAxis that = (SymbolAxis) obj;
if (!this.symbols.equals(that.symbols)) {
return false;
}
if (this.gridBandsVisible != that.gridBandsVisible) {
return false;
}
if (!PaintUtilities.equal(this.gridBandPaint, that.gridBandPaint)) {
return false;
}
if (!PaintUtilities.equal(this.gridBandAlternatePaint,
that.gridBandAlternatePaint)) {
return false;
}
return super.equals(obj);
}
Tests this axis for equality with an arbitrary object. |
public Paint getGridBandAlternatePaint() {
return this.gridBandAlternatePaint;
}
Returns the paint used for alternate grid bands. |
public Paint getGridBandPaint() {
return this.gridBandPaint;
}
Returns the paint used to color the grid bands. |
public String[] getSymbols() {
String[] result = new String[this.symbols.size()];
result = (String[]) this.symbols.toArray(result);
return result;
}
Returns an array of the symbols for the axis. |
public boolean isGridBandsVisible() {
return this.gridBandsVisible;
}
Returns true if the grid bands are showing, and
false otherwise. |
public List refreshTicks(Graphics2D g2,
AxisState state,
Rectangle2D dataArea,
RectangleEdge edge) {
List ticks = null;
if (RectangleEdge.isTopOrBottom(edge)) {
ticks = refreshTicksHorizontal(g2, dataArea, edge);
}
else if (RectangleEdge.isLeftOrRight(edge)) {
ticks = refreshTicksVertical(g2, dataArea, edge);
}
return ticks;
}
Calculates the positions of the tick labels for the axis, storing the
results in the tick label list (ready for drawing). |
protected List refreshTicksHorizontal(Graphics2D g2,
Rectangle2D dataArea,
RectangleEdge edge) {
List ticks = new java.util.ArrayList();
Font tickLabelFont = getTickLabelFont();
g2.setFont(tickLabelFont);
double size = getTickUnit().getSize();
int count = calculateVisibleTickCount();
double lowestTickValue = calculateLowestVisibleTickValue();
double previousDrawnTickLabelPos = 0.0;
double previousDrawnTickLabelLength = 0.0;
if (count < = ValueAxis.MAXIMUM_TICK_COUNT) {
for (int i = 0; i < count; i++) {
double currentTickValue = lowestTickValue + (i * size);
double xx = valueToJava2D(currentTickValue, dataArea, edge);
String tickLabel;
NumberFormat formatter = getNumberFormatOverride();
if (formatter != null) {
tickLabel = formatter.format(currentTickValue);
}
else {
tickLabel = valueToString(currentTickValue);
}
// avoid to draw overlapping tick labels
Rectangle2D bounds = TextUtilities.getTextBounds(tickLabel, g2,
g2.getFontMetrics());
double tickLabelLength = isVerticalTickLabels()
? bounds.getHeight() : bounds.getWidth();
boolean tickLabelsOverlapping = false;
if (i > 0) {
double avgTickLabelLength = (previousDrawnTickLabelLength
+ tickLabelLength) / 2.0;
if (Math.abs(xx - previousDrawnTickLabelPos)
< avgTickLabelLength) {
tickLabelsOverlapping = true;
}
}
if (tickLabelsOverlapping) {
tickLabel = ""; // don't draw this tick label
}
else {
// remember these values for next comparison
previousDrawnTickLabelPos = xx;
previousDrawnTickLabelLength = tickLabelLength;
}
TextAnchor anchor = null;
TextAnchor rotationAnchor = null;
double angle = 0.0;
if (isVerticalTickLabels()) {
anchor = TextAnchor.CENTER_RIGHT;
rotationAnchor = TextAnchor.CENTER_RIGHT;
if (edge == RectangleEdge.TOP) {
angle = Math.PI / 2.0;
}
else {
angle = -Math.PI / 2.0;
}
}
else {
if (edge == RectangleEdge.TOP) {
anchor = TextAnchor.BOTTOM_CENTER;
rotationAnchor = TextAnchor.BOTTOM_CENTER;
}
else {
anchor = TextAnchor.TOP_CENTER;
rotationAnchor = TextAnchor.TOP_CENTER;
}
}
Tick tick = new NumberTick(new Double(currentTickValue),
tickLabel, anchor, rotationAnchor, angle);
ticks.add(tick);
}
}
return ticks;
}
Calculates the positions of the tick labels for the axis, storing the
results in the tick label list (ready for drawing). |
protected List refreshTicksVertical(Graphics2D g2,
Rectangle2D dataArea,
RectangleEdge edge) {
List ticks = new java.util.ArrayList();
Font tickLabelFont = getTickLabelFont();
g2.setFont(tickLabelFont);
double size = getTickUnit().getSize();
int count = calculateVisibleTickCount();
double lowestTickValue = calculateLowestVisibleTickValue();
double previousDrawnTickLabelPos = 0.0;
double previousDrawnTickLabelLength = 0.0;
if (count < = ValueAxis.MAXIMUM_TICK_COUNT) {
for (int i = 0; i < count; i++) {
double currentTickValue = lowestTickValue + (i * size);
double yy = valueToJava2D(currentTickValue, dataArea, edge);
String tickLabel;
NumberFormat formatter = getNumberFormatOverride();
if (formatter != null) {
tickLabel = formatter.format(currentTickValue);
}
else {
tickLabel = valueToString(currentTickValue);
}
// avoid to draw overlapping tick labels
Rectangle2D bounds = TextUtilities.getTextBounds(tickLabel, g2,
g2.getFontMetrics());
double tickLabelLength = isVerticalTickLabels()
? bounds.getWidth() : bounds.getHeight();
boolean tickLabelsOverlapping = false;
if (i > 0) {
double avgTickLabelLength = (previousDrawnTickLabelLength
+ tickLabelLength) / 2.0;
if (Math.abs(yy - previousDrawnTickLabelPos)
< avgTickLabelLength) {
tickLabelsOverlapping = true;
}
}
if (tickLabelsOverlapping) {
tickLabel = ""; // don't draw this tick label
}
else {
// remember these values for next comparison
previousDrawnTickLabelPos = yy;
previousDrawnTickLabelLength = tickLabelLength;
}
TextAnchor anchor = null;
TextAnchor rotationAnchor = null;
double angle = 0.0;
if (isVerticalTickLabels()) {
anchor = TextAnchor.BOTTOM_CENTER;
rotationAnchor = TextAnchor.BOTTOM_CENTER;
if (edge == RectangleEdge.LEFT) {
angle = -Math.PI / 2.0;
}
else {
angle = Math.PI / 2.0;
}
}
else {
if (edge == RectangleEdge.LEFT) {
anchor = TextAnchor.CENTER_RIGHT;
rotationAnchor = TextAnchor.CENTER_RIGHT;
}
else {
anchor = TextAnchor.CENTER_LEFT;
rotationAnchor = TextAnchor.CENTER_LEFT;
}
}
Tick tick = new NumberTick(new Double(currentTickValue),
tickLabel, anchor, rotationAnchor, angle);
ticks.add(tick);
}
}
return ticks;
}
Calculates the positions of the tick labels for the axis, storing the
results in the tick label list (ready for drawing). |
protected void selectAutoTickUnit(Graphics2D g2,
Rectangle2D dataArea,
RectangleEdge edge) {
throw new UnsupportedOperationException();
}
This operation is not supported by this axis. |
public void setGridBandAlternatePaint(Paint paint) {
if (paint == null) {
throw new IllegalArgumentException("Null 'paint' argument.");
}
this.gridBandAlternatePaint = paint;
notifyListeners(new AxisChangeEvent(this));
}
Sets the paint used for alternate grid bands and sends a
AxisChangeEvent to all registered listeners. |
public void setGridBandPaint(Paint paint) {
if (paint == null) {
throw new IllegalArgumentException("Null 'paint' argument.");
}
this.gridBandPaint = paint;
notifyListeners(new AxisChangeEvent(this));
}
Sets the grid band paint and sends an AxisChangeEvent to
all registered listeners. |
public void setGridBandsVisible(boolean flag) {
if (this.gridBandsVisible != flag) {
this.gridBandsVisible = flag;
notifyListeners(new AxisChangeEvent(this));
}
}
Sets the visibility of the grid bands and notifies registered
listeners that the axis has been modified. |
public String valueToString(double value) {
String strToReturn;
try {
strToReturn = (String) this.symbols.get((int) value);
}
catch (IndexOutOfBoundsException ex) {
strToReturn = "";
}
return strToReturn;
}
Converts a value to a string, using the list of symbols. |