Example usage for org.jfree.chart.plot PlotRenderingInfo setPlotArea

List of usage examples for org.jfree.chart.plot PlotRenderingInfo setPlotArea

Introduction

In this page you can find the example usage for org.jfree.chart.plot PlotRenderingInfo setPlotArea.

Prototype

public void setPlotArea(Rectangle2D area) 

Source Link

Document

Sets the plot area.

Usage

From source file:de.laures.cewolf.jfree.ThermometerPlot.java

/**
 * Draws the plot on a Java 2D graphics device (such as the screen or a printer).
 *
 * @param g2  the graphics device.//from   ww w .  j  a  va2  s.  c o m
 * @param area  the area within which the plot should be drawn.
 * @param anchor  the anchor point (<code>null</code> permitted).
 * @param parentState  the state from the parent plot, if there is one.
 * @param info  collects info about the drawing.
 */
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState,
        PlotRenderingInfo info) {

    RoundRectangle2D outerStem = new RoundRectangle2D.Double();
    RoundRectangle2D innerStem = new RoundRectangle2D.Double();
    RoundRectangle2D mercuryStem = new RoundRectangle2D.Double();
    Ellipse2D outerBulb = new Ellipse2D.Double();
    Ellipse2D innerBulb = new Ellipse2D.Double();
    String temp = null;
    FontMetrics metrics = null;
    if (info != null) {
        info.setPlotArea(area);
    }

    // adjust for insets...
    RectangleInsets insets = getInsets();
    insets.trim(area);
    drawBackground(g2, area);

    // adjust for padding...
    Rectangle2D interior = (Rectangle2D) area.clone();
    this.padding.trim(interior);
    int midX = (int) (interior.getX() + (interior.getWidth() / 2));
    int midY = (int) (interior.getY() + (interior.getHeight() / 2));
    int stemTop = (int) (interior.getMinY() + getBulbRadius());
    int stemBottom = (int) (interior.getMaxY() - getBulbDiameter());
    Rectangle2D dataArea = new Rectangle2D.Double(midX - getColumnRadius(), stemTop, getColumnRadius(),
            stemBottom - stemTop);

    outerBulb.setFrame(midX - getBulbRadius(), stemBottom, getBulbDiameter(), getBulbDiameter());

    outerStem.setRoundRect(midX - getColumnRadius(), interior.getMinY(), getColumnDiameter(),
            stemBottom + getBulbDiameter() - stemTop, getColumnDiameter(), getColumnDiameter());

    Area outerThermometer = new Area(outerBulb);
    Area tempArea = new Area(outerStem);
    outerThermometer.add(tempArea);

    innerBulb.setFrame(midX - getBulbRadius() + getGap(), stemBottom + getGap(),
            getBulbDiameter() - getGap() * 2, getBulbDiameter() - getGap() * 2);

    innerStem.setRoundRect(midX - getColumnRadius() + getGap(), interior.getMinY() + getGap(),
            getColumnDiameter() - getGap() * 2, stemBottom + getBulbDiameter() - getGap() * 2 - stemTop,
            getColumnDiameter() - getGap() * 2, getColumnDiameter() - getGap() * 2);

    Area innerThermometer = new Area(innerBulb);
    tempArea = new Area(innerStem);
    innerThermometer.add(tempArea);

    if ((this.dataset != null) && (this.dataset.getValue() != null)) {
        double current = this.dataset.getValue().doubleValue();
        double ds = this.rangeAxis.valueToJava2D(current, dataArea, RectangleEdge.LEFT);

        int i = getColumnDiameter() - getGap() * 2; // already calculated
        int j = getColumnRadius() - getGap(); // already calculated
        int l = (i / 2);
        int k = (int) Math.round(ds);
        if (k < (getGap() + interior.getMinY())) {
            k = (int) (getGap() + interior.getMinY());
            l = getBulbRadius();
        }

        Area mercury = new Area(innerBulb);

        if (k < (stemBottom + getBulbRadius())) {
            mercuryStem.setRoundRect(midX - j, k, i, (stemBottom + getBulbRadius()) - k, l, l);
            tempArea = new Area(mercuryStem);
            mercury.add(tempArea);
        }

        g2.setPaint(getCurrentPaint());
        g2.fill(mercury);

        // draw range indicators...
        if (this.subrangeIndicatorsVisible) {
            g2.setStroke(this.subrangeIndicatorStroke);
            Range range = this.rangeAxis.getRange();

            // draw start of normal range
            double value = this.subrangeInfo[NORMAL][RANGE_LOW];
            if (range.contains(value)) {
                double x = midX + getColumnRadius() + 2;
                double y = this.rangeAxis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
                Line2D line = new Line2D.Double(x, y, x + 10, y);
                g2.setPaint(this.subrangePaint[NORMAL]);
                g2.draw(line);
            }

            // draw start of warning range
            value = this.subrangeInfo[WARNING][RANGE_LOW];
            if (range.contains(value)) {
                double x = midX + getColumnRadius() + 2;
                double y = this.rangeAxis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
                Line2D line = new Line2D.Double(x, y, x + 10, y);
                g2.setPaint(this.subrangePaint[WARNING]);
                g2.draw(line);
            }

            // draw start of critical range
            value = this.subrangeInfo[CRITICAL][RANGE_LOW];
            if (range.contains(value)) {
                double x = midX + getColumnRadius() + 2;
                double y = this.rangeAxis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
                Line2D line = new Line2D.Double(x, y, x + 10, y);
                g2.setPaint(this.subrangePaint[CRITICAL]);
                g2.draw(line);
            }
        }

        // draw the axis...
        if ((this.rangeAxis != null) && (this.axisLocation != NONE)) {
            int drawWidth = AXIS_GAP;
            Rectangle2D drawArea;
            double cursor = 0;

            switch (this.axisLocation) {
            case RIGHT:
                cursor = midX + getColumnRadius();
                drawArea = new Rectangle2D.Double(cursor, stemTop, drawWidth, (stemBottom - stemTop + 1));
                this.rangeAxis.draw(g2, cursor, area, drawArea, RectangleEdge.RIGHT, null);
                break;

            case LEFT:
            default:
                //cursor = midX - COLUMN_RADIUS - AXIS_GAP;
                cursor = midX - getColumnRadius();
                drawArea = new Rectangle2D.Double(cursor, stemTop, drawWidth, (stemBottom - stemTop + 1));
                this.rangeAxis.draw(g2, cursor, area, drawArea, RectangleEdge.LEFT, null);
                break;
            }

        }

        // draw text value on screen
        g2.setFont(this.valueFont);
        g2.setPaint(this.valuePaint);
        metrics = g2.getFontMetrics();
        switch (this.valueLocation) {
        case RIGHT:
            g2.drawString(this.valueFormat.format(current), midX + getColumnRadius() + getGap(), midY);
            break;
        case LEFT:
            String valueString = this.valueFormat.format(current);
            int stringWidth = metrics.stringWidth(valueString);
            g2.drawString(valueString, midX - getColumnRadius() - getGap() - stringWidth, midY);
            break;
        case BULB:
            temp = this.valueFormat.format(current);
            i = metrics.stringWidth(temp) / 2;
            g2.drawString(temp, midX - i, stemBottom + getBulbRadius() + getGap());
            break;
        default:
        }
        /***/
    }

    g2.setPaint(this.thermometerPaint);
    g2.setFont(this.valueFont);

    //  draw units indicator
    metrics = g2.getFontMetrics();
    int tickX1 = midX - getColumnRadius() - getGap() * 2 - metrics.stringWidth(UNITS[this.units]);
    if (tickX1 > area.getMinX()) {
        g2.drawString(UNITS[this.units], tickX1, (int) (area.getMinY() + 20));
    }

    // draw thermometer outline
    g2.setStroke(this.thermometerStroke);
    g2.draw(outerThermometer);
    g2.draw(innerThermometer);

    drawOutline(g2, area);
}

