1

I am using JFreeChart to make a chart in my program. My problem is I cannot set the height properly. I have tried every command I could think of, I have put it in other panels, however, nothing works. I want to be able to put it somewhere in a corner...or anywhere really and to be able to set both width and height. I have also tried with setPrefferedSize, but it only works with width.

Code :

DefaultPieDataset result = new DefaultPieDataset();
    result.setValue("TotalSwag", totalSwag);
    result.setValue("TotalYolo", totalYolo);
    result.setValue("TotalCool", totalCool);
    result.setValue("TotalLame", totalLame);
    JFreeChart chart = ChartFactory.createPieChart3D("Swag-O-meter", result, true, true, false);
    PiePlot3D plot = (PiePlot3D) chart.getPlot();
    plot.setStartAngle(290);
    plot.setDirection(Rotation.CLOCKWISE);
    plot.setForegroundAlpha(0.5f);
    ChartPanel chartPanel = new ChartPanel(chart, W, H, W, H, W, H,
            false, true, true, true, true, true);
    chartPanel.setMaximumSize(new Dimension(150, 150));

    charPanel.add(chartPanel);
    charPanel.setSize(W, H);
    contentPane.add(charPanel, BorderLayout.WEST);
Ted
  • 629
  • 2
  • 8
  • 19
  • 2
    Try calling `frame.pack();` just before `frame.setVisible(true);`. – Bobulous Apr 21 '13 at 18:04
  • Is there any way to do it without forcing the size of the frame to be with what stuff it has inside? I'd like all windows to be the same size – Ted Apr 21 '13 at 18:33
  • Well my answer is the one that fixed it, but I can only accept it in 24 hours for some reason. Still, your addition was extremely useful, thank you – Ted Apr 22 '13 at 12:55

2 Answers2

3

A JFreeChart is not a JComponent, but a ChartPanel is. You can control the size using any appropriate approach mentioned here.

I'd like all windows to be the same size.

In this case, I'd override getPreferredSize() to return a Dimension having equal width and height. You can use PiePlot#setCircular() to preserve the aspect ratio of the PiePlot.

This example shows a GridLayout(1, 0) of charts each having a ThermometerPlot:

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I managed to make the chart small in the corner, but there is still a huge gray area where the height was earlier. – Ted Apr 21 '13 at 22:07
  • I make the frame any size it's still there, usually it would be ok but it's covering a background image I loaded – Ted Apr 21 '13 at 22:54
  • 1
    Please edit your question to include an [sscce](http://sscce.org/) that shows your approach. [`FauxImage`](http://stackoverflow.com/a/8090328/230513) may be a convenient adjunct. – trashgod Apr 22 '13 at 01:54
  • edited, also please don't judge on the values, it's a mini "ironic" project, which became terribly hard because of the pie chart – Ted Apr 22 '13 at 08:25
0

I have found how to fix it, all I had to do is not use a flowLayout, but a grouplayout, since the panel was making the chart be enlarged, just like if you'd put a button in a flowLayout it would become the size of it's containing zone. Thank you everyone for the amazing help.

Ted
  • 629
  • 2
  • 8
  • 19
  • 1
    `FlowLayout` adopts the preferred size of the component. More on `GroupLayout` [here](http://stackoverflow.com/a/8504753/230513). Are you using a GUI editor? – trashgod Apr 22 '13 at 09:43
  • I am using it to set the layouts automatically, since I'm new at swing and I'm still a bit intimidated by some of the abilities it has. Thank you very much – Ted Apr 22 '13 at 12:54
  • 1
    You are welcome. Don't be afraid to experiment with [short, complete examples](http://sscce.org/), such as this [one](http://stackoverflow.com/a/2561540/230513), that may help you use the editor while you learn. – trashgod Apr 22 '13 at 14:43