Java Code Examples for org.jfree.chart.text.TextUtilities#calculateRotatedStringBounds()

The following examples show how to use org.jfree.chart.text.TextUtilities#calculateRotatedStringBounds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XYTextAnnotation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws the annotation.
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param dataArea  the data area.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param rendererIndex  the renderer index.
 * @param info  an optional info object that will be populated with
 *              entity information.
 */
public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
                 ValueAxis domainAxis, ValueAxis rangeAxis, 
                 int rendererIndex,
                 PlotRenderingInfo info) {

    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
            plot.getDomainAxisLocation(), orientation);
    RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
            plot.getRangeAxisLocation(), orientation);

    float anchorX = (float) domainAxis.valueToJava2D(
            this.x, dataArea, domainEdge);
    float anchorY = (float) rangeAxis.valueToJava2D(
            this.y, dataArea, rangeEdge);

    if (orientation == PlotOrientation.HORIZONTAL) {
        float tempAnchor = anchorX;
        anchorX = anchorY;
        anchorY = tempAnchor;
    }
    
    g2.setFont(getFont());
    g2.setPaint(getPaint());
    TextUtilities.drawRotatedString(getText(), g2, anchorX, anchorY,
            getTextAnchor(), getRotationAngle(), getRotationAnchor());
    Shape hotspot = TextUtilities.calculateRotatedStringBounds(
            getText(), g2, anchorX, anchorY, getTextAnchor(), 
            getRotationAngle(), getRotationAnchor());
    
    String toolTip = getToolTipText();
    String url = getURL();
    if (toolTip != null || url != null) {
        addEntity(info, hotspot, rendererIndex, toolTip, url);
    }

}
 
Example 2
Source File: CrosshairOverlay.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: CrosshairOverlay.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example 4
Source File: XYBarRenderer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws an item label.  This method is provided as an alternative to
 * {@link #drawItemLabel(Graphics2D, PlotOrientation, XYDataset, int, int,
 * double, double, boolean)} so that the bar can be used to calculate the
 * label anchor point.
 *
 * @param g2  the graphics device.
 * @param dataset  the dataset.
 * @param series  the series index.
 * @param item  the item index.
 * @param selected  is the data item selected?
 * @param plot  the plot.
 * @param generator  the label generator (<code>null</code> permitted, in
 *         which case the method does nothing, just returns).
 * @param bar  the bar.
 * @param negative  a flag indicating a negative value.
 *
 * @since 1.2.0
 */
