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);
}
I had a problem with my pie chart being too big with BorderLayout too. I ended up solving my problem by converting the chart to an image instead.
Before
After
Code
When you create your
ChartPanel
, you have several options that affect the result:Accept the
DEFAULT_WIDTH
andDEFAULT_HEIGHT
: 680 x 420.Specify the preferred
width
andheight
in the constructor.Invoke
setPreferredSize()
explicitly if appropriate.Override
getPreferredSize()
to calculate the size dynamically.Choose the layout of the container to which the
ChartPanel
will be added. Note that the default layout ofJPanel
isFlowLayout
, while that ofJFrame
isBorderLayout
. As a concrete example,ThermometerDemo
uses both preferred values in the constructor and aGridLayout
for the container to allow dynamic resizing.In addition to answer "4" of @trashgod, I had the same problem and managed to solve it like this: (1) create a custom class which extends JPanel (2) get the size somehow, that you would like to pass to your chart (3) create a method which returns a "ChartPanel" object like this:
I prepared a SSCCE to let you know how it works:
I really hope it helps!
Try setting the size of the Panel your chart is in.
You might need to set both JPanel middle and ChartPanel cp
http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=18631
http://www.jfree.org/forum/viewtopic.php?f=3&t=109615
http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=31582