Using JFreeChart in to produce 25+ charts in the same scroll

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
nobody

Using JFreeChart in to produce 25+ charts in the same scroll

Post by nobody » Mon Mar 10, 2003 6:57 am

Hello,

I am relatively new to Java, and want to confirm this before we go on and fix JFreeChart as the component to be used in an upcoming project.

The application would require a single scrollable window (using Swing) that produces 25+ charts (say, 5 rows with 5 charts each). The user should easily scroll within the window (because all the charts wouldn't be visible at once).

Later, the program would have to save PNGs/JPGs of all these 25+ charts to a folder.

I know the questions involve a lot of basic Swing too, but if I can get the answer here, it would be easy for us to assume we'd be using JFreeChart when we take on the Swing project.

Thanks

David Gilbert

Re: Using JFreeChart in to produce 25+ charts in the same sc

Post by David Gilbert » Mon Mar 10, 2003 9:46 am

You can do this by creating multiple instances of the ChartPanel class, putting them into a container panel using an appropriate layout manager, and add the container panel to the JScrollPane. But you should try it out, looking closely at:

- axis alignment : if your charts are in a grid, you might expect the axes to be aligned between all the charts. But JFreeChart does it's own layout for each chart, allocating space as required for the labels that it needs to display on the axes. Different charts may end up with different axis sizes...your alignment may look odd;

- memory usage : the ChartPanel class will draw its chart into a BufferedImage and then copy that image to the screen as required, only updating the image when the chart changes in some way. This improves performance, but uses up memory. You can turn that behaviour off, to eat less memory but more CPU.

Saving the charts to PNG format is no trouble, there are methods in the ChartUtilities class.

Regards,

Dave Gilbert

stoughto
Posts: 2
Joined: Mon Mar 12, 2007 7:25 pm

more clues, please

Post by stoughto » Tue Mar 13, 2007 6:16 pm

David,

I'd lke to do a very similar thing.

I see in TimeSeriesDemo11 that you put four charts on one page. How would I export this to a PNG file?

Regarding aligning:

Suppose I have JFreeChart chart1 and JFreeChart chart2. I want to make a PNG file
that has chart1 in the top of the page and chart2 in the bottom of the page.

It would be nice to have a way to align the domain axes.

For extra credit: draw a vertical line at a significant value on the domain axis that covers both plots.

Is this possible? Could you cook up and example?

Thanks!

stoughto
Posts: 2
Joined: Mon Mar 12, 2007 7:25 pm

almost, but the image is blank

Post by stoughto » Tue Mar 13, 2007 6:47 pm

I found code on another forum, but the resulting file is blank:

JPanel panel = new JPanel(new GridLayout(1, 2));
panel.setSize(800,600);
panel.add(new ChartPanel(chart1));
panel.add(new ChartPanel(chart2));

try {
saveComponentAsJPEG(panel, "fileName.jpg");
} catch (Exception e) {
throw new Error(e);
}

}


public void saveComponentAsJPEG(Component myComponent, String filename)
throws Exception
{
Dimension size = myComponent.getSize();
BufferedImage myImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
g2.dispose();

try
{
OutputStream out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

purduephotog
Posts: 17
Joined: Wed Nov 10, 2004 11:29 pm

Post by purduephotog » Thu Mar 15, 2007 5:41 pm

I haven't been able to get my 3x JPanels to work with JFreechart. I need one 'overview' graph on the left and three 'data' graphs on the right. I originally designed the form with JPanels for containers, but that doesn't work...

Good luck- if you find a solution let me know.

Kousu
Posts: 63
Joined: Wed Mar 07, 2007 10:40 pm

Post by Kousu » Thu Mar 15, 2007 6:51 pm

What? There's no reason it shouldn't work, it's a perfectly elegant solution. Post some code so we can see what you might be doing wrong.

purduephotog
Posts: 17
Joined: Wed Nov 10, 2004 11:29 pm

Post by purduephotog » Fri Mar 16, 2007 2:01 pm

purduephotog wrote:I haven't been able to get my 3x JPanels to work with JFreechart. I need one 'overview' graph on the left and three 'data' graphs on the right. I originally designed the form with JPanels for containers, but that doesn't work...

Good luck- if you find a solution let me know.
It was necessary to add this prior to every add with the jpanels:

Code: Select all

        
jPanel_ScanPattern.setLayout(new FlowLayout());
jPanel_ScanPattern.add(createLatLonCPLPanel());

The default graph panel's just too small now, so I will figure out some way... it's amazing all the ways I can see to use this - but figuring out how is taking more time than I have. Ahhh, the pressures...

Locked