Example usage for org.jfree.chart ChartUtilities writeBufferedImageAsPNG

List of usage examples for org.jfree.chart ChartUtilities writeBufferedImageAsPNG

Introduction

In this page you can find the example usage for org.jfree.chart ChartUtilities writeBufferedImageAsPNG.

Prototype

public static void writeBufferedImageAsPNG(OutputStream out, BufferedImage image) throws IOException 

Source Link

Document

Writes a BufferedImage to an output stream in PNG format.

Usage

From source file:gov.nih.nci.caintegrator.plots.kaplanmeier.JFreeChartIKMPlottermpl.java

public void writeBufferedImageAsPNG(OutputStream out, BufferedImage image) throws IOException {
    ChartUtilities.writeBufferedImageAsPNG(out, image);
}

From source file:domainhealth.frontend.graphics.JFreeChartGraphImpl.java

/**
 * Write a PNG image representation of the Graph to the given output 
 * stream//from w w  w  .ja  v  a 2 s.  co  m
 * 
 * @param out The output stream to write the PNG bytes to
 * @throws IOException Indicates a problem writing to the output stream
 */
public void writeGraphImage(int numServersDisplayed, OutputStream out) throws IOException {
    ValueAxis xAxis = new DateAxis(MonitorProperties.units(DATE_TIME));
    NumberAxis yAxis = new NumberAxis(yAxisUnits);
    yAxis.setAutoRangeIncludesZero(true);
    yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    XYPlot xyPlotLine = new XYPlot(xySeriesCollection, xAxis, yAxis,
            new StandardXYItemRenderer(StandardXYItemRenderer.LINES));
    JFreeChart chart = new JFreeChart(chartTitle, JFreeChart.DEFAULT_TITLE_FONT, xyPlotLine, true);
    chart.setBackgroundPaint(java.awt.Color.white);
    // Increase size of graph height to accommodate large legends for when many servers in the domain
    int graphAdditionalHeight = GRAPH_INCREMENT_HEIGHT
            * ((int) (numServersDisplayed / GRAPH_INCREMENT_SERVER_RATIO));
    BufferedImage graphImage = chart.createBufferedImage(GRAPH_WIDTH,
            INITIAL_GRAPH_HEIGHT + graphAdditionalHeight,
            new ChartRenderingInfo(new StandardEntityCollection()));
    addNoDataLogoIfEmpty(graphImage);
    ChartUtilities.writeBufferedImageAsPNG(out, graphImage); // Could try extra two PNG related params: encodeAlpha and compression
}

From source file:com.elasticgrid.examples.video.components.WatchChart.java

public StreamResponse onChart(final int width, final int height, Object... rest)
        throws RemoteException, InterruptedException {
    String serviceID = (String) rest[2];
    String watchID = (String) rest[3];
    System.out.println("Service ID is: " + serviceID + ". Watch ID is: " + watchID);

    List<WatchDataSource> watches = ServiceLocator
            .getWatchDataSourcesByServiceID(ConfigUtil.createServiceID(serviceID));
    WatchDataSource watch = null;//from   w  w w  .  jav a2  s.  co  m
    for (WatchDataSource w : watches) {
        System.out.println("Testing with " + w.getID());
        if (w.getID().equals(watchID))
            watch = w;
    }
    if (watch == null)
        return null;

    TimeSeries s1 = new TimeSeries(watch.getID(), FixedMillisecond.class);
    for (Calculable calculable : watch.getCalculable())
        s1.add(new FixedMillisecond(calculable.getWhen()), calculable.getValue());

    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(s1);

    final JFreeChart chart = ChartFactory.createTimeSeriesChart(watch.getID() + " Watch", // title
            "Date", // x-axis label
            "Value", // y-axis label
            dataset, // data
            true, // create legend?
            true, // generate tooltips?
            false // generate URLs?
    );

    return new StreamResponse() {
        public String getContentType() {
            return "image/png";
        }

        public InputStream getStream() throws IOException {
            BufferedImage image = chart.createBufferedImage(width, height);
            ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
            ChartUtilities.writeBufferedImageAsPNG(byteArray, image);
            return new ByteArrayInputStream(byteArray.toByteArray());
        }

        public void prepareResponse(Response response) {
        }
    };
}

From source file:com.googlecode.jchav.chart.Chart.java

/**
 * Creates a PNG graphic for the given data as a thumbnail image.
 *
 * @param out the stream to write the PNG to.  The caller is responsible
 *  for closing the stream./*from ww  w  .  java 2  s . c o  m*/
 * 
 * @throws IOException if there was a problem creating the chart.
 */
public void writeThumbnail(final OutputStream out) throws IOException {

    // Set up the transfomration:
    final AffineTransform xform = new AffineTransform();
    xform.scale(thumbnailScale, thumbnailScale);

    // Thanks to the almanac for this one:
    // http://javaalmanac.com/egs/java.awt.image/CreateTxImage.html?l=rel
    final AffineTransformOp op = new AffineTransformOp(xform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);

    // The thumbnail does not need so much chart chrome: you can't
    // read the axis and the title is given in the HTML, so removing
    // these elements means there's more space in the thumbnail for the data
    boolean thumbChrome = false;

    if (false == thumbChrome) {
        chart.setTitle((String) null);
        chart.clearSubtitles();
        chart.removeLegend();

        // Removing the axis completly looks just weird, so we just
        // remove the labels:

        chart.getCategoryPlot().getRangeAxis().setLabel(null);
        chart.getCategoryPlot().getRangeAxis().setTickLabelsVisible(true);
        chart.getCategoryPlot().getRangeAxis().setTickMarksVisible(true);
        chart.getCategoryPlot().getRangeAxis().setAxisLineVisible(true);

        //  To show up at a small scale, we need a good sized axis stroke:
        Stroke stroke = new BasicStroke(2f);
        chart.getCategoryPlot().getRangeAxis().setAxisLineStroke(stroke);
        chart.getCategoryPlot().getRangeAxis().setTickMarkStroke(stroke);

        chart.getCategoryPlot().getDomainAxis().setLabel(null);
        chart.getCategoryPlot().getDomainAxis().setTickLabelsVisible(false);
        chart.getCategoryPlot().getDomainAxis().setAxisLineVisible(true);
        chart.getCategoryPlot().getDomainAxis().setAxisLineStroke(stroke);
    }

    final BufferedImage fullsize = chart.createBufferedImage(width, height);

    Graphics2D g = fullsize.createGraphics();
    for (Decorator decorator : thumbnailDecorators) {
        decorator.decorate(g, this);
    }

    final BufferedImage thumbnail = op.filter(fullsize, null /*null means create the image for us*/);

    ChartUtilities.writeBufferedImageAsPNG(out, thumbnail);

}

