Embed JFreeChart chart in a JPanel

Please let me know how I can embed a JFreeChart in a JPanel. If possible, please provide some sample code.

Who is Participating?
for_yanConnect With a Mentor Commented:

You can use ChartPanel which extends JPanel, like that:

    ApplicationFrame af = new ApplicationFrame("Chart");
  JFreeChart chart = af.createChart(dataset);
         ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(600, 350));
       af. setContentPane(chartPanel);

Open in new window

There are lots of examples there
on http://www.java2s.com/Code/Java/Chart/JFreeChartLineChartDemo6.htm
What kind of more code do you want?


Download JFreeChart
from here:


JCommon from here:


Put them in the classpath of your project

Select the best example you like from that www.java2s.com
site and see how it works.

From that point in many cases it is easy to understand
how to modify it to get your stuff working
And yes, there may be discrepancy - the examples may use a litytle bit older version is some unlucky cases,
but then wlooking at API and dealing with the particular case it is in most cases possible to figure out
how to change

Look ath the right side picttures in the examples and select something which
is most close to your task at hand
All Courses

From novice to tech pro — start learning today.