Search in sources :

Example 16 with TextAnchor

use of org.jfree.ui.TextAnchor in project SIMVA-SoS by SESoS.

the class CategoryTickTest method testEquals.

/**
 * Confirm that the equals method can distinguish all the required fields.
 */
@Test
public void testEquals() {
    Comparable c1 = "C1";
    Comparable c2 = "C2";
    TextBlock tb1 = new TextBlock();
    tb1.addLine(new TextLine("Block 1"));
    TextBlock tb2 = new TextBlock();
    tb1.addLine(new TextLine("Block 2"));
    TextBlockAnchor tba1 = TextBlockAnchor.CENTER;
    TextBlockAnchor tba2 = TextBlockAnchor.BOTTOM_CENTER;
    TextAnchor ta1 = TextAnchor.CENTER;
    TextAnchor ta2 = TextAnchor.BASELINE_LEFT;
    CategoryTick t1 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
    CategoryTick t2 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
    assertTrue(t1.equals(t2));
    t1 = new CategoryTick(c2, tb1, tba1, ta1, 1.0f);
    assertFalse(t1.equals(t2));
    t2 = new CategoryTick(c2, tb1, tba1, ta1, 1.0f);
    assertTrue(t1.equals(t2));
    t1 = new CategoryTick(c2, tb2, tba1, ta1, 1.0f);
    assertFalse(t1.equals(t2));
    t2 = new CategoryTick(c2, tb2, tba1, ta1, 1.0f);
    assertTrue(t1.equals(t2));
    t1 = new CategoryTick(c2, tb2, tba2, ta1, 1.0f);
    assertFalse(t1.equals(t2));
    t2 = new CategoryTick(c2, tb2, tba2, ta1, 1.0f);
    assertTrue(t1.equals(t2));
    t1 = new CategoryTick(c2, tb2, tba2, ta2, 1.0f);
    assertFalse(t1.equals(t2));
    t2 = new CategoryTick(c2, tb2, tba2, ta2, 1.0f);
    assertTrue(t1.equals(t2));
    t1 = new CategoryTick(c2, tb2, tba2, ta2, 2.0f);
    assertFalse(t1.equals(t2));
    t2 = new CategoryTick(c2, tb2, tba2, ta2, 2.0f);
    assertTrue(t1.equals(t2));
}
Also used : TextAnchor(org.jfree.ui.TextAnchor) TextLine(org.jfree.text.TextLine) TextBlockAnchor(org.jfree.text.TextBlockAnchor) TextBlock(org.jfree.text.TextBlock) Test(org.junit.Test)

Example 17 with TextAnchor

use of org.jfree.ui.TextAnchor in project SIMVA-SoS by SESoS.

the class DateTickTest method testHashCode.

/**
 * Two objects that are equal are required to return the same hashCode.
 */
@Test
public void testHashCode() {
    Date d1 = new Date(0L);
    String l1 = "Label 1";
    TextAnchor ta1 = TextAnchor.CENTER;
    DateTick t1 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0);
    DateTick t2 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0);
    assertTrue(t1.equals(t2));
    int h1 = t1.hashCode();
    int h2 = t2.hashCode();
    assertEquals(h1, h2);
}
Also used : TextAnchor(org.jfree.ui.TextAnchor) Date(java.util.Date) Test(org.junit.Test)

Example 18 with TextAnchor

use of org.jfree.ui.TextAnchor in project SIMVA-SoS by SESoS.

the class DateTickTest method testEquals.

/**
 * Confirm that the equals method can distinguish all the required fields.
 */
@Test
public void testEquals() {
    Date d1 = new Date(0L);
    Date d2 = new Date(1L);
    String l1 = "Label 1";
    String l2 = "Label 2";
    TextAnchor ta1 = TextAnchor.CENTER;
    TextAnchor ta2 = TextAnchor.BASELINE_LEFT;
    DateTick t1 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0);
    DateTick t2 = new DateTick(d1, l1, ta1, ta1, Math.PI / 2.0);
    assertTrue(t1.equals(t2));
    t1 = new DateTick(d2, l1, ta1, ta1, Math.PI / 2.0);
    assertFalse(t1.equals(t2));
    t2 = new DateTick(d2, l1, ta1, ta1, Math.PI / 2.0);
    assertTrue(t1.equals(t2));
    t1 = new DateTick(d1, l2, ta1, ta1, Math.PI / 2.0);
    assertFalse(t1.equals(t2));
    t2 = new DateTick(d1, l2, ta1, ta1, Math.PI / 2.0);
    assertTrue(t1.equals(t2));
    t1 = new DateTick(d1, l1, ta2, ta1, Math.PI / 2.0);
    assertFalse(t1.equals(t2));
    t2 = new DateTick(d1, l1, ta2, ta1, Math.PI / 2.0);
    assertTrue(t1.equals(t2));
    t1 = new DateTick(d1, l1, ta1, ta2, Math.PI / 2.0);
    assertFalse(t1.equals(t2));
    t2 = new DateTick(d1, l1, ta1, ta2, Math.PI / 2.0);
    assertTrue(t1.equals(t2));
    t1 = new DateTick(d1, l1, ta1, ta1, Math.PI / 3.0);
    assertFalse(t1.equals(t2));
    t2 = new DateTick(d1, l1, ta1, ta1, Math.PI / 3.0);
    assertTrue(t1.equals(t2));
    t1 = new DateTick(TickType.MINOR, d1, l1, ta1, ta1, Math.PI);
    t2 = new DateTick(TickType.MAJOR, d1, l1, ta1, ta1, Math.PI);
    assertFalse(t1.equals(t2));
    t2 = new DateTick(TickType.MINOR, d1, l1, ta1, ta1, Math.PI);
    assertTrue(t1.equals(t2));
}
Also used : TextAnchor(org.jfree.ui.TextAnchor) Date(java.util.Date) Test(org.junit.Test)