From source file:ucar.unidata.idv.control.chart.MyXYPlot.java

/**
 * Draws the plot within the specified area on a graphics device.
 *
 * @param g2  the graphics device./* w w  w.j av a 2  s. c o  m*/
 * @param area  the plot area (in Java2D space).
 * @param anchor  an anchor point in Java2D space (<code>null</code>
 *                permitted).
 * @param parentState  the state from the parent plot, if there is one
 *                     (<code>null</code> permitted).
 * @param info  collects chart drawing information (<code>null</code>
 *              permitted).
 */
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState,
        PlotRenderingInfo info) {

    // if the plot area is too small, just return...
    boolean b1 = (area.getWidth() <= MINIMUM_WIDTH_TO_DRAW);
    boolean b2 = (area.getHeight() <= MINIMUM_HEIGHT_TO_DRAW);
    if (b1 || b2) {
        return;
    }

    // record the plot area...
    if (info != null) {
        info.setPlotArea(area);
    }

    // adjust the drawing area for the plot insets (if any)...
    RectangleInsets insets = getInsets();
    insets.trim(area);

    AxisSpace space = calculateAxisSpace(g2, area);
    Rectangle2D dataArea = space.shrink(area, null);
    this.axisOffset.trim(dataArea);

    if (info != null) {
        info.setDataArea(dataArea);
    }

    // draw the plot background and axes...
    drawBackground(g2, dataArea);
    Map axisStateMap = drawAxes(g2, area, dataArea, info);

    if ((anchor != null) && !dataArea.contains(anchor)) {
        anchor = null;
    }
    CrosshairState crosshairState = new CrosshairState();
    crosshairState.setCrosshairDistance(Double.POSITIVE_INFINITY);
    crosshairState.setAnchor(anchor);
    crosshairState.setCrosshairX(getDomainCrosshairValue());
    crosshairState.setCrosshairY(getRangeCrosshairValue());
    Shape originalClip = g2.getClip();
    Composite originalComposite = g2.getComposite();

    g2.clip(dataArea);
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));

    AxisState domainAxisState = (AxisState) axisStateMap.get(getDomainAxis());
    if (domainAxisState == null) {
        if (parentState != null) {
            domainAxisState = (AxisState) parentState.getSharedAxisStates().get(getDomainAxis());
        }
    }
    if (domainAxisState != null) {
        drawDomainTickBands(g2, dataArea, domainAxisState.getTicks());
        drawDomainGridlines(g2, dataArea, domainAxisState.getTicks());
    }

    AxisState rangeAxisState = (AxisState) axisStateMap.get(getRangeAxis());
    if (rangeAxisState == null) {
        if (parentState != null) {
            rangeAxisState = (AxisState) parentState.getSharedAxisStates().get(getRangeAxis());
        }
    }
    if (rangeAxisState != null) {
        drawRangeTickBands(g2, dataArea, rangeAxisState.getTicks());
        drawRangeGridlines(g2, dataArea, rangeAxisState.getTicks());
        drawZeroRangeBaseline(g2, dataArea);
    }

    // draw the markers that are associated with a specific renderer...
    for (int i = 0; i < this.renderers.size(); i++) {
        drawDomainMarkers(g2, dataArea, i, Layer.BACKGROUND);
    }
    for (int i = 0; i < this.renderers.size(); i++) {
        drawRangeMarkers(g2, dataArea, i, Layer.BACKGROUND);
    }

    // now draw annotations and render data items...
    boolean foundData = false;
    DatasetRenderingOrder order = getDatasetRenderingOrder();
    if (order == DatasetRenderingOrder.FORWARD) {

        // draw background annotations
        int rendererCount = this.renderers.size();
        for (int i = 0; i < rendererCount; i++) {
            XYItemRenderer r = getRenderer(i);
            if (r != null) {
                ValueAxis domainAxis = getDomainAxisForDataset(i);
                ValueAxis rangeAxis = getRangeAxisForDataset(i);
                r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.BACKGROUND, info);
            }
        }

        // render data items...
        for (int i = 0; i < getDatasetCount(); i++) {
            foundData = render(g2, dataArea, i, info, crosshairState) || foundData;
        }

        // draw foreground annotations
        for (int i = 0; i < rendererCount; i++) {
            XYItemRenderer r = getRenderer(i);
            if (r != null) {
                ValueAxis domainAxis = getDomainAxisForDataset(i);
                ValueAxis rangeAxis = getRangeAxisForDataset(i);
                r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.FOREGROUND, info);
            }
        }

    } else if (order == DatasetRenderingOrder.REVERSE) {

        // draw background annotations
        int rendererCount = this.renderers.size();
        for (int i = rendererCount - 1; i >= 0; i--) {
            XYItemRenderer r = getRenderer(i);
            if (r != null) {
                ValueAxis domainAxis = getDomainAxisForDataset(i);
                ValueAxis rangeAxis = getRangeAxisForDataset(i);
                r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.BACKGROUND, info);
            }
        }

        ucar.unidata.util.Trace.call1("renderData");
        for (int i = getDatasetCount() - 1; i >= 0; i--) {
            foundData = render(g2, dataArea, i, info, crosshairState) || foundData;
        }
        ucar.unidata.util.Trace.call2("renderData");

        // draw foreground annotations
        for (int i = rendererCount - 1; i >= 0; i--) {
            XYItemRenderer r = getRenderer(i);
            if (r != null) {
                ValueAxis domainAxis = getDomainAxisForDataset(i);
                ValueAxis rangeAxis = getRangeAxisForDataset(i);
                r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.FOREGROUND, info);
            }
        }

    }

    PlotOrientation orient = getOrientation();

    // draw domain crosshair if required...
    if (!this.domainCrosshairLockedOnData && (anchor != null)) {
        double xx = getDomainAxis().java2DToValue(anchor.getX(), dataArea, getDomainAxisEdge());
        crosshairState.setCrosshairX(xx);
    }
    setDomainCrosshairValue(crosshairState.getCrosshairX(), false);
    if (isDomainCrosshairVisible()) {
        double x = getDomainCrosshairValue();
        Paint paint = getDomainCrosshairPaint();
        Stroke stroke = getDomainCrosshairStroke();
        if (orient == PlotOrientation.HORIZONTAL) {
            drawHorizontalLine(g2, dataArea, x, stroke, paint);
        } else if (orient == PlotOrientation.VERTICAL) {
            drawVerticalLine(g2, dataArea, x, stroke, paint);
        }
    }

    // draw range crosshair if required...
    if (!this.rangeCrosshairLockedOnData && (anchor != null)) {
        double yy = getRangeAxis().java2DToValue(anchor.getY(), dataArea, getRangeAxisEdge());
        crosshairState.setCrosshairX(yy);
    }
    setRangeCrosshairValue(crosshairState.getCrosshairY(), false);
    if (isRangeCrosshairVisible() && getRangeAxis().getRange().contains(getRangeCrosshairValue())) {
        double y = getRangeCrosshairValue();
        Paint paint = getRangeCrosshairPaint();
        Stroke stroke = getRangeCrosshairStroke();
        if (orient == PlotOrientation.HORIZONTAL) {
            drawVerticalLine(g2, dataArea, y, stroke, paint);
        } else if (orient == PlotOrientation.VERTICAL) {
            drawHorizontalLine(g2, dataArea, y, stroke, paint);
        }
    }

    if (!foundData) {
        drawNoDataMessage(g2, dataArea);
    }

    for (int i = 0; i < this.renderers.size(); i++) {
        drawDomainMarkers(g2, dataArea, i, Layer.FOREGROUND);
    }
    for (int i = 0; i < this.renderers.size(); i++) {
        drawRangeMarkers(g2, dataArea, i, Layer.FOREGROUND);
    }

    drawAnnotations(g2, dataArea, info);
    g2.setClip(originalClip);
    g2.setComposite(originalComposite);

    drawOutline(g2, dataArea);

}