protected void drawItemLabelForBar(Graphics2D g2, XYPlot plot,
        XYDataset dataset, int series, int item, boolean selected,
        XYItemLabelGenerator generator, Rectangle2D bar,
        boolean negative) {

    if (generator == null) {
        return;  // nothing to do
    }
    String label = generator.generateLabel(dataset, series, item);
    if (label == null) {
        return;  // nothing to do
    }

    Font labelFont = getItemLabelFont(series, item, selected);
    g2.setFont(labelFont);
    Paint paint = getItemLabelPaint(series, item, selected);
    g2.setPaint(paint);

    // find out where to place the label...
    ItemLabelPosition position = null;
    if (!negative) {
        position = getPositiveItemLabelPosition(series, item, selected);
    }
    else {
        position = getNegativeItemLabelPosition(series, item, selected);
    }

    // work out the label anchor point...
    Point2D anchorPoint = calculateLabelAnchorPoint(
            position.getItemLabelAnchor(), bar, plot.getOrientation());

    if (isInternalAnchor(position.getItemLabelAnchor())) {
        Shape bounds = TextUtilities.calculateRotatedStringBounds(label,
                g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());

        if (bounds != null) {
            if (!bar.contains(bounds.getBounds2D())) {
                if (!negative) {
                    position = getPositiveItemLabelPositionFallback();
                }
                else {
                    position = getNegativeItemLabelPositionFallback();
                }
                if (position != null) {
                    anchorPoint = calculateLabelAnchorPoint(
                            position.getItemLabelAnchor(), bar,
                            plot.getOrientation());
                }
            }
        }

    }

    if (position != null) {
        TextUtilities.drawRotatedString(label, g2,
                (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());
    }
}
 
Example 5
Source File: BarRenderer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws an item label.  This method is used for bars instead of
 * {@link #drawItemLabel()} so that the bar can be used to calculate the
 * label anchor point.
 *
 * @param g2  the graphics device.
 * @param dataset  the dataset.
 * @param row  the row.
 * @param column  the column.
 * @param selected  is the item selected?
 * @param plot  the plot.
 * @param generator  the label generator.
 * @param bar  the bar.
 * @param negative  a flag indicating a negative value.
 *
 * @since 1.2.0
 */
protected void drawItemLabelForBar(Graphics2D g2, CategoryPlot plot,
        CategoryDataset dataset, int row, int column, boolean selected,
        CategoryItemLabelGenerator generator, Rectangle2D bar,
        boolean negative) {

    String label = generator.generateLabel(dataset, row, column);
    if (label == null) {
        return;  // nothing to do
    }

    Font labelFont = getItemLabelFont(row, column, selected);
    g2.setFont(labelFont);
    Paint paint = getItemLabelPaint(row, column, selected);
    g2.setPaint(paint);

    // find out where to place the label...
    ItemLabelPosition position = null;
    if (!negative) {
        position = getPositiveItemLabelPosition(row, column, selected);
    }
    else {
        position = getNegativeItemLabelPosition(row, column, selected);
    }

    // work out the label anchor point...
    Point2D anchorPoint = calculateLabelAnchorPoint(
            position.getItemLabelAnchor(), bar, plot.getOrientation());

    if (isInternalAnchor(position.getItemLabelAnchor())) {
        Shape bounds = TextUtilities.calculateRotatedStringBounds(label,
                g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());

        if (bounds != null) {
            if (!bar.contains(bounds.getBounds2D())) {
                if (!negative) {
                    position = getPositiveItemLabelPositionFallback();
                }
                else {
                    position = getNegativeItemLabelPositionFallback();
                }
                if (position != null) {
                    anchorPoint = calculateLabelAnchorPoint(
                            position.getItemLabelAnchor(), bar,
                            plot.getOrientation());
                }
            }
        }

    }

    if (position != null) {
        TextUtilities.drawRotatedString(label, g2,
                (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());
    }
}
 
Example 6
Source File: XYPointerAnnotation.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws the annotation.
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param dataArea  the data area.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param rendererIndex  the renderer index.
 * @param info  the plot rendering info.
 */
public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
                 ValueAxis domainAxis, ValueAxis rangeAxis,
                 int rendererIndex,
                 PlotRenderingInfo info) {

    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
            plot.getDomainAxisLocation(), orientation);
    RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
            plot.getRangeAxisLocation(), orientation);
    double j2DX = domainAxis.valueToJava2D(getX(), dataArea, domainEdge);
    double j2DY = rangeAxis.valueToJava2D(getY(), dataArea, rangeEdge);
    if (orientation == PlotOrientation.HORIZONTAL) {
        double temp = j2DX;
        j2DX = j2DY;
        j2DY = temp;
    }
    double startX = j2DX + Math.cos(this.angle) * this.baseRadius;
    double startY = j2DY + Math.sin(this.angle) * this.baseRadius;

    double endX = j2DX + Math.cos(this.angle) * this.tipRadius;
    double endY = j2DY + Math.sin(this.angle) * this.tipRadius;

    double arrowBaseX = endX + Math.cos(this.angle) * this.arrowLength;
    double arrowBaseY = endY + Math.sin(this.angle) * this.arrowLength;

    double arrowLeftX = arrowBaseX
            + Math.cos(this.angle + Math.PI / 2.0) * this.arrowWidth;
    double arrowLeftY = arrowBaseY
            + Math.sin(this.angle + Math.PI / 2.0) * this.arrowWidth;

    double arrowRightX = arrowBaseX
            - Math.cos(this.angle + Math.PI / 2.0) * this.arrowWidth;
    double arrowRightY = arrowBaseY
            - Math.sin(this.angle + Math.PI / 2.0) * this.arrowWidth;

    GeneralPath arrow = new GeneralPath();
    arrow.moveTo((float) endX, (float) endY);
    arrow.lineTo((float) arrowLeftX, (float) arrowLeftY);
    arrow.lineTo((float) arrowRightX, (float) arrowRightY);
    arrow.closePath();

    g2.setStroke(this.arrowStroke);
    g2.setPaint(this.arrowPaint);
    Line2D line = new Line2D.Double(startX, startY, arrowBaseX, arrowBaseY);
    g2.draw(line);
    g2.fill(arrow);

    // draw the label
    double labelX = j2DX + Math.cos(this.angle) * (this.baseRadius
            + this.labelOffset);
    double labelY = j2DY + Math.sin(this.angle) * (this.baseRadius
            + this.labelOffset);
    g2.setFont(getFont());
    Shape hotspot = TextUtilities.calculateRotatedStringBounds(
            getText(), g2, (float) labelX, (float) labelY, getTextAnchor(),
            getRotationAngle(), getRotationAnchor());
    if (getBackgroundPaint() != null) {
        g2.setPaint(getBackgroundPaint());
        g2.fill(hotspot);
    }
    g2.setPaint(getPaint());
    TextUtilities.drawRotatedString(getText(), g2, (float) labelX,
            (float) labelY, getTextAnchor(), getRotationAngle(),
            getRotationAnchor());
    if (isOutlineVisible()) {
        g2.setStroke(getOutlineStroke());
        g2.setPaint(getOutlinePaint());
        g2.draw(hotspot);
    }

    String toolTip = getToolTipText();
    String url = getURL();
    if (toolTip != null || url != null) {
        addEntity(info, hotspot, rendererIndex, toolTip, url);
    }

}
 
