Hi,
I needed to save charts in a readable format, and "I" wrote (with the help of Sun's community forum !) a method to do it. I think it can be useful for other people in order to store and reuse charts.
All the times I used it, it has worked well but there may always be problems ...
To work, the JFreeChartPanel has to be displayed at least once ( else its dimension are (0,0) and the file created is empty) or you have to set the size of the panel.
/*
Method panelToJPEG
function: convert a chart (displayed on a JFreeChartPanel into a jpeg file)
in: -- JFreeChartPanel aJFPanel : chart to be saved
-- aFileName: name of the creatd file
Note: for using this method you need to import
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder ;
(and the JFreeChartPackage)
*/
public static void panelToJPEG(JFreeChartPanel aJFPanel , String aFileName){
try {
Rectangle rect = aJFPanel.getBounds() ;
BufferedImage bi = new BufferedImage( rect.width,rect.height, BufferedImage.TYPE_INT_RGB) ;
aJFPanel.paint(bi.getGraphics() ) ;
ByteArrayOutputStream boutstream = new ByteArrayOutputStream() ;
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(boutstream) ;
encoder.encode(bi) ;
FileOutputStream fimage = new FileOutputStream(new File(aFileName) ) ;
boutstream.writeTo(fimage) ;
fimage.close() ;
}
catch (IOException e){System.out.println("Error in writing " + aFileName) ; }
}
A code to save a chart in a jpeg' file
RE: A code to save a chart in a jpeg' file
Eric,
Thanks for your post. There's also some code in the demo program with the recently released version 0.6.0 which saves charts in PNG format. I prefer the PNG format since it is "lossless", but plenty of people have asked about JPEG so your code is helpful...
Regards,
DG.
Thanks for your post. There's also some code in the demo program with the recently released version 0.6.0 which saves charts in PNG format. I prefer the PNG format since it is "lossless", but plenty of people have asked about JPEG so your code is helpful...
Regards,
DG.
RE: A code to save a chart in a jpeg' file
There is code in the servlet example for saving to a JPEG file. This code is very easy to use and doesn't require a panel - saves a Chart object directly to a file with programmer specified dimension and image quality. I separated this code into a module and built it into a localized version of jfreechart.jar. I'd suggest this code be integrated directly into the shipped version of jfreechart.jar since it would seem to be commonly useful. Let me know if you would like the code that I split into its own file.
RE: A code to save a chart in a jpeg' file
Guy,
Thanks for your suggestion. I finally installed Tomcat the other day and tried out Wolfgang Irler's servlet demo. Now that I understand how it works, I will include it in the standard JFreeChart download as of the next version. I'll also take a look at separating out the JPEG code for general use...
Regards,
DG.
Thanks for your suggestion. I finally installed Tomcat the other day and tried out Wolfgang Irler's servlet demo. Now that I understand how it works, I will include it in the standard JFreeChart download as of the next version. I'll also take a look at separating out the JPEG code for general use...
Regards,
DG.