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.
Returning Image Object from JFreeChart
RE: Returning Image Object from JFreeChart
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.
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.
RE: Returning Image Object from JFreeChart
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.
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.