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:org.jfree.chart.demo.MeterChartDemo4.java

public static void main(String args[]) {
    DefaultValueDataset defaultvaluedataset = new DefaultValueDataset(75D);
    MeterPlot meterplot = new MeterPlot(defaultvaluedataset);
    JFreeChart jfreechart = new JFreeChart("Scaled Image Test", meterplot);
    try {/*from w  w  w . jav  a2s  .  com*/
        File file = new File("meterchart100.png");
        BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file));
        java.awt.image.BufferedImage bufferedimage = jfreechart.createBufferedImage(200, 200, 400D, 400D, null);
        ChartUtilities.writeBufferedImageAsPNG(bufferedoutputstream, bufferedimage);
    } catch (IOException ioexception) {
        System.out.println(ioexception.toString());
    }
}

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

public static void main(String args[]) throws IOException {
    DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    defaultpiedataset.setValue("A", 1.0D);
    defaultpiedataset.setValue("B", 2D);
    defaultpiedataset.setValue("C", 3D);
    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);
    graphics2d.dispose();/*from   ww  w  .  java 2 s . c o m*/
    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("LegendTitleToImageDemo1.png")));
    ChartUtilities.writeBufferedImageAsPNG(bufferedoutputstream, bufferedimage1);
    bufferedoutputstream.close();
}

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();/*ww w .j av a 2 s . c o  m*/
    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:org.jfree.chart.demo.MeterChartDemo2.java

/**
 * Starting point for the demo./*from ww w  .ja  va2 s  .c  om*/
 *
 * @param args  ignored.
 */
public static void main(final String[] args) {

    final ValueDataset dataset = new DefaultValueDataset(75.0);
    final MeterPlot plot = new MeterPlot(dataset);
    final JFreeChart chart = new JFreeChart("Scaled Image Test", plot);

    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    * 
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************

    // save it to an image
    try {
        final File file1 = new File("meterchart100.png");
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(file1));
        final BufferedImage image = chart.createBufferedImage(200, 200, 400, 400, null);
        ChartUtilities.writeBufferedImageAsPNG(out, image);
    } catch (IOException e) {
        System.out.println(e.toString());
    }

}

From source file:gov.nih.nci.caintegrator.application.geneexpression.GeneExpressionPlotImpl.java

/**
 * {@inheritDoc}//w  ww.ja  v a 2 s  .  c  o m
 */
public void writePlotImage(OutputStream out) {
    BufferedImage bufferedImage = getPlotChart().createBufferedImage(width, height);
    try {
        ChartUtilities.writeBufferedImageAsPNG(out, bufferedImage);
    } catch (IOException e) {
        LOGGER.warn("Couldn't write GeneExpressionPlot image", e);
    }
}

From source file:gov.nih.nci.caintegrator.application.kmplot.KMPlotImpl.java

/**
 * {@inheritDoc}/*from  ww w.j  av a  2  s . c  om*/
 */
public void writePlotImage(OutputStream out) {
    fixHeight();
    BufferedImage bufferedImage = getPlotChart().createBufferedImage(width, height);
    try {
        ChartUtilities.writeBufferedImageAsPNG(out, bufferedImage);
    } catch (IOException e) {
        LOGGER.warn("Couldn't write KMPlot image", e);
    }
}

From source file:org.sonar.api.charts.AbstractChartTest.java

protected void assertChartSizeGreaterThan(BufferedImage img, int size) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ChartUtilities.writeBufferedImageAsPNG(output, img);
    assertTrue("PNG size in bits=" + output.size(), output.size() > size);
}

From source file:org.sonar.api.charts.AbstractChartTest.java

protected void assertChartSizeLesserThan(BufferedImage img, int size) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ChartUtilities.writeBufferedImageAsPNG(output, img);
    assertTrue("PNG size in bits=" + output.size(), output.size() < size);
}

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

public void writeBufferedImage(OutputStream out, BufferedImage image, ImageTypes imgType) throws KMException {
    if (imgType == null)
        imgType = ImageTypes.PNG; // default

    try {// w ww  .  j  av a2  s  . c o m
        if (imgType == ImageTypes.PNG)
            ChartUtilities.writeBufferedImageAsPNG(out, image);
        else if (imgType == ImageTypes.JPEG)
            ChartUtilities.writeBufferedImageAsJPEG(out, image);
        else {
            logger.debug(new String("UnSupported File Format: " + imgType.getValue()));
            throw new KMException("UnSupported File Format: " + imgType.getValue());
        }
    } catch (IOException ioe) {
        logger.debug(ioe);
        throw new KMException(ioe);
    }

}

From source file:org.sonar.api.charts.AbstractChartTest.java

protected void saveChart(BufferedImage img, String name) throws IOException {
    File target = new File("target/tmp-chart", name);
    FileUtils.forceMkdir(target.getParentFile());
    ByteArrayOutputStream imgOutput = new ByteArrayOutputStream();
    ChartUtilities.writeBufferedImageAsPNG(imgOutput, img);
    OutputStream out = new FileOutputStream(target);
    out.write(imgOutput.toByteArray());//from  w  w w . ja  va2 s  .  co  m
    out.close();

}