Example usage for org.jfree.chart.block RectangleConstraint RectangleConstraint

List of usage examples for org.jfree.chart.block RectangleConstraint RectangleConstraint

Introduction

In this page you can find the example usage for org.jfree.chart.block RectangleConstraint RectangleConstraint.

Prototype

public RectangleConstraint(double w, Range h) 

Source Link

Document

Creates a new constraint with a fixed width and a range for the height.

Usage

From source file:org.jfree.chart.demo.LegendTitleToImageDemo2.java

public static void main(String args[]) throws IOException {
    DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    defaultpiedataset.setValue("England", 1.0D);
    defaultpiedataset.setValue("France", 2D);
    defaultpiedataset.setValue("Germany", 3D);
    defaultpiedataset.setValue("Italy", 4D);
    defaultpiedataset.setValue("Scotland", 5D);
    defaultpiedataset.setValue("Belgium", 6D);
    defaultpiedataset.setValue("Poland", 7D);
    defaultpiedataset.setValue("Spain", 8D);
    defaultpiedataset.setValue("Portugal", 9D);
    defaultpiedataset.setValue("Switzerland", 10D);
    defaultpiedataset.setValue("Austria", 11D);
    defaultpiedataset.setValue("Luxembourg", 12D);
    JFreeChart jfreechart = ChartFactory.createPieChart("Test", defaultpiedataset, true, false, false);
    LegendTitle legendtitle = jfreechart.getLegend();
    legendtitle.setMargin(0.0D, 0.0D, 1.0D, 1.0D);
    BufferedImage bufferedimage = new BufferedImage(1, 1, 2);
    Graphics2D graphics2d = bufferedimage.createGraphics();
    Size2D size2d = legendtitle.arrange(graphics2d, new RectangleConstraint(250D, new Range(0.0D, 10000D)));
    graphics2d.dispose();/*from  w w  w  . j a  va 2 s .  c  om*/
    int i = (int) Math.rint(size2d.width);
    int j = (int) Math.rint(size2d.height);
    BufferedImage bufferedimage1 = new BufferedImage(i, j, 2);
    Graphics2D graphics2d1 = bufferedimage1.createGraphics();
    legendtitle.draw(graphics2d1, new java.awt.geom.Rectangle2D.Double(0.0D, 0.0D, i, j));
    graphics2d1.dispose();
    BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
            new FileOutputStream(new File("LegendTitleToImageDemo2.png")));
    ChartUtilities.writeBufferedImageAsPNG(bufferedoutputstream, bufferedimage1);
    bufferedoutputstream.close();
}

From source file:edu.cudenver.bios.chartsvc.representation.LegendImageRepresentation.java

/**
 * Called internally by Restlet library to write the image as the HTTP
 * response.//from  w w  w  .j a v  a  2 s .com
 * @param out output stream
 */
@Override
public void write(OutputStream out) throws IOException {
    // build the legend from the plot, and write it to a jpeg image
    if (plot != null) {
        LegendTitle legend = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
        legend.setFrame(BlockBorder.NONE);
        //legend.setMargin(new RectangleInsets(2.0, 2.0, 2.0, 2.0));
        legend.setBackgroundPaint(Color.white);
        legend.setPosition(RectangleEdge.BOTTOM);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        Rectangle2D.Double legendArea = new Rectangle2D.Double(0, 0, width, height);
        g.clip(legendArea);
        legend.arrange(g, new RectangleConstraint(width, height));
        legend.draw(g, legendArea);
        g.dispose();
        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);
    }
}

From source file:de.laures.cewolf.util.Renderer.java

/**
 * Renders a legend/*w  w  w.  j av  a  2s  .  c  o m*/
 * @param cd the chart iamge to be rendred
 * @return the rendered image
 * @throws CewolfException
 */
private static RenderedImage renderLegend(ChartImage cd, Object c) throws CewolfException {
    try {
        JFreeChart chart = (JFreeChart) c;
        final int width = cd.getWidth();
        final int height = cd.getHeight();
        LegendTitle legend = getLegend(chart);
        boolean haslegend = true;

        // with JFreeChart v0.9.20, the only way to get a valid legend,
        // is either to retrieve it from the chart or to assign a new
        // one to the chart. In the case where the chart has no legend,
        // a new one must be assigned, but just for rendering. After, we
        // have to reset the legend to null in the chart.
        if (null == legend) {
            haslegend = false;
            legend = new LegendTitle(chart.getPlot());
        }
        legend.setPosition(RectangleEdge.BOTTOM);
        BufferedImage bimage = ImageHelper.createImage(width, height);
        Graphics2D g = bimage.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, width, height);
        legend.arrange(g, new RectangleConstraint(width, height));
        legend.draw(g, new Rectangle(width, height));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
        param.setQuality(1.0f, true);
        encoder.encode(bimage, param);
        out.close();

        // if the chart had no legend, reset it to null in order to give back the
        // chart in the state we received it.
        if (!haslegend) {
            removeLegend(chart);
        }

        return new RenderedImage(out.toByteArray(), "image/jpeg",
                new ChartRenderingInfo(new StandardEntityCollection()));
    } catch (IOException ioex) {
        log.error(ioex);
        throw new ChartRenderingException(ioex.getMessage(), ioex);
    }
}

