A discussion forum for JFreeChart (a 2D chart library for the Java platform).
-
arwelbath
- Posts: 24
- Joined: Fri Aug 10, 2007 12:37 pm
Post
by arwelbath » Fri Jan 23, 2009 1:38 pm
Hi,
I'm making a chart panel in this way....
Code: Select all
public ChartPanel mkCutThroughsChart(XYSeriesCollection data, XYSeriesCollection point) {
NumberAxis xAxis = new NumberAxis("Value");
NumberAxis yAxis = new NumberAxis("Chi Squared");
xAxis.setAutoRangeIncludesZero(false);
yAxis.setAutoRangeIncludesZero(false);
XYLineAndShapeRenderer renderer_line = new XYLineAndShapeRenderer();
renderer_line.setLinesVisible(true);
renderer_line.setShapesVisible(false);
XYLineAndShapeRenderer renderer_point = new XYLineAndShapeRenderer();
renderer_point.setLinesVisible(false);
renderer_point.setShapesVisible(true);
XYPlot plot = new XYPlot(data, xAxis, yAxis, renderer_line);
plot.setDataset(1,point);
plot.setRenderer(1,renderer_point);
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinesVisible(false);
plot.setRangeGridlinesVisible(false);
JFreeChart chart = new JFreeChart("", plot);
chart.removeLegend();
final ChartPanel panel = new ChartPanel(chart);
panel.getChartRenderingInfo().setEntityCollection(null);
cutThroughsChart = chart;
return panel;
}
Then, to display it I'm putting into a JPanel like this...
Code: Select all
thePanel = window.getCutThroughsPanel();
panelSize = thePanel.getSize();
thePanel.add(chart);
chart.setSize(panelSize);
The chart displays at the correct size initially, but doesn't then re-size as the window is resized.
How can I get the ChartPanel to automatically resize with the panel that contains it?
Cheers,
Arwel
-
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 » Fri Jan 23, 2009 1:41 pm
It is the responsibility of the LayoutManager of the panel that contains the ChartPanel. So read up on the use of LayoutManagers in Swing.
-
arwelbath
- Posts: 24
- Joined: Fri Aug 10, 2007 12:37 pm
Post
by arwelbath » Fri Jan 23, 2009 1:56 pm
Hi David,
The panel is being created in NetBeans gui editor. It's layout is by default set to 'FreeDesign', which sounds like a Netbeans gui designer specific thing.
It does of course have the options of being set to the usual suspects (Border, Flow, Gridbag etc...)
Any hint as to what is the correct one to use in this case would be very handy.
Cheers in advance,
Arwel
-
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 » Fri Jan 23, 2009 2:12 pm
BorderLayout is probably best, but if you don't know why you *really* should read up about Swing layout managers.
-
arwelbath
- Posts: 24
- Joined: Fri Aug 10, 2007 12:37 pm
Post
by arwelbath » Fri Jan 23, 2009 2:19 pm
Thanks David.
I'm not completely ignorant of layout manages, and I have read up on 'Layout Managers 101' back in the bad old days beore I discovered Netbeans GUI editor, and used to code everything in GridBag by hand *shudder*
Now, just changing to BorderLayout on its own isn't fixing it unfortunately. Should I be setting the ChartPanel's PreferredSize to the size of the panel or something???
This is all a bit weird, as I've just dropped ChartPanels into (Netbeans constructed) JPanels before and its worked fine. I can't for the life of me see whats different in this case...
Arwel
-
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 » Fri Jan 23, 2009 2:24 pm
The preferred size won't matter in a BorderLayout (as long as you didn't add the chart to one of the borders). Whatever component occupies the center of the layout will be resized to fit all remaining space. I suspect that NetBeans is doing something that you're not expecting.
-
arwelbath
- Posts: 24
- Joined: Fri Aug 10, 2007 12:37 pm
Post
by arwelbath » Fri Jan 23, 2009 2:52 pm
"I suspect that NetBeans is doing something that you're not expecting"
Yup, sounds about right.
Anyway, it eventually worked (but just dropping it in at CENTRE didn't quite do it... it went in at dimensions[0,0], and I had to re-size manually to the panel size...
Code: Select all
thePanel.setLayout(BorderLayout);
panelSize = thePanel.getSize();
thePanel.add(chart,BorderLayout.CENTER);
chart.setSize(panelSize);
..but now it works fine.
Thanks for your help.
-
mikedezz548
- Posts: 1
- Joined: Fri Aug 03, 2012 10:56 pm
- antibot: No, of course not.
Post
by mikedezz548 » Fri Aug 03, 2012 11:02 pm
Hi, I am fairly new to Jfree and I am running across this same problem. I have been taking a look at how Layout Managers work but I am still not exactly sure how to solve this problem. Could you please explain what you did to fix the problem. Thank you very much in advance.
-
John Matthews
- Posts: 513
- Joined: Wed Sep 12, 2007 3:18 pm
Post
by John Matthews » Sat Aug 04, 2012 11:44 am
The default layout of JPanel is FlowLayout, which relies on a component's preferred size. As David Gilbert notes, BorderLayout.CENTER ignores a component's preferred size: "Whatever component occupies the center of the layout will be resized to fit all remaining space." GridLayout responds similarly. Note that ChartPanel does
not override getPreferredSize(), but you can:
Code: Select all
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart){
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
f.add(chartPanel);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);