Search code examples
javaswinguser-interfacejfreechart

How to display JFreeChart in Java Swing GUI?


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);

Solution

  • 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 ..).