Dynamic Graph

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
devipriya
Posts: 4
Joined: Mon May 22, 2006 4:53 am

Dynamic Graph

Post by devipriya » Mon May 22, 2006 5:45 am

I have an immediate requirement.
I have to draw a dynamic graph based on changing statistical data(Stock market appliaction).The x and y axis in the graph will change according to the data it recieves without using refresh option and all ... In a periodic interval of time ,say for every 10 minutes the application have to check whether it has any changes in the statistical data it recieves (the data maybe from database or form anyother application). If it finds any changes ,automatically it should reflect those changes in the graph.
Please help me out .I have been struggling with this over a period of time but am not able to resolve. :( Please provide me with any sample application and code.
Keeping my fingers crossed.
Thanks in Advance.

JavaAddict
Posts: 3
Joined: Mon Apr 17, 2006 7:20 am

Post by JavaAddict » Sat May 27, 2006 11:28 am

Hi DeviPriya,

I’m a naive user of JfreeChart, please find the code below and see if it is useful to you. Sorry if it seems stupid ;-). If im correct let me know, so that I can improve the code! ;-)

Code: Select all


  import org.jfree.chart.ChartFactory;
 import org.jfree.chart.ChartFrame;
 import org.jfree.chart.JFreeChart;
 import org.jfree.data.general.DefaultPieDataset;
 import org.jfree.data.general.DatasetChangeListener;
 import org.jfree.data.general.DatasetChangeEvent;
 
 public class StockPloter{

        public static void main(String[] args) throws Exception{
               
               DefaultPieDataset dataset = new DefaultPieDataset();

			   //register a listener to get notified for any changes in data set 
			   dataset.addChangeListener(new EventNotifier());

			   //adding a data values to data set result in an event to be get 
			   //fierd, see the op in console also 
			   dataset.setValue("Yes", 45.0); 
			   Thread.sleep(3000); 
			   dataset.setValue("No", 30.0); 
			   Thread.sleep(3000); 
			   dataset.setValue("No Comments", 25.0);        
}


 }

	
 //you need to implement the dataset change listener to get 
 //notification on any changes in dataset

 class EventNotifier implements DatasetChangeListener{

		ChartFrame frame;

		//this variable takes care of avoiding multiple windows to get generated
		//creates a frame only once and updates chart alone later point in time
		boolean fresh;

		EventNotifier(){
			fresh = true;
		}

		public void datasetChanged(DatasetChangeEvent event){

			System.out.println("Dataset Changed");
			
			JFreeChart chart = ChartFactory.createPieChart("Results",
				(DefaultPieDataset)event.getDataset(), true, true, false);
            frame = new ChartFrame("Hello JFreeChart", chart);

			if (fresh){
				frame.pack();
				frame.setVisible(true);
				fresh = false;
			}
		}
 }
reg
Sarathy

devipriya
Posts: 4
Joined: Mon May 22, 2006 4:53 am

Post by devipriya » Tue May 30, 2006 4:26 am

Dear Friend,
Many Thanks.

ashutosh
Posts: 15
Joined: Fri Dec 08, 2006 1:43 pm

Post by ashutosh » Wed Jan 10, 2007 1:48 pm

JavaAddict wrote:Hi DeviPriya,

I’m a naive user of JfreeChart, please find the code below and see if it is useful to you. Sorry if it seems stupid ;-). If im correct let me know, so that I can improve the code! ;-)

Code: Select all


  import org.jfree.chart.ChartFactory;
 import org.jfree.chart.ChartFrame;
 import org.jfree.chart.JFreeChart;
 import org.jfree.data.general.DefaultPieDataset;
 import org.jfree.data.general.DatasetChangeListener;
 import org.jfree.data.general.DatasetChangeEvent;
 
 public class StockPloter{

        public static void main(String[] args) throws Exception{
               
               DefaultPieDataset dataset = new DefaultPieDataset();

			   //register a listener to get notified for any changes in data set 
			   dataset.addChangeListener(new EventNotifier());

			   //adding a data values to data set result in an event to be get 
			   //fierd, see the op in console also 
			   dataset.setValue("Yes", 45.0); 
			   Thread.sleep(3000); 
			   dataset.setValue("No", 30.0); 
			   Thread.sleep(3000); 
			   dataset.setValue("No Comments", 25.0);        
}


 }

	
 //you need to implement the dataset change listener to get 
 //notification on any changes in dataset

 class EventNotifier implements DatasetChangeListener{

		ChartFrame frame;

		//this variable takes care of avoiding multiple windows to get generated
		//creates a frame only once and updates chart alone later point in time
		boolean fresh;

		EventNotifier(){
			fresh = true;
		}

		public void datasetChanged(DatasetChangeEvent event){

			System.out.println("Dataset Changed");
			
			JFreeChart chart = ChartFactory.createPieChart("Results",
				(DefaultPieDataset)event.getDataset(), true, true, false);
            frame = new ChartFrame("Hello JFreeChart", chart);

			if (fresh){
				frame.pack();
				frame.setVisible(true);
				fresh = false;
			}
		}
 }
reg
Sarathy

Can you tell me how can I refresh two dataset with the above code. If I have two dataset that generates two diffrent events. How can I add two pie chart with diffrent events in one single frame.

Locked