I’ve added a JFreeChart to a JPanel (using a BorderLayout), and it’s huge. Is there something I can do to make it smaller?
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);
chart.setBackgroundPaint(Color.white);
chart.getTitle().setPaint(Color.black);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.blue);
//cp.setMaximumDrawHeight(5);
//cp.setMaximumDrawWidth(5);
//cp.setZoomOutFactor(.1);
JPanel graph = new JPanel();
graph.add(cp);
middle.add(graph, BorderLayout.CENTER);
}
When you create your
ChartPanel, you have several options that affect the result:Accept the
DEFAULT_WIDTHandDEFAULT_HEIGHT: 680 x 420.Specify the preferred
widthandheightin the constructor.Invoke
setPreferredSize()explicitly if appropriate.Override
getPreferredSize()to calculate the size dynamically.Choose the layout of the container to which the
ChartPanelwill be added. Note that the default layout ofJPanelisFlowLayout, while that ofJFrameisBorderLayout. As a concrete example,ThermometerDemouses both preferred values in the constructor and aGridLayoutfor the container to allow dynamic resizing.