save as jpg or gif file
save as jpg or gif file
It seems I can't save image as jpg or gif files in JFreechart. Does anyone know how I can save image as jpg or gif files or what package I can use to achieve this? Thanks.
RE: save as jpg or gif file
There is a new class ChartUtilities (in JFreeChart 0.7.0) that has methods for saving charts as PNG or JPEG format files. I don't recommend JPEG for charts (JPEG was designed for photographic images), PNG is better for charts - it is a lossless compression algorithm so you don't lose any detail when you compress your charts.
GIF is an older, proprietary, format that does a similar job to PNG, but it is technically inferior to PNG. Most browsers support GIF, some of the older ones don't support PNG.
You choose...
DG.
GIF is an older, proprietary, format that does a similar job to PNG, but it is technically inferior to PNG. Most browsers support GIF, some of the older ones don't support PNG.
You choose...
DG.
RE: save as jpg or gif file
Thanks. Based on your info., I modified the code so I can save it as JPG image -- reason is I want to see it in IE and I can't see PNG in browser. I modified method attemptSaveAs() at class JFreeChartPanel as below,
private void attemptSaveAs() {
JFileChooser fileChooser = new JFileChooser();
ExtensionFileFilter filterPNG = new ExtensionFileFilter("PNG Image Files", ".png");
fileChooser.addChoosableFileFilter(filterPNG);
ExtensionFileFilter filterJPG = new ExtensionFileFilter("JPG Image Files", ".jpg");
fileChooser.addChoosableFileFilter(filterJPG);
int option = fileChooser.showSaveDialog(this);
if (option==JFileChooser.APPROVE_OPTION) {
String fileDesc = fileChooser.getFileFilter().getDescription();
if (fileDesc.startsWith("PNG")) {
if(!fileChooser.getSelectedFile().getName().toUpperCase().endsWith("PNG")) ChartUtilities.saveChartAsPNG(new File(fileChooser.getSelectedFile().getAbsolutePath() + ".png"), this.chart, this.getWidth(), this.getHeight());
else ChartUtilities.saveChartAsPNG(fileChooser.getSelectedFile(), this.chart, this.getWidth(), this.getHeight());
} else if (fileDesc.startsWith("JPG")) {
if(!fileChooser.getSelectedFile().getName().toUpperCase().endsWith("JPG")) ChartUtilities.saveChartAsJPEG(new File(fileChooser.getSelectedFile().getAbsolutePath() + ".jpg"), this.chart, this.getWidth(), this.getHeight());
else ChartUtilities.saveChartAsJPEG(fileChooser.getSelectedFile(), this.chart, this.getWidth(), this.getHeight());
}
}//else
}
private void attemptSaveAs() {
JFileChooser fileChooser = new JFileChooser();
ExtensionFileFilter filterPNG = new ExtensionFileFilter("PNG Image Files", ".png");
fileChooser.addChoosableFileFilter(filterPNG);
ExtensionFileFilter filterJPG = new ExtensionFileFilter("JPG Image Files", ".jpg");
fileChooser.addChoosableFileFilter(filterJPG);
int option = fileChooser.showSaveDialog(this);
if (option==JFileChooser.APPROVE_OPTION) {
String fileDesc = fileChooser.getFileFilter().getDescription();
if (fileDesc.startsWith("PNG")) {
if(!fileChooser.getSelectedFile().getName().toUpperCase().endsWith("PNG")) ChartUtilities.saveChartAsPNG(new File(fileChooser.getSelectedFile().getAbsolutePath() + ".png"), this.chart, this.getWidth(), this.getHeight());
else ChartUtilities.saveChartAsPNG(fileChooser.getSelectedFile(), this.chart, this.getWidth(), this.getHeight());
} else if (fileDesc.startsWith("JPG")) {
if(!fileChooser.getSelectedFile().getName().toUpperCase().endsWith("JPG")) ChartUtilities.saveChartAsJPEG(new File(fileChooser.getSelectedFile().getAbsolutePath() + ".jpg"), this.chart, this.getWidth(), this.getHeight());
else ChartUtilities.saveChartAsJPEG(fileChooser.getSelectedFile(), this.chart, this.getWidth(), this.getHeight());
}
}//else
}