Reputation: 2975
The way I am saving a jFreeChart to a jpeg file is :
JFreeChart chart = ChartFactory.createXYLineChart(
"Hysteresis Plot", // chart title
"Pounds(lb)", // domain axis label
"Movement(inch)", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
true, // tooltips
false // urls
);
Then:
image=chart.createBufferedImage( 300, 200);
The image appeas as:
My save function is:
public static void saveToFile(BufferedImage img)
throws FileNotFoundException, IOException
{
FileOutputStream fos = new FileOutputStream("D:/Sample.jpg");
JPEGImageEncoder encoder2 =
JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam param2 =
encoder2.getDefaultJPEGEncodeParam(img);
param2.setQuality((float) 200, true);
encoder2.encode(img,param2);
fos.close();
}
I am calling it as:
try{
saveToFile(image);
}catch(Exception e){
e.printStackTrace();
}
The saved image appeas as:
Any suggestion, where I am wrong or how to save it the way it appears or may be I need to save as .png. Can anyone let me know how to save as .png?
Thanks
Upvotes: 1
Views: 23386
Reputation: 135
Here is a great example on how this could be done.
File imageFile = new File("C:\\LineChart.png");
int width = 640;
int height = 480;
try {
ChartUtilities.saveChartAsPNG(imageFile, chart, width, height);
} catch (IOException ex) {
System.err.println(ex);
}
Upvotes: 1
Reputation: 11
I would rather suggest that instead of using ImageIo.write in order to save your image, you better use the following function:
ChartUtilities.saveChartAsJPEG("name of your file", jfreechart, lenght, width);
Because then you can manage the size of the picture but also save picture without filters.
Upvotes: 1
Reputation: 2975
A simple Solution:
public static void saveToFile(BufferedImage img)
throws FileNotFoundException, IOException
{
File outputfile = new File("D:\\Sample.png");
ImageIO.write(img, "png", outputfile);
}
Saved the image, the way it appears.
Upvotes: 7