From source file:org.jfree.experimental.chart.annotations.XYTitleAnnotation.java

/**
 * Draws the annotation.  This method is called by the drawing code in the 
 * {@link XYPlot} class, you don't normally need to call this method 
 * directly.// ww  w . j a  va  2s . c o  m
 *
 * @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  if supplied, this info object 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();
    AxisLocation domainAxisLocation = plot.getDomainAxisLocation();
    AxisLocation rangeAxisLocation = plot.getRangeAxisLocation();
    RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(domainAxisLocation, orientation);
    RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(rangeAxisLocation, orientation);
    Range xRange = domainAxis.getRange();
    Range yRange = rangeAxis.getRange();
    double anchorX = 0.0;
    double anchorY = 0.0;
    if (this.coordinateType == XYCoordinateType.RELATIVE) {
        anchorX = xRange.getLowerBound() + (this.x * xRange.getLength());
        anchorY = yRange.getLowerBound() + (this.y * yRange.getLength());
    } else {
        anchorX = domainAxis.valueToJava2D(this.x, dataArea, domainEdge);
        anchorY = rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge);
    }

    float j2DX = (float) domainAxis.valueToJava2D(anchorX, dataArea, domainEdge);
    float j2DY = (float) rangeAxis.valueToJava2D(anchorY, dataArea, rangeEdge);
    float xx = 0.0f;
    float yy = 0.0f;
    if (orientation == PlotOrientation.HORIZONTAL) {
        xx = j2DY;
        yy = j2DX;
    } else if (orientation == PlotOrientation.VERTICAL) {
        xx = j2DX;
        yy = j2DY;
    }

    double maxW = dataArea.getWidth();
    double maxH = dataArea.getHeight();
    if (this.coordinateType == XYCoordinateType.RELATIVE) {
        if (this.maxWidth > 0.0) {
            maxW = maxW * this.maxWidth;
        }
        if (this.maxHeight > 0.0) {
            maxH = maxH * this.maxHeight;
        }
    }
    if (this.coordinateType == XYCoordinateType.DATA) {
        maxW = this.maxWidth;
        maxH = this.maxHeight;
    }
    RectangleConstraint rc = new RectangleConstraint(new Range(0, maxW), new Range(0, maxH));

    Size2D size = this.title.arrange(g2, rc);
    Rectangle2D titleRect = new Rectangle2D.Double(0, 0, size.width, size.height);
    Point2D anchorPoint = RectangleAnchor.coordinates(titleRect, this.anchor);
    xx = xx - (float) anchorPoint.getX();
    yy = yy - (float) anchorPoint.getY();
    titleRect.setRect(xx, yy, titleRect.getWidth(), titleRect.getHeight());
    BlockParams p = new BlockParams();
    if (info != null) {
        if (info.getOwner().getEntityCollection() != null) {
            p.setGenerateEntities(true);
        }
    }
    Object result = this.title.draw(g2, titleRect, p);
    if (result instanceof EntityBlockResult) {
        EntityBlockResult ebr = (EntityBlockResult) result;
        info.getOwner().getEntityCollection().addAll(ebr.getEntityCollection());
    }
    String toolTip = getToolTipText();
    String url = getURL();
    if (toolTip != null || url != null) {
        addEntity(info, new Rectangle2D.Float(xx, yy, (float) size.width, (float) size.height), rendererIndex,
                toolTip, url);
    }
}

From source file:org.gumtree.vis.plot1d.Plot1DPanel.java

private void drawInternalLegend(Graphics2D g2) {
    //      XYDataset dataset = getXYPlot().getDataset();
    //      int numSeries = dataset.getSeriesCount();
    Rectangle2D screenArea = getScreenDataArea();
    Rectangle2D lengendArea = new Rectangle2D.Double(screenArea.getMaxX() - internalLegendSetup.getMinX(),
            screenArea.getMinY() + internalLegendSetup.getMinY(), internalLegendSetup.getWidth(),
            internalLegendSetup.getHeight());
    LegendTitle legend = new LegendTitle(getXYPlot());
    RectangleConstraint rc = new RectangleConstraint(new Range(0, internalLegendSetup.getWidth()),
            new Range(0, internalLegendSetup.getHeight()));
    Size2D size = legend.arrange(g2, rc);
    getXYPlot().getLegendItems();// ww w  .j  a va  2  s  . c om
    legend.draw(g2, lengendArea);
    Rectangle2D titleRect = new Rectangle2D.Double(lengendArea.getMinX(), lengendArea.getMinY(), size.width,
            size.height);
    internalLegendSetup.setRect(internalLegendSetup.getX(), internalLegendSetup.getY(), titleRect.getWidth(),
            titleRect.getHeight());
    if (isInternalLegendSelected) {
        ChartMaskingUtilities.drawMaskBoarder(g2, titleRect);
    } else {
        g2.setColor(Color.GRAY);
        g2.draw(titleRect);
    }
}