From source file:com.fiveamsolutions.nci.commons.kmplot.KMPlotImpl.java

/**
 * {@inheritDoc}/*from  w  w w .ja  va2s.  c om*/
 */
public void writePlotImage(OutputStream out) {
    BufferedImage bufferedImage = getPlotChart().createBufferedImage(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    try {
        ChartUtilities.writeBufferedImageAsPNG(out, bufferedImage);
    } catch (IOException e) {
        LOGGER.warn("Couldn't write KMPlot image", e);
    }
}

From source file:org.gumtree.vis.awt.CompositePanel.java

@Override
public void saveTo(String filename, String fileType) throws IOException {
    int filterIndex = 0;
    if (fileType != null) {
        if (fileType.toLowerCase().contains("png")) {
            filterIndex = 0;//  w ww.j  ava 2s  . com
        } else if (fileType.toLowerCase().contains("jpg") || fileType.toLowerCase().contains("jpeg")) {
            filterIndex = 1;
        }
    }
    if (filterIndex == 0) {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
        BufferedImage chartImage = getImage();
        try {
            ChartUtilities.writeBufferedImageAsPNG(out, chartImage);
        } finally {
            out.close();
        }
    } else if (filterIndex == 1) {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
        BufferedImage chartImage = getImage();
        try {
            ChartUtilities.writeBufferedImageAsJPEG(out, chartImage);
        } finally {
            out.close();
        }
    }
}

From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java

/**
 * Writes a chart to an output stream in PNG format.  This method allows
 * you to pass in a {@link ChartRenderingInfo} object, to collect
 * information about the chart dimensions/entities.  You will need this
 * info if you want to create an HTML image map.
 *
 * @param out  the output stream (<code>null</code> not permitted).
 * @param chart  the chart (<code>null</code> not permitted).
 * @param width  the image width.//from   ww w  .j  av  a  2  s  . c om
 * @param height  the image height.
 * @param info  carries back chart rendering info (<code>null</code>
 *              permitted).
 * @param shapeMap 
 * @param encodeAlpha  encode alpha?
 * @param compression  the PNG compression level (0-9).
 *
 * @throws IOException if there are any I/O errors.
 */
public static void writeChartAsPNG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info,
        Rectangle2D imageArea, LinkedHashMap<AbstractMask, Color> maskList,
        LinkedHashMap<Shape, Color> shapeMap, LinkedHashMap<Rectangle2D, String> textContentMap)
        throws IOException {

    if (file == null) {
        throw new IllegalArgumentException("Null 'file' argument.");
    }
    if (chart == null) {
        throw new IllegalArgumentException("Null 'chart' argument.");
    }
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    BufferedImage chartImage = chart.createBufferedImage(width, height, BufferedImage.TYPE_INT_ARGB, info);
    Graphics2D g2 = chartImage.createGraphics();
    drawMasks(g2, imageArea, maskList, null, chart);
    drawShapes(g2, imageArea, shapeMap, chart);
    drawText(g2, imageArea, textContentMap, chart);
    g2.dispose();
    try {
        ChartUtilities.writeBufferedImageAsPNG(out, chartImage);
    } finally {
        out.close();
    }
}

From source file:lucee.runtime.tag.Chart.java

private void copy(OutputStream os, JFreeChart jfc, ChartRenderingInfo info)
        throws ApplicationException, IOException, ExpressionException {
    //OutputStream os = null;
    try {//  w ww .j  a v a 2  s  . c o m
        //os = res.getOutputStream();

        BufferedImage bi;
        if (format == FORMAT_JPG) {
            bi = jfc.createBufferedImage(chartwidth, chartheight, BufferedImage.TYPE_INT_RGB, info);
        } else {
            bi = jfc.createBufferedImage(chartwidth, chartheight, info);
        }
        Image img;

        // add border
        if (showborder) {
            try {
                img = new Image(bi);
                img.addBorder(1, Color.BLACK, Image.BORDER_TYPE_CONSTANT);
                bi = img.getBufferedImage();
            } catch (PageException e) {
            }
        }
        if (format == FORMAT_PNG)
            ChartUtilities.writeBufferedImageAsPNG(os, bi);
        else if (format == FORMAT_JPG)
            ChartUtilities.writeBufferedImageAsJPEG(os, bi);
        else if (format == FORMAT_GIF) {
            img = new lucee.runtime.img.Image(bi);
            img.writeOut(os, "gif", 1, true);

            //throw new ApplicationException("format gif not supported");
        } else if (format == FORMAT_FLASH)
            throw new ApplicationException("format flash not supported");
    } finally {
        IOUtil.flushEL(os);
        IOUtil.closeEL(os);
    }
}