Customizing JFreeChart Size for a JPanel
In a JPanel, JFreeChart's default size may not always be optimal. Here's how you can customize the size:
1. Specify Preferred Size in Constructor:
JPanel graph = new JPanel(new GridLayout()); graph.add(new ChartPanel(chart, 500, 300));
2. Invoke setPreferredSize()
ChartPanel cp = new ChartPanel(chart); cp.setPreferredSize(new Dimension(500, 300));
3. Override getPreferredSize()
@Override public Dimension getPreferredSize() { return new Dimension(w, h); }
4. Choose Container Layout
JPanel's FlowLayout is not ideal for resizing, whereas BorderLayout in JFrame allows dynamic resizing.
5. Other Methods
Example:
public void generateChart() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); //set the values of the chart for(int i=0; i<8; i++) { dataset.setValue(income_array[i], "Income", Double.toString(percent_array[i])); } JFreeChart chart = ChartFactory.createBarChart( "Required Annual Income for a Variety of Interest Rates", "Percent", "Income", dataset, PlotOrientation.VERTICAL, false,true, false); ChartPanel cp = new ChartPanel(chart, 500, 300); chart.setBackgroundPaint(Color.white);
The above is the detailed content of How Can I Customize JFreeChart's Size within a JPanel?. For more information, please follow other related articles on the PHP Chinese website!