Converting a chart to a JPEG or GIF

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply

Converting a chart to a JPEG or GIF

Post by Jonathan » Thu Aug 03, 2000 8:04 am

Hi there I'm wanting to display charts that I have created into
a GIF or JPEG so as I can display them in a WEB Browser..
Would you have any idea on how I might be able to achieve this as I
would love some sort of solution.
Jonathan Michael Gurfinkel.

David Gilbert

RE: Converting a chart to a JPEG or GIF

Post by David Gilbert » Thu Aug 03, 2000 11:28 am

Hi Jonathan,

Take a look at the earlier posting on PNG Images. It has a code snippet that shows how to (1) draw a chart into a BufferedImage, (2) encode the image in PNG format, and (3) save the encoded image to a file. I think the support among the major web browsers for PNG images has improved, so perhaps the above will meet your requirement.

But if you still want JPEG and GIF format images, then a follow-up to the PNG post, from David Berry, lists some encoders you can download for a variety of common image formats - then you would just have to replace step (2) above with whatever encoder you want (I assume most of them will work on a BufferedImage).

I haven't decided yet whether or not to add methods to JFreeChart for producing images - or just to leave the code outside of JFreeChart as in the steps above (opinions, anyone?)




RE: Converting a chart to a JPEG or GIF

Post by TD » Thu Aug 03, 2000 3:48 pm

I say leave it sep., but also provide a sep. download or link to the GIF/JPG/PNG code. Most importantly, put some pages out here with sample code showing how to use ACME GIF, and others.

Sonny Chen

RE: Converting a chart to a JPEG or GIF

Post by Sonny Chen » Wed Aug 09, 2000 5:43 am

If possible, I sugggest you put a encloded to WBMP code. I have do some experiment with Jfreechar and transfer to wap standard.
However, I got some "OutOfmemory" error, which I put all the codes under servlets . Hope you can help me with this.

Sonny Chen

David Berry

RE: Converting a chart to a JPEG or GIF

Post by David Berry » Thu Aug 10, 2000 12:09 am

Hi Sonny,

As you are aware, images really take a lot of memory. By default, the JDK allocates itself 4 MB of memory, which can grow up to 8. If your doing heavy duty image work, this will quickly be exhausted.

You can instruct the JDK to allocate itself additonal memory on startup, which is probably what you want to do. This will vary by the servlet engine that you are using. I myself use Apache JServ, and they allow you to pass flags to the Java Runtime. For me, the appropriate file is /usr/local/jserv/

Here is what the file looks like:

# Execution parameters

# The Java Virtual Machine interpreter.
# Syntax: wrapper.bin=[filename] (String)
# Note: specify a full path if the interpreter is not visible in your path.

# Arguments passed to Java interpreter (optional)
# Syntax: wrapper.bin.parameters=[parameters] (String)
# Default: NONE
wrapper.bin.parameters=-ms16m -mx64m

This tells my Java Runtime to start with an inital 16 MB or memory, and I can expand up to 64 MB. How to set this varies by servlet engine, but your documentation should have something like this somewhere.

One other item to look at. While Java has automatic garbage collection, this doesn't always work as well as advertised, and there are some types of objects the garbage collector can't recycle on its own. Specifically, make sure you dispose of all your Graphics and Graphics2D objects when you are done by explicitely calling the dispose() method. Otherwise, they will never get garbage collected, and you will be out of memory very fast.

The same also applies to any sockets, database connections or other objects that use system resources, but its probably the graphics objects that are causing you grief.

I hope this helps,
David Berry

Sonny Chen

RE: Converting a chart to a JPEG or GIF

Post by Sonny Chen » Thu Aug 10, 2000 9:57 am

Thanks David:
Simnce I am using enhydra server, I have put max the memory to 100MB.
However, it still "hang it there."
also it give me some error as:
java.awt.awtException : can not open XIM
which seems ok.
Also I put a dispose and set the panel to null when finished. So it should be put into garbage cllector.
If you have chace , can you give me some suggestion.

Thanks for your time

Post Reply