Display JFreeChart inside a panel

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
griver
Posts: 4
Joined: Fri May 16, 2008 4:40 pm
Location: CA

Display JFreeChart inside a panel

Post by griver » Fri May 16, 2008 4:45 pm

I have JFrame form with several panels of user inputs. I would like to display a JFreeChart on a panel inside my JFrame. Is this possible?
>gr<

Taqua
JFreeReport Project Leader
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm
Contact:

Post by Taqua » Fri May 16, 2008 5:22 pm

@see org.jfree.chart.ChartPanel

griver
Posts: 4
Joined: Fri May 16, 2008 4:40 pm
Location: CA

Post by griver » Fri May 16, 2008 10:48 pm

I am new to JFreeChart. Is there an example I could look at for displaying a chart inside a panel?
>gr<

Taqua
JFreeReport Project Leader
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm
Contact:

Post by Taqua » Sat May 17, 2008 11:10 pm

Well, as the name suggests, a ChartPanel is a Swing-Component that displays a chart in a Swing-Component.

If you need info on getting a swing-component displayed in an other swing component, then have a look at the Swing-Tutorials at java.sun.com, they give easy tutorials on how to write Swing-Applications.

http://java.sun.com/docs/books/tutorial/uiswing/

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Sun May 18, 2008 8:24 am

Take a look at org.jfree.chart.demo.PieChartDemo1.java.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

griver
Posts: 4
Joined: Fri May 16, 2008 4:40 pm
Location: CA

Post by griver » Tue May 20, 2008 12:31 am

I looked at the samples and demos. They create a panel before displaying the chart. I would like to display the chart inside an existing JPanel in an existing JFrame so the chart is inside one JFrame. Would I use content.add(call to create scatter chart)?
>gr<

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Tue May 20, 2008 7:17 am

griver wrote:Would I use content.add(call to create scatter chart)?
Depends on what (call to create scatter chart) returns. If it returns a JFreeChart, the answer is no. If it returns a ChartPanel, the answer is yes.

khanhlv
Posts: 21
Joined: Thu May 22, 2008 10:27 am

Post by khanhlv » Fri May 23, 2008 9:45 am

It's simple! Just add the chartPanel into the old panel

ChartPanel chartPanel = new ChartPanel(chart);
oldPanel.add(chartPanel);
oldPanel.UpdateUI();

Let's try!

harrierpark
Posts: 3
Joined: Wed May 28, 2008 9:39 pm

ChartPanel does not appear in JFrame

Post by harrierpark » Thu May 29, 2008 5:19 pm

I am also trying to display a chart in a JPanel; so far without luck. Reading the preceding posts and reviewing the JFreeChart documentation did not produce an answer. Using NetBeans 6.1, I created a simple 3 panel frame (header, side and main) and placed 3 buttons in the side panel. One button is a simple jPanelMain.add(label) that puts a new label in the main panel; this works fine. The second button creates a JFreeChart in a separate window using ChartFrame; this also works fine. BUT, many attempts to move the chart to the panel have failed. The program simply sits there running. I've tried a number of variations on the following basic code, including (but not limited to) UpdateUI, setPreferredSize and setContentPane. There must be a simple solution. Any help is appreciated.

ChartPanel chartpanel = new ChartPanel(chart);
jPanelMain.add(chartpanel);
return;

iaragorn
Posts: 2
Joined: Fri May 30, 2008 12:37 pm

ChartPanel does not appear in JFrame

Post by iaragorn » Fri May 30, 2008 12:42 pm

The same problem. When chart is in separate window is ok. But when I want to put chart into JPanel (which is for example on the JFrame), it doesn't work correctly. The chart is dipslayed but not in JPanel but in whole window and all other components form window just dissappear ! Strange!

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

Re: ChartPanel does not appear in JFrame

Post by skunk » Fri May 30, 2008 6:29 pm

harrierpark wrote: ChartPanel chartpanel = new ChartPanel(chart);
jPanelMain.add(chartpanel);
return;
Does it work as expected if you try jPanelMain.add(new JButton("foo"));

If not, the problem is certainly related to the layout manager that is installed on the container

harrierpark
Posts: 3
Joined: Wed May 28, 2008 9:39 pm

Post by harrierpark » Fri May 30, 2008 7:22 pm

Skunk, No, it does not work with JButtons, (or JCheckBox). So I sent an email to Sun. I will post the reply. Any other suggestions? Thanks.

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

Post by skunk » Fri May 30, 2008 7:46 pm

Post some more of the code that you use to create/init jPanelMain

jpmaia
Posts: 53
Joined: Fri Feb 22, 2008 10:44 pm

Post by jpmaia » Fri May 30, 2008 9:00 pm

Sounds like it might be a problem with the choice of layout manager.

Also....

When you do successive actions to add things to the jPanelMain, do you clear out any old components before adding the new one? It is possible, depending on the layout manager, that if older components are not removed first, the new component will be laid out in a way that is outside the range of the visible window. Also possible: if you haven't set the preferred size of the chart panel, some layout managers won't size your component properly - usually leaving it nothing more than a small dot or box.

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

Post by skunk » Fri May 30, 2008 9:17 pm

jpmaia wrote:Also possible: if you haven't set the preferred size of the chart panel, some layout managers won't size your component properly - usually leaving it nothing more than a small dot or box.
If you read the source of org.jfree.chart.ChartPanel, you will find that the constructor sets the preferred size.

Code: Select all

572            setPreferredSize(new Dimension(width, height));

Locked