Search in sources :

Example 1 with TextAnchor

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

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.
 *
 * @since 1.0.16
 */
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...
    ParamChecks.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.ui.TextAnchor) Shape(java.awt.Shape) Rectangle2D(java.awt.geom.Rectangle2D) RectangleInsets(org.jfree.ui.RectangleInsets) AffineTransform(java.awt.geom.AffineTransform) TextLayout(java.awt.font.TextLayout)

Example 2 with TextAnchor

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

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...
    ParamChecks.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 = TextUtilities.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);
        TextUtilities.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);
        TextUtilities.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);
        TextUtilities.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);
        TextUtilities.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.ui.TextAnchor) Shape(java.awt.Shape) FontMetrics(java.awt.FontMetrics) Rectangle2D(java.awt.geom.Rectangle2D) RectangleInsets(org.jfree.ui.RectangleInsets) AffineTransform(java.awt.geom.AffineTransform) Font(java.awt.Font)

Example 3 with TextAnchor

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

the class CrosshairOverlay method drawVerticalCrosshair.

/**
 * Draws a crosshair vertically on the plot.
 *
 * @param g2  the graphics target.
 * @param dataArea  the data area.
 * @param x  the x-value in Java2D space.
 * @param crosshair  the crosshair.
 */
protected void drawVerticalCrosshair(Graphics2D g2, Rectangle2D dataArea, double x, Crosshair crosshair) {
    if (x >= dataArea.getMinX() && x <= dataArea.getMaxX()) {
        Line2D line = new Line2D.Double(x, dataArea.getMinY(), x, dataArea.getMaxY());
        Paint savedPaint = g2.getPaint();
        Stroke savedStroke = g2.getStroke();
        g2.setPaint(crosshair.getPaint());
        g2.setStroke(crosshair.getStroke());
        g2.draw(line);
        if (crosshair.isLabelVisible()) {
            String label = crosshair.getLabelGenerator().generateLabel(crosshair);
            RectangleAnchor anchor = crosshair.getLabelAnchor();
            Point2D pt = calculateLabelPoint(line, anchor, 5, 5);
            float xx = (float) pt.getX();
            float yy = (float) pt.getY();
            TextAnchor alignPt = textAlignPtForLabelAnchorV(anchor);
            Shape hotspot = TextUtilities.calculateRotatedStringBounds(label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
            if (!dataArea.contains(hotspot.getBounds2D())) {
                anchor = flipAnchorH(anchor);
                pt = calculateLabelPoint(line, anchor, 5, 5);
                xx = (float) pt.getX();
                yy = (float) pt.getY();
                alignPt = textAlignPtForLabelAnchorV(anchor);
                hotspot = TextUtilities.calculateRotatedStringBounds(label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
            }
            g2.setPaint(crosshair.getLabelBackgroundPaint());
            g2.fill(hotspot);
            g2.setPaint(crosshair.getLabelOutlinePaint());
            g2.draw(hotspot);
            TextUtilities.drawAlignedString(label, g2, xx, yy, alignPt);
        }
        g2.setPaint(savedPaint);
        g2.setStroke(savedStroke);
    }
}
Also used : Stroke(java.awt.Stroke) TextAnchor(org.jfree.ui.TextAnchor) Shape(java.awt.Shape) Point2D(java.awt.geom.Point2D) RectangleAnchor(org.jfree.ui.RectangleAnchor) Paint(java.awt.Paint) Line2D(java.awt.geom.Line2D)

Example 4 with TextAnchor

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

the class CrosshairOverlay method drawHorizontalCrosshair.

/**
 * Draws a crosshair horizontally across the plot.
 *
 * @param g2  the graphics target.
 * @param dataArea  the data area.
 * @param y  the y-value in Java2D space.
 * @param crosshair  the crosshair.
 */
protected void drawHorizontalCrosshair(Graphics2D g2, Rectangle2D dataArea, double y, Crosshair crosshair) {
    if (y >= dataArea.getMinY() && y <= dataArea.getMaxY()) {
        Line2D line = new Line2D.Double(dataArea.getMinX(), y, dataArea.getMaxX(), y);
        Paint savedPaint = g2.getPaint();
        Stroke savedStroke = g2.getStroke();
        g2.setPaint(crosshair.getPaint());
        g2.setStroke(crosshair.getStroke());
        g2.draw(line);
        if (crosshair.isLabelVisible()) {
            String label = crosshair.getLabelGenerator().generateLabel(crosshair);
            RectangleAnchor anchor = crosshair.getLabelAnchor();
            Point2D pt = calculateLabelPoint(line, anchor, 5, 5);
            float xx = (float) pt.getX();
            float yy = (float) pt.getY();
            TextAnchor alignPt = textAlignPtForLabelAnchorH(anchor);
            Shape hotspot = TextUtilities.calculateRotatedStringBounds(label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
            if (!dataArea.contains(hotspot.getBounds2D())) {
                anchor = flipAnchorV(anchor);
                pt = calculateLabelPoint(line, anchor, 5, 5);
                xx = (float) pt.getX();
                yy = (float) pt.getY();
                alignPt = textAlignPtForLabelAnchorH(anchor);
                hotspot = TextUtilities.calculateRotatedStringBounds(label, g2, xx, yy, alignPt, 0.0, TextAnchor.CENTER);
            }
            g2.setPaint(crosshair.getLabelBackgroundPaint());
            g2.fill(hotspot);
            g2.setPaint(crosshair.getLabelOutlinePaint());
            g2.draw(hotspot);
            TextUtilities.drawAlignedString(label, g2, xx, yy, alignPt);
        }
        g2.setPaint(savedPaint);
        g2.setStroke(savedStroke);
    }
}
Also used : Stroke(java.awt.Stroke) TextAnchor(org.jfree.ui.TextAnchor) Shape(java.awt.Shape) Point2D(java.awt.geom.Point2D) RectangleAnchor(org.jfree.ui.RectangleAnchor) Paint(java.awt.Paint) Line2D(java.awt.geom.Line2D)

Example 5 with TextAnchor

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

the class LogarithmicAxis method refreshTicksVertical.

/**
 * 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 plot should be drawn.
 * @param edge  the location of the axis.
 *
 * @return A list of ticks.
 */
@Override
protected List refreshTicksVertical(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) {
    List ticks = new java.util.ArrayList();
    // get lower bound value:
    double lowerBoundVal = getRange().getLowerBound();
    // then set to a small value (don't allow <= 0):
    if (this.smallLogFlag && lowerBoundVal < SMALL_LOG_VALUE) {
        lowerBoundVal = SMALL_LOG_VALUE;
    }
    // get upper bound value
    double upperBoundVal = getRange().getUpperBound();
    // get log10 version of lower bound and round to integer:
    int iBegCount = (int) Math.rint(switchedLog10(lowerBoundVal));
    // get log10 version of upper bound and round to integer:
    int iEndCount = (int) Math.rint(switchedLog10(upperBoundVal));
    if (iBegCount == iEndCount && iBegCount > 0 && Math.pow(10, iBegCount) > lowerBoundVal) {
        // only 1 power of 10 value, it's > 0 and its resulting
        // tick value will be larger than lower bound of data
        // decrement to generate more ticks
        --iBegCount;
    }
    double tickVal;
    String tickLabel;
    boolean zeroTickFlag = false;
    for (int i = iBegCount; i <= iEndCount; i++) {
        // for each tick with a label to be displayed
        int jEndCount = 10;
        if (i == iEndCount) {
            jEndCount = 1;
        }
        for (int j = 0; j < jEndCount; j++) {
            // for each tick to be displayed
            if (this.smallLogFlag) {
                // small log values in use
                tickVal = Math.pow(10, i) + (Math.pow(10, i) * j);
                if (j == 0) {
                    // first tick of group; create label text
                    if (this.log10TickLabelsFlag) {
                        // if flag then
                        // create "log10"-type label
                        tickLabel = "10^" + i;
                    } else {
                        // not "log10"-type label
                        if (this.expTickLabelsFlag) {
                            // if flag then
                            // create "1e#"-type label
                            tickLabel = "1e" + i;
                        } else {
                            // not "1e#"-type label
                            if (i >= 0) {
                                // if positive exponent then
                                // make integer
                                NumberFormat format = getNumberFormatOverride();
                                if (format != null) {
                                    tickLabel = format.format(tickVal);
                                } else {
                                    tickLabel = Long.toString((long) Math.rint(tickVal));
                                }
                            } else {
                                // negative exponent; create fractional value
                                // set exact number of fractional digits to
                                // be shown:
                                this.numberFormatterObj.setMaximumFractionDigits(-i);
                                // create tick label:
                                tickLabel = this.numberFormatterObj.format(tickVal);
                            }
                        }
                    }
                } else {
                    // not first tick to be displayed
                    // no tick label
                    tickLabel = "";
                }
            } else {
                // not small log values in use; allow for values <= 0
                if (zeroTickFlag) {
                    // if did zero tick last iter then
                    --j;
                }
                // decrement to do 1.0 tick now
                tickVal = (i >= 0) ? Math.pow(10, i) + (Math.pow(10, i) * j) : -(Math.pow(10, -i) - (Math.pow(10, -i - 1) * j));
                if (j == 0) {
                    // first tick of group
                    if (!zeroTickFlag) {
                        // iteration
                        if (i > iBegCount && i < iEndCount && Math.abs(tickVal - 1.0) < 0.0001) {
                            // not first or last tick on graph and value
                            // is 1.0
                            // change value to 0.0
                            tickVal = 0.0;
                            // indicate zero tick
                            zeroTickFlag = true;
                            // create label for tick
                            tickLabel = "0";
                        } else {
                            // create label for tick:
                            if (this.log10TickLabelsFlag) {
                                // create "log10"-type label
                                tickLabel = (((i < 0) ? "-" : "") + "10^" + Math.abs(i));
                            } else {
                                if (this.expTickLabelsFlag) {
                                    // create "1e#"-type label
                                    tickLabel = (((i < 0) ? "-" : "") + "1e" + Math.abs(i));
                                } else {
                                    NumberFormat format = getNumberFormatOverride();
                                    if (format != null) {
                                        tickLabel = format.format(tickVal);
                                    } else {
                                        tickLabel = Long.toString((long) Math.rint(tickVal));
                                    }
                                }
                            }
                        }
                    } else {
                        // did zero tick last iteration
                        // no label
                        tickLabel = "";
                        // clear flag
                        zeroTickFlag = false;
                    }
                } else {
                    // not first tick of group
                    // no label
                    tickLabel = "";
                    // make sure flag cleared
                    zeroTickFlag = false;
                }
            }
            if (tickVal > upperBoundVal) {
                // if past highest data value then exit method
                return ticks;
            }
            if (tickVal >= lowerBoundVal - SMALL_LOG_VALUE) {
                // tick value not below lowest data value
                TextAnchor anchor;
                TextAnchor rotationAnchor;
                double angle = 0.0;
                if (isVerticalTickLabels()) {
                    if (edge == RectangleEdge.LEFT) {
                        anchor = TextAnchor.BOTTOM_CENTER;
                        rotationAnchor = TextAnchor.BOTTOM_CENTER;
                        angle = -Math.PI / 2.0;
                    } else {
                        anchor = TextAnchor.BOTTOM_CENTER;
                        rotationAnchor = TextAnchor.BOTTOM_CENTER;
                        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;
                    }
                }
                // create tick object and add to list:
                ticks.add(new NumberTick(new Double(tickVal), tickLabel, anchor, rotationAnchor, angle));
            }
        }
    }
    return ticks;
}
Also used : TextAnchor(org.jfree.ui.TextAnchor) List(java.util.List) NumberFormat(java.text.NumberFormat)

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