Search in sources :

Example 16 with TextAnchor

use of org.jfree.chart.text.TextAnchor in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

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 = TextUtils.getTextBounds(label1, g2, g2.getFontMetrics());
    Rectangle2D b2 = TextUtils.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 = TextUtils.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 = TextUtils.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 = TextUtils.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.chart.text.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)

Example 17 with TextAnchor

use of org.jfree.chart.text.TextAnchor in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class SymbolAxis method refreshTicksHorizontal.

/**
 * Calculates the positions of the tick labels for the axis, storing the
 * results in the tick label list (ready for drawing).
 *
 * @param g2  the graphics device.
 * @param dataArea  the area in which the data should be drawn.
 * @param edge  the location of the axis.
 *
 * @return The ticks.
 */
@Override
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 = TextUtils.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) {
                // don't draw this tick label
                tickLabel = "";
            } else {
                // remember these values for next comparison
                previousDrawnTickLabelPos = xx;
                previousDrawnTickLabelLength = tickLabelLength;
            }
            TextAnchor anchor;
            TextAnchor rotationAnchor;
            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(currentTickValue, tickLabel, anchor, rotationAnchor, angle);
            ticks.add(tick);
        }
    }
    return ticks;
}
Also used : TextAnchor(org.jfree.chart.text.TextAnchor) Rectangle2D(java.awt.geom.Rectangle2D) Font(java.awt.Font) Paint(java.awt.Paint) List(java.util.List) NumberFormat(java.text.NumberFormat)

Example 18 with TextAnchor

use of org.jfree.chart.text.TextAnchor in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class Axis method drawLabel.

/**
 * Draws the axis label.
 *
 * @param label  the label text.
 * @param g2  the graphics device.
 * @param plotArea  the plot area.
 * @param dataArea  the area inside the axes.
 * @param edge  the location of the axis.
 * @param state  the axis state ({@code null} not permitted).
 *
 * @return Information about the axis.
 */
protected AxisState drawLabel(String label, Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, AxisState state) {
    // it is unlikely that 'state' will be null, but check anyway...
    Args.nullNotPermitted(state, "state");
    if ((label == null) || (label.equals(""))) {
        return state;
    }
    Font font = getLabelFont();
    RectangleInsets insets = getLabelInsets();
    g2.setFont(font);
    g2.setPaint(getLabelPaint());
    FontMetrics fm = g2.getFontMetrics();
    Rectangle2D labelBounds = TextUtils.getTextBounds(label, g2, fm);
    if (edge == RectangleEdge.TOP) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = labelLocationX(this.labelLocation, dataArea);
        double labely = state.getCursor() - insets.getBottom() - labelBounds.getHeight() / 2.0;
        TextAnchor anchor = labelAnchorH(this.labelLocation);
        TextUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle(), TextAnchor.CENTER);
        state.cursorUp(insets.getTop() + labelBounds.getHeight() + insets.getBottom());
    } else if (edge == RectangleEdge.BOTTOM) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = labelLocationX(this.labelLocation, dataArea);
        double labely = state.getCursor() + insets.getTop() + labelBounds.getHeight() / 2.0;
        TextAnchor anchor = labelAnchorH(this.labelLocation);
        TextUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle(), TextAnchor.CENTER);
        state.cursorDown(insets.getTop() + labelBounds.getHeight() + insets.getBottom());
    } else if (edge == RectangleEdge.LEFT) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle() - Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = state.getCursor() - insets.getRight() - labelBounds.getWidth() / 2.0;
        double labely = labelLocationY(this.labelLocation, dataArea);
        TextAnchor anchor = labelAnchorV(this.labelLocation);
        TextUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle() - Math.PI / 2.0, anchor);
        state.cursorLeft(insets.getLeft() + labelBounds.getWidth() + insets.getRight());
    } else if (edge == RectangleEdge.RIGHT) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle() + Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = state.getCursor() + insets.getLeft() + labelBounds.getWidth() / 2.0;
        double labely = labelLocationY(this.labelLocation, dataArea);
        TextAnchor anchor = labelAnchorV(this.labelLocation);
        TextUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle() + Math.PI / 2.0, anchor);
        state.cursorRight(insets.getLeft() + labelBounds.getWidth() + insets.getRight());
    }
    return state;
}
Also used : TextAnchor(org.jfree.chart.text.TextAnchor) Shape(java.awt.Shape) FontMetrics(java.awt.FontMetrics) Rectangle2D(java.awt.geom.Rectangle2D) RectangleInsets(org.jfree.chart.api.RectangleInsets) AffineTransform(java.awt.geom.AffineTransform) Font(java.awt.Font)

Example 19 with TextAnchor

use of org.jfree.chart.text.TextAnchor in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class Axis method drawAttributedLabel.

/**
 * Draws the axis label.
 *
 * @param label  the label text.
 * @param g2  the graphics device.
 * @param plotArea  the plot area.
 * @param dataArea  the area inside the axes.
 * @param edge  the location of the axis.
 * @param state  the axis state ({@code null} not permitted).
 *
 * @return Information about the axis.
 */