From source file:org.jfree.chart.plot.StackedXYPlot.java

/**
 * Draws the plot.//from  w w w .  j a  v  a 2  s  .  c om
 * @param graphics2d the graphics device.
 * @param plotArea the plot plotArea (in Java2D space).
 * @param anchor an anchor point in Java2D space (<code>null</code>
        permitted).
 * @param parentState the state from the parent plot
             (<code>null</code> permitted).
 * @param plotRenderingInfo chart drawing information (<code>null</code>
      permitted).
 */
@Override
public void draw(Graphics2D graphics2d, Rectangle2D plotArea, Point2D anchor, PlotState parentState,
        PlotRenderingInfo plotRenderingInfo) {

    if (plotRenderingInfo != null) {
        plotRenderingInfo.setPlotArea(plotArea);
    }

    RectangleInsets insets = getInsets();
    insets.trim(plotArea);

    setFixedRangeAxisSpaceForSubplots(null);
    //calculateAxisSpace will also calculate sub-plot plotArea
    AxisSpace space = calculateAxisSpace(graphics2d, plotArea);
    Rectangle2D dataArea = space.shrink(plotArea, null);
    Rectangle2D[] calculatedSubPlotAreas = null;
    //get subplotsAreas from parent class
    try {
        calculatedSubPlotAreas = (Rectangle2D[]) FieldUtils.readField(this, "subplotAreas", true);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(StackedXYPlot.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
    }

    setFixedRangeAxisSpaceForSubplots(space);

    // draw all the subplots         
    for (int i = 0; i < getSubplots().size(); i++) {
        XYPlot plot = (XYPlot) getSubplots().get(i);
        PlotRenderingInfo subplotInfo = null;
        if (plotRenderingInfo != null) {
            subplotInfo = new PlotRenderingInfo(plotRenderingInfo.getOwner());
            plotRenderingInfo.addSubplotInfo(subplotInfo);
        }
        plot.draw(graphics2d, calculatedSubPlotAreas[i], anchor, parentState, subplotInfo);
    }

    if (plotRenderingInfo != null) {
        plotRenderingInfo.setDataArea(dataArea);
    }
}