JFreeChart implements org.jfree.ui.Drawable so you can create your custom JPanel, override public void paint(Graphics g) and in this method you can create JFreeChart, then you can call method draw of JfreeChart object with arg Graphic.
When data will change repaint this JPanel
below is example as you wish:
/** Your custom JPanel */
public class MyPanel extends JPanel{
Random random =new Random(System.currentTimeMillis()); //this is only to simulate change data
public MyPanel(){
//simulation change data
new Thread(){
public void run(){
while(true){
try {
Thread.sleep(3000l);//in every 3 sec refresh
Thread.yield(); // release processor
repaint(); //repaint panel with new data
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}.start();
}
public void paint(Graphics g){
//paint panel
super.paint(g);
// create chart
JFreeChart lineChart = ChartFactory.createLineChart(
"My Title",
"Years","Number of Schools",
createDataset(),
PlotOrientation.VERTICAL,
true,true,false);
//draw chart on panel
lineChart.draw((Graphics2D) g, this.getVisibleRect());
}
/** create data for chart */
private DefaultCategoryDataset createDataset( )
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
dataset.addValue( random.nextInt(100) , "schools" , "1970" );
dataset.addValue( random.nextInt(100) , "schools" , "1980" );
dataset.addValue( random.nextInt(100) , "schools" , "1990" );
dataset.addValue( random.nextInt(100) , "schools" , "2000" );
dataset.addValue( random.nextInt(100) , "schools" , "2010" );
dataset.addValue( random.nextInt(100) , "schools" , "2014" );
return dataset;
}
}