Is it possible to cast an image/BufferedImage to JFreeChart?
Asked
Active
Viewed 2.4k times
6
-
1You want to cast/convert an image to a chart? I don't understand how that might work – klonq Mar 11 '11 at 13:57
-
2Do you want to make images/BufferedImages with Jfreechart? – Piyush Mattoo Mar 11 '11 at 18:03
3 Answers
21
Casting an image to JFree is not possbile. To create an image from JFreechart you can do the following:
BufferedImage objBufferedImage=objJFreechart.createBufferedImage(600,800);
ByteArrayOutputStream bas = new ByteArrayOutputStream();
try {
ImageIO.write(objBufferedImage, "png", bas);
} catch (IOException e) {
e.printStackTrace();
}
byte[] byteArray=bas.toByteArray();
This creates the byte[] .
Now you need to create the image from byte[]. The following does this.
InputStream in = new ByteArrayInputStream(obj);
BufferedImage image = ImageIO.read(in);
File outputfile = new File("image.png");
ImageIO.write(image, "png", outputfile);
The image gets created at the place where your project is created(local drive).

Anuj Balan
- 7,629
- 23
- 58
- 92
-
13Another way to create image from JFreeChart is by using `ChartUtilities.writeChartAsPNG(ouputstream,jfreechart,x,y)` function. – giannis christofakis Dec 18 '11 at 21:38
-
1yannis hristofakis: that way, you don't have access to the BufferedImage. I needed this because the chart adds some padding which I couldn't get rid of by configuring the chart, so I had to crop the image - BufferedImage.getSubImage(...) – Buffalo Nov 01 '13 at 14:43
17
JfreeChart takes data first and generate image using generic ChartUtilities class or any customized utility class.
ChartUtilities.writeChartAsPNG(outputstream,getDataset(), width,height);
Maybe this can help you:here

Konrad Viltersten
- 36,151
- 76
- 250
- 438

Vaibs
- 2,018
- 22
- 29
4
The JFreeChart
object is for making images, it does not consume them and images cannot be converted into a JFreeChart object.
See: http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html

Adam
- 3,675
- 8
- 45
- 77