5

I want to create a graph using JFreeChart and display it in a java swing GUI. At the moment I have the following code to display it in a separate frame, please can someone help me out with the code I need in order to display it my GUI?

JFreeChart chart = ChartFactory.createScatterPlot(
                "Title",
                "",
                "",
                myDataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false
                );

ChartFrame frame = new ChartFrame("Results", chart);
frame.pack();
frame.setVisible(true);

1 Answers1

11

A little searching suggests that:

  • A JFreeChart instance can also be added to a ChartPanel.
  • The ChartPanel extends JPanel so can be added to a JFrame (JApplet, JDialog, JWindow, JPanel, JDesktopPane ..).
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for your help. How would I go about setting a chart to be displayed in the ChartPanel though? –  Oct 23 '11 at 19:54
  • ChartPanel is component in the JFreeChart that you create an pass on the chart object. Afterwards you can use it as any other panel while adding to frame. – Ashwinee K Jha Oct 23 '11 at 19:58