Newsletter:

(Source Code) JAVA - JFreeChart - How To Save a JFreeChart to JPEG File

Source Code : JAVA - JFreeChart - How To Save a JFreeChart to JPEG File

A quick how to use JFreeChart quide can be found at- http://robbamforth.wordpress.com/2008/10/30/java-jfreechart-graphs-and-charts-in-java/

I wanted to be able to output the the charts to a picture file (JPEG) programatically. After a bit of research I got the following methods to paint the chart and output the image to a file.

Add these 2 methods to the class that creates the charts:

Method 1 saveToFile():

public static void saveToFile(JFreeChart chart,

    String aFileName,

    int width,

    int height,

    double quality)

    throws FileNotFoundException, IOException

    {

        BufferedImage img = draw( chart, width, height );

 

        FileOutputStream fos = new FileOutputStream(aFileName);

        JPEGImageEncoder encoder2 =

        JPEGCodec.createJPEGEncoder(fos);

        JPEGEncodeParam param2 =

        encoder2.getDefaultJPEGEncodeParam(img);

        param2.setQuality((float) quality, true);

        encoder2.encode(img,param2);

        fos.close();

    }

 

[Read More..]

Courtesy:- Robbamforth.wordpress.com