Example 19 with TextAnchor

use of org.jfree.ui.TextAnchor in project SIMVA-SoS by SESoS.

the class LogAxis method refreshTicksVertical.

/**
 * Returns a list of ticks for an axis at the left or right of the chart.
 *
 * @param g2  the graphics device ({@code null} not permitted).
 * @param dataArea  the data area ({@code null} not permitted).
 * @param edge  the edge that the axis is aligned to ({@code null}
 *     not permitted).
 *
 * @return A list of ticks.
 */
protected List refreshTicksVertical(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) {
    Range range = getRange();
    List ticks = new ArrayList();
    Font tickLabelFont = getTickLabelFont();
    g2.setFont(tickLabelFont);
    TextAnchor textAnchor;
    if (edge == RectangleEdge.RIGHT) {
        textAnchor = TextAnchor.CENTER_LEFT;
    } else {
        textAnchor = TextAnchor.CENTER_RIGHT;
    }
    if (isAutoTickUnitSelection()) {
        selectAutoTickUnit(g2, dataArea, edge);
    }
    int minorTickCount = this.tickUnit.getMinorTickCount();
    double unit = getTickUnit().getSize();
    double index = Math.ceil(calculateLog(getRange().getLowerBound()) / unit);
    double start = index * unit;
    double end = calculateLog(getUpperBound());
    double current = start;
    boolean hasTicks = (this.tickUnit.getSize() > 0.0) && !Double.isInfinite(start);
    while (hasTicks && current <= end) {
        double v = calculateValueNoINF(current);
        if (range.contains(v)) {
            ticks.add(new LogTick(TickType.MAJOR, v, createTickLabel(v), textAnchor));
        }
        // add minor ticks (for gridlines)
        double next = Math.pow(this.base, current + this.tickUnit.getSize());
        for (int i = 1; i < minorTickCount; i++) {
            double minorV = v + i * ((next - v) / minorTickCount);
            if (range.contains(minorV)) {
                ticks.add(new LogTick(TickType.MINOR, minorV, null, textAnchor));
            }
        }
        current = current + this.tickUnit.getSize();
    }
    return ticks;
}
Also used : TextAnchor(org.jfree.ui.TextAnchor) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Range(org.jfree.data.Range) Font(java.awt.Font)

Example 20 with TextAnchor

use of org.jfree.ui.TextAnchor in project SIMVA-SoS by SESoS.

the class PeriodAxis method drawTickLabels.

/**
 * Draws the tick labels for one "band" of time periods.
 *
 * @param band  the band index (zero-based).
 * @param g2  the graphics device.
 * @param state  the axis state.
 * @param dataArea  the data area.
 * @param edge  the edge where the axis is located.
 *
 * @return The updated axis state.
 */
