Returning Image Object from JFreeChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Bijay Ghimire

Returning Image Object from JFreeChart

Post by Bijay Ghimire » Thu Dec 21, 2000 2:56 am

Help ! Help ! Help !

How to return plotted graph as Image Object type so that it can be further manipulated or converted into GIF. I am using JFreeChart in server side graph generation.

Can anybody help me in this matter?

Bijay Ghimire
from Nepal.

David Gilbert

RE: Returning Image Object from JFreeChart

Post by David Gilbert » Thu Dec 21, 2000 2:03 pm

Hi Bijay,

Click on the 'Search' link, and enter 'PNG' then select 'All dates' ('Last 30 days' is the default) and click the 'Search' button. That returns me two messages from a while back in this forum. The second one includes some sample code to create PNG images using JFreeChart. The sample code uses a free library - the link is in the original message (and also on the links page on my website).

You can do something similar for GIF, but I don't think there are any free GIF encoders because of a patent that some big company (can't remember who) holds on GIF. PNG is a better format anyway, and most browsers support it, so I'd recommend using PNG.

Hope that helps,

DG.

Wolfgang Irler

RE: Returning Image Object from JFreeChart

Post by Wolfgang Irler » Mon Mar 05, 2001 10:42 am

Hi Bijay,
I needed an image servlet and did my own demo. Here some JDK1.2 snipplet:
...
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
....
// create the chart
JFreeChart chart = createChart();
...
BufferedImage img = draw( chart, width, height );
OutputStream out = response.getOutputStream();
JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(img);
param.setQuality(1.0f,true);
encoder.encode(img,param);
out.close();

protected BufferedImage draw(JFreeChart chart, int width, int height)
{
BufferedImage img =
new BufferedImage(width , height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = img.createGraphics();
chart.draw(g2, new Rectangle2D.Double(0, 0, width, height));
g2.dispose();
return img;
}
..
hope this is of some help
Wolfgang
PS: I sent the entire WAR for the servlet demo to David.

Locked