Example 7
Source File: XYTextAnnotation.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws the annotation.
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param dataArea  the data area.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param rendererIndex  the renderer index.
 * @param info  an optional info object that will be populated with
 *              entity information.
 */
public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
                 ValueAxis domainAxis, ValueAxis rangeAxis,
                 int rendererIndex,
                 PlotRenderingInfo info) {

    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
            plot.getDomainAxisLocation(), orientation);
    RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
            plot.getRangeAxisLocation(), orientation);

    float anchorX = (float) domainAxis.valueToJava2D(
            this.x, dataArea, domainEdge);
    float anchorY = (float) rangeAxis.valueToJava2D(
            this.y, dataArea, rangeEdge);

    if (orientation == PlotOrientation.HORIZONTAL) {
        float tempAnchor = anchorX;
        anchorX = anchorY;
        anchorY = tempAnchor;
    }

    g2.setFont(getFont());
    Shape hotspot = TextUtilities.calculateRotatedStringBounds(
            getText(), g2, anchorX, anchorY, getTextAnchor(),
            getRotationAngle(), getRotationAnchor());
    if (this.backgroundPaint != null) {
        g2.setPaint(this.backgroundPaint);
        g2.fill(hotspot);
    }
    g2.setPaint(getPaint());
    TextUtilities.drawRotatedString(getText(), g2, anchorX, anchorY,
            getTextAnchor(), getRotationAngle(), getRotationAnchor());
    if (this.outlineVisible) {
        g2.setStroke(this.outlineStroke);
        g2.setPaint(this.outlinePaint);
        g2.draw(hotspot);
    }

    String toolTip = getToolTipText();
    String url = getURL();
    if (toolTip != null || url != null) {
        addEntity(info, hotspot, rendererIndex, toolTip, url);
    }

}
 
Example 8
Source File: XYBarRenderer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws an item label.  This method is provided as an alternative to
 * {@link #drawItemLabel(Graphics2D, PlotOrientation, XYDataset, int, int, 
 * double, double, boolean)} so that the bar can be used to calculate the 
 * label anchor point. 
 * 
 * @param g2  the graphics device.
 * @param dataset  the dataset.
 * @param series  the series index.
 * @param item  the item index.
 * @param plot  the plot.
 * @param generator  the label generator (<code>null</code> permitted, in 
 *         which case the method does nothing, just returns).
 * @param bar  the bar.
 * @param negative  a flag indicating a negative value.
 */