protected AxisState drawTickLabels(int band, Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge) {
    // work out the initial gap
    double delta1 = 0.0;
    FontMetrics fm = g2.getFontMetrics(this.labelInfo[band].getLabelFont());
    if (edge == RectangleEdge.BOTTOM) {
        delta1 = this.labelInfo[band].getPadding().calculateTopOutset(fm.getHeight());
    } else if (edge == RectangleEdge.TOP) {
        delta1 = this.labelInfo[band].getPadding().calculateBottomOutset(fm.getHeight());
    }
    state.moveCursor(delta1, edge);
    long axisMin = this.first.getFirstMillisecond();
    long axisMax = this.last.getLastMillisecond();
    g2.setFont(this.labelInfo[band].getLabelFont());
    g2.setPaint(this.labelInfo[band].getLabelPaint());
    // work out the number of periods to skip for labelling
    RegularTimePeriod p1 = this.labelInfo[band].createInstance(new Date(axisMin), this.timeZone, this.locale);
    RegularTimePeriod p2 = this.labelInfo[band].createInstance(new Date(axisMax), this.timeZone, this.locale);
    DateFormat df = this.labelInfo[band].getDateFormat();
    df.setTimeZone(this.timeZone);
    String label1 = df.format(new Date(p1.getMiddleMillisecond()));
    String label2 = df.format(new Date(p2.getMiddleMillisecond()));
    Rectangle2D b1 = TextUtilities.getTextBounds(label1, g2, g2.getFontMetrics());
    Rectangle2D b2 = TextUtilities.getTextBounds(label2, g2, g2.getFontMetrics());
    double w = Math.max(b1.getWidth(), b2.getWidth());
    long ww = Math.round(java2DToValue(dataArea.getX() + w + 5.0, dataArea, edge));
    if (isInverted()) {
        ww = axisMax - ww;
    } else {
        ww = ww - axisMin;
    }
    long length = p1.getLastMillisecond() - p1.getFirstMillisecond();
    int periods = (int) (ww / length) + 1;
    RegularTimePeriod p = this.labelInfo[band].createInstance(new Date(axisMin), this.timeZone, this.locale);
    Rectangle2D b = null;
    long lastXX = 0L;
    float y = (float) (state.getCursor());
    TextAnchor anchor = TextAnchor.TOP_CENTER;
    float yDelta = (float) b1.getHeight();
    if (edge == RectangleEdge.TOP) {
        anchor = TextAnchor.BOTTOM_CENTER;
        yDelta = -yDelta;
    }
    while (p.getFirstMillisecond() <= axisMax) {
        float x = (float) valueToJava2D(p.getMiddleMillisecond(), dataArea, edge);
        String label = df.format(new Date(p.getMiddleMillisecond()));
        long first = p.getFirstMillisecond();
        long last = p.getLastMillisecond();
        if (last > axisMax) {
            // this is the last period, but it is only partially visible
            // so check that the label will fit before displaying it...
            Rectangle2D bb = TextUtilities.getTextBounds(label, g2, g2.getFontMetrics());
            if ((x + bb.getWidth() / 2) > dataArea.getMaxX()) {
                float xstart = (float) valueToJava2D(Math.max(first, axisMin), dataArea, edge);
                if (bb.getWidth() < (dataArea.getMaxX() - xstart)) {
                    x = ((float) dataArea.getMaxX() + xstart) / 2.0f;
                } else {
                    label = null;
                }
            }
        }
        if (first < axisMin) {
            // this is the first period, but it is only partially visible
            // so check that the label will fit before displaying it...
            Rectangle2D bb = TextUtilities.getTextBounds(label, g2, g2.getFontMetrics());
            if ((x - bb.getWidth() / 2) < dataArea.getX()) {
                float xlast = (float) valueToJava2D(Math.min(last, axisMax), dataArea, edge);
                if (bb.getWidth() < (xlast - dataArea.getX())) {
                    x = (xlast + (float) dataArea.getX()) / 2.0f;
                } else {
                    label = null;
                }
            }
        }
        if (label != null) {
            g2.setPaint(this.labelInfo[band].getLabelPaint());
            b = TextUtilities.drawAlignedString(label, g2, x, y, anchor);
        }
        if (lastXX > 0L) {
            if (this.labelInfo[band].getDrawDividers()) {
                long nextXX = p.getFirstMillisecond();
                long mid = (lastXX + nextXX) / 2;
                float mid2d = (float) valueToJava2D(mid, dataArea, edge);
                g2.setStroke(this.labelInfo[band].getDividerStroke());
                g2.setPaint(this.labelInfo[band].getDividerPaint());
                g2.draw(new Line2D.Float(mid2d, y, mid2d, y + yDelta));
            }
        }
        lastXX = last;
        for (int i = 0; i < periods; i++) {
            p = p.next();
        }
        p.peg(this.calendar);
    }
    double used = 0.0;
    if (b != null) {
        used = b.getHeight();
        // work out the trailing gap
        if (edge == RectangleEdge.BOTTOM) {
            used += this.labelInfo[band].getPadding().calculateBottomOutset(fm.getHeight());
        } else if (edge == RectangleEdge.TOP) {
            used += this.labelInfo[band].getPadding().calculateTopOutset(fm.getHeight());
        }
    }
    state.moveCursor(used, edge);
    return state;
}
Also used : TextAnchor(org.jfree.ui.TextAnchor) Rectangle2D(java.awt.geom.Rectangle2D) Line2D(java.awt.geom.Line2D) Date(java.util.Date) Paint(java.awt.Paint) FontMetrics(java.awt.FontMetrics) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod)

Aggregations

TextAnchor (org.jfree.ui.TextAnchor)23 List (java.util.List)13 Font (java.awt.Font)11 NumberFormat (java.text.NumberFormat)7 Paint (java.awt.Paint)5 Rectangle2D (java.awt.geom.Rectangle2D)5 Date (java.util.Date)5 Shape (java.awt.Shape)4 Test (org.junit.Test)4 Line2D (java.awt.geom.Line2D)3 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ArrayList (java.util.ArrayList)3 Range (org.jfree.data.Range)3 FontMetrics (java.awt.FontMetrics)2 Stroke (java.awt.Stroke)2 AffineTransform (java.awt.geom.AffineTransform)2 Point2D (java.awt.geom.Point2D)2 TextBlock (org.jfree.text.TextBlock)2 TextBlockAnchor (org.jfree.text.TextBlockAnchor)2