cancel
Showing results for 
Search instead for 
Did you mean: 

Creating pie chart diagram in java and converting into BMP file

Former Member
0 Kudos

hi all ,

my req. is to create draw a pie chart diagram and store

the picture in BMP. it is Possible. if so how ?

cheers

senthil

Accepted Solutions (0)

Answers (2)

Answers (2)

0 Kudos

Hi Senthil,

This response is a bit late but I hope it can help in some way. Your requirement (to create draw a pie chart diagram and store the picture in BMP) is possible and actually quite easy if you don't mind using two open source libraries. In his previous response to this thread, Detlev mentioned JFreeChart for as a solution for charting however he also mentioned that it lacks support for BMP. Although this is true, you can use the JFreeChart library (http://www.jfree.org/jfreechart/index.html) in conjunction with an imaging library to meet your requirement. I have used JFreeChart on multiple projects and I highly recommend it. For the imaging library you have many options, however the only one I have used is The Java Imaging Utilities (JIU - http://jiu.sourceforge.net/). Using these two class libraries, you can generate your pie chart and save it to a BMP file with the following block of code:

try

{

JFreeChart chart = this.createChart();

FileOutputStream streamOut = new FileOutputStream("your/files/path/BMPfile.bmp");

BufferedImage image = chart.createBufferedImage(600, 300);

if(image == null)

{

reportError("Chart Image is NULL!!!!!!!!");

return(false);

}

RGB24Image rgbImage = ImageCreator.convertImageToRGB24Image(image);

Codec codec = new BMPCodec();

codec.setImage(rgbImage);

codec.setOutputStream(streamOut);

codec.process();

codec.close();

streamOut.flush();

streamOut.close();

}catch(IOException ioExcept)

{

// throw or handle IOException...

return(false);

}catch(OperationFailedException opFailedExcept)

{

// throw or handle OperationFailedException...

return(false);

}

The first line inside the catch block calls a helper method that should create the actual chart using the JFreeChart library. Once you have your chart instance (all chart types are represented using the JFreeChart class), you can then retrieve a BufferedImage of the chart (JFreeChart.createBufferedImage(int width, int height);). In our situation, the BufferedImage class will act as an "intermediate" class, for both libraries (the charting and imaging) can make sense of BufferedImages. The rest of the code deals with storing the generated chart (the BufferedImage) as a BMP file to disk. The RGB24Image, Codec, BMPCodec, CodecMode, and OperationFailedException classes are all part of the JIU library. Also, note that the storage source is not solely limited to a File on disk; the Codec.setOutputStream(OutputStream os) method will accept any subclass of OutputStream. This implies that not only can you store to the actual App Server disk (where your app is running) but also to sources such as Knowledge Management (KM) resources or even directly to the clients browser (in the case of an iView).

Once again, sorry for the late response and let me know if you have any questions regarding the code above.

Regards,

Michael Portner

detlev_beutner
Active Contributor
0 Kudos

Hi Senthil,

a commercial but low priced product would be ChartDirector: http://www.advsofteng.com/product.html

JCharts and JFreeChart, both open source, do not support BMP

But see http://www.geocities.com/marcoschmidt.geo/java-image-coding.html for tools (open source as well as commercial) converting into BMP.

Hope it helps

Detlev