protected void drawItemLabel(Graphics2D g2, XYDataset dataset,
        int series, int item, XYPlot plot, XYItemLabelGenerator generator, 
        Rectangle2D bar, boolean negative) {
                                 
    if (generator == null) {
        return;  // nothing to do
    }
    String label = generator.generateLabel(dataset, series, item);
    if (label == null) {
        return;  // nothing to do   
    }
    
    Font labelFont = getItemLabelFont(series, item);
    g2.setFont(labelFont);
    Paint paint = getItemLabelPaint(series, item);
    g2.setPaint(paint);

    // find out where to place the label...
    ItemLabelPosition position = null;
    if (!negative) {
        position = getPositiveItemLabelPosition(series, item);
    }
    else {
        position = getNegativeItemLabelPosition(series, item);
    }

    // work out the label anchor point...
    Point2D anchorPoint = calculateLabelAnchorPoint(
            position.getItemLabelAnchor(), bar, plot.getOrientation());
    
    if (isInternalAnchor(position.getItemLabelAnchor())) {
        Shape bounds = TextUtilities.calculateRotatedStringBounds(label, 
                g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());
        
        if (bounds != null) {
            if (!bar.contains(bounds.getBounds2D())) {
                if (!negative) {
                    position = getPositiveItemLabelPositionFallback();
                }
                else {
                    position = getNegativeItemLabelPositionFallback();
                }
                if (position != null) {
                    anchorPoint = calculateLabelAnchorPoint(
                            position.getItemLabelAnchor(), bar, 
                            plot.getOrientation());
                }
            }
        }
    
    }
    
    if (position != null) {
        TextUtilities.drawRotatedString(label, g2, 
                (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(), 
                position.getRotationAnchor());
    }        
}
 
Example 9
Source File: BarRenderer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws an item label.  This method is overridden so that the bar can be 
 * used to calculate the label anchor point.
 * 
 * @param g2  the graphics device.
 * @param data  the dataset.
 * @param row  the row.
 * @param column  the column.
 * @param plot  the plot.
 * @param generator  the label generator.
 * @param bar  the bar.
 * @param negative  a flag indicating a negative value.
 */
protected void drawItemLabel(Graphics2D g2,
                             CategoryDataset data,
                             int row,
                             int column,
                             CategoryPlot plot,
                             CategoryItemLabelGenerator generator,
                             Rectangle2D bar,
                             boolean negative) {
                                 
    String label = generator.generateLabel(data, row, column);
    if (label == null) {
        return;  // nothing to do   
    }
    
    Font labelFont = getItemLabelFont(row, column);
    g2.setFont(labelFont);
    Paint paint = getItemLabelPaint(row, column);
    g2.setPaint(paint);

    // find out where to place the label...
    ItemLabelPosition position = null;
    if (!negative) {
        position = getPositiveItemLabelPosition(row, column);
    }
    else {
        position = getNegativeItemLabelPosition(row, column);
    }

    // work out the label anchor point...
    Point2D anchorPoint = calculateLabelAnchorPoint(
            position.getItemLabelAnchor(), bar, plot.getOrientation());
    
    if (isInternalAnchor(position.getItemLabelAnchor())) {
        Shape bounds = TextUtilities.calculateRotatedStringBounds(label, 
                g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());
        
        if (bounds != null) {
            if (!bar.contains(bounds.getBounds2D())) {
                if (!negative) {
                    position = getPositiveItemLabelPositionFallback();
                }
                else {
                    position = getNegativeItemLabelPositionFallback();
                }
                if (position != null) {
                    anchorPoint = calculateLabelAnchorPoint(
                            position.getItemLabelAnchor(), bar, 
                            plot.getOrientation());
                }
            }
        }
    
    }
    
    if (position != null) {
        TextUtilities.drawRotatedString(label, g2, 
                (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(), 
                position.getRotationAnchor());
    }        
}