protected AxisState drawAttributedLabel(AttributedString label, Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, AxisState state) {
    // it is unlikely that 'state' will be null, but check anyway...
    Args.nullNotPermitted(state, "state");
    if (label == null) {
        return state;
    }
    RectangleInsets insets = getLabelInsets();
    g2.setFont(getLabelFont());
    g2.setPaint(getLabelPaint());
    TextLayout layout = new TextLayout(this.attributedLabel.getIterator(), g2.getFontRenderContext());
    Rectangle2D labelBounds = layout.getBounds();
    if (edge == RectangleEdge.TOP) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = labelLocationX(this.labelLocation, dataArea);
        double labely = state.getCursor() - insets.getBottom() - labelBounds.getHeight() / 2.0;
        TextAnchor anchor = labelAnchorH(this.labelLocation);
        AttrStringUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle(), TextAnchor.CENTER);
        state.cursorUp(insets.getTop() + labelBounds.getHeight() + insets.getBottom());
    } else if (edge == RectangleEdge.BOTTOM) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = labelLocationX(this.labelLocation, dataArea);
        double labely = state.getCursor() + insets.getTop() + labelBounds.getHeight() / 2.0;
        TextAnchor anchor = labelAnchorH(this.labelLocation);
        AttrStringUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle(), TextAnchor.CENTER);
        state.cursorDown(insets.getTop() + labelBounds.getHeight() + insets.getBottom());
    } else if (edge == RectangleEdge.LEFT) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle() - Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = state.getCursor() - insets.getRight() - labelBounds.getWidth() / 2.0;
        double labely = labelLocationY(this.labelLocation, dataArea);
        TextAnchor anchor = labelAnchorV(this.labelLocation);
        AttrStringUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle() - Math.PI / 2.0, anchor);
        state.cursorLeft(insets.getLeft() + labelBounds.getWidth() + insets.getRight());
    } else if (edge == RectangleEdge.RIGHT) {
        AffineTransform t = AffineTransform.getRotateInstance(getLabelAngle() + Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY());
        Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
        labelBounds = rotatedLabelBounds.getBounds2D();
        double labelx = state.getCursor() + insets.getLeft() + labelBounds.getWidth() / 2.0;
        double labely = labelLocationY(this.labelLocation, dataArea);
        TextAnchor anchor = labelAnchorV(this.labelLocation);
        AttrStringUtils.drawRotatedString(label, g2, (float) labelx, (float) labely, anchor, getLabelAngle() + Math.PI / 2.0, anchor);
        state.cursorRight(insets.getLeft() + labelBounds.getWidth() + insets.getRight());
    }
    return state;
}
Also used : TextAnchor(org.jfree.chart.text.TextAnchor) Shape(java.awt.Shape) Rectangle2D(java.awt.geom.Rectangle2D) RectangleInsets(org.jfree.chart.api.RectangleInsets) AffineTransform(java.awt.geom.AffineTransform) TextLayout(java.awt.font.TextLayout)

Example 20 with TextAnchor

use of org.jfree.chart.text.TextAnchor in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

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);
    assertEquals(t1, t2);
    t1 = new DateTick(d2, l1, ta1, ta1, Math.PI / 2.0);
    assertNotEquals(t1, t2);
    t2 = new DateTick(d2, l1, ta1, ta1, Math.PI / 2.0);
    assertEquals(t1, t2);
    t1 = new DateTick(d1, l2, ta1, ta1, Math.PI / 2.0);
    assertNotEquals(t1, t2);
    t2 = new DateTick(d1, l2, ta1, ta1, Math.PI / 2.0);
    assertEquals(t1, t2);
    t1 = new DateTick(d1, l1, ta2, ta1, Math.PI / 2.0);
    assertNotEquals(t1, t2);
    t2 = new DateTick(d1, l1, ta2, ta1, Math.PI / 2.0);
    assertEquals(t1, t2);
    t1 = new DateTick(d1, l1, ta1, ta2, Math.PI / 2.0);
    assertNotEquals(t1, t2);
    t2 = new DateTick(d1, l1, ta1, ta2, Math.PI / 2.0);
    assertEquals(t1, t2);
    t1 = new DateTick(d1, l1, ta1, ta1, Math.PI / 3.0);
    assertNotEquals(t1, t2);
    t2 = new DateTick(d1, l1, ta1, ta1, Math.PI / 3.0);
    assertEquals(t1, t2);
    t1 = new DateTick(TickType.MINOR, d1, l1, ta1, ta1, Math.PI);
    t2 = new DateTick(TickType.MAJOR, d1, l1, ta1, ta1, Math.PI);
    assertNotEquals(t1, t2);
    t2 = new DateTick(TickType.MINOR, d1, l1, ta1, ta1, Math.PI);
    assertEquals(t1, t2);
}
Also used : TextAnchor(org.jfree.chart.text.TextAnchor) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

TextAnchor (org.jfree.chart.text.TextAnchor)23 Font (java.awt.Font)13 List (java.util.List)10 NumberFormat (java.text.NumberFormat)7 Paint (java.awt.Paint)5 Rectangle2D (java.awt.geom.Rectangle2D)5 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 Shape (java.awt.Shape)4 Test (org.junit.jupiter.api.Test)4 Line2D (java.awt.geom.Line2D)3 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)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 RectangleAnchor (org.jfree.chart.api.RectangleAnchor)2 RectangleInsets (org.jfree.chart.api.RectangleInsets)2