Programatically save a chart as an image

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
fabiok
Posts: 3
Joined: Fri Jul 16, 2010 12:57 am
antibot: No, of course not.

Programatically save a chart as an image

Post by fabiok » Fri Jul 16, 2010 1:16 am

Hi!

I have a program that generate a couple of charts dinamically. After some periodic calculations, the charts are updated with a new bar during a period user-defined.

I know that with the right-click button we can save the chart as an image. But can I access the method to save the chart as an image programatically?
I mean, an image of the final chart must be saved and I don't want ask the user to do it. I wanna do it programatically...

Is it possible?

Thanks in advance!

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Programatically save a chart as an image

Post by skunk » Fri Jul 16, 2010 3:04 am

Look at org.jfree.chart.ChartUtilities

nemesi8s
Posts: 5
Joined: Mon Jun 28, 2010 3:16 pm
antibot: No, of course not.

Re: Programatically save a chart as an image

Post by nemesi8s » Fri Jul 16, 2010 12:06 pm

skunk wrote:Look at org.jfree.chart.ChartUtilities
That stuff is a piece of sh**. It generates a low quality image (even if the image quality float is maximized), with lower (yes, it can be lower) quality text on it. Sadly there's no other method for saving a chart to file...

pooo
Posts: 9
Joined: Tue Jul 13, 2010 2:04 pm
antibot: No, of course not.

Re: Programatically save a chart as an image

Post by pooo » Fri Jul 16, 2010 2:26 pm

If you don't want to deal with ChartUtilities, I suggest you have a look at the ScreenImage class. I never used it myself, but it looks rather simple.

http://tips4java.wordpress.com/2008/10/13/screen-image/

Basically, all you have to do is create a chart, add this chart to a ChartPanel, call the method ScreenImage.createImage(...), and eventually call the method ScreenImage.writeImage(...) to store the image to a file.

Code: Select all

JFreeChart chart = ChartFactory.createLineChart(...);
ChartPanel panel = new ChartPanel(chart);
ScreenImage.writeImage(ScreenImage.createImage(panel), "myImageFile.jpg");

fabiok
Posts: 3
Joined: Fri Jul 16, 2010 12:57 am
antibot: No, of course not.

Re: Programatically save a chart as an image

Post by fabiok » Fri Jul 16, 2010 9:31 pm

Thank you for the replies!

No problem in using the org.jfree.chart.ChartUtilities...

I'll try using this class.

fabiok
Posts: 3
Joined: Fri Jul 16, 2010 12:57 am
antibot: No, of course not.

Re: Programatically save a chart as an image

Post by fabiok » Sun Aug 01, 2010 7:31 pm

skunk wrote:Look at org.jfree.chart.ChartUtilities
By clicking in "Save Image" in context menu (clicking directly on generated map) who method is called? org.jfree.chart.ChartUtilities.saveChartAsPNG ?

Thanks in advance!

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Programatically save a chart as an image

Post by skunk » Sun Aug 01, 2010 8:46 pm

Read the source for org.jfree.chart.ChartPanel

Code: Select all

public void doSaveAs() throws IOException {

Locked