how to update my Linechart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
riadhhwajdii
Posts: 12
Joined: Tue Aug 25, 2009 9:12 am
antibot: No, of course not.

how to update my Linechart

Post by riadhhwajdii » Mon Aug 31, 2009 10:46 am

hi
i red in jfreechart Developer Guide that JFreeChart uses an event notification mechanism to update a chart
but i don't know how to implement DatasetChangeListener and DatasetChangeEvent
please help me :(

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: how to update my Linechart

Post by david.gilbert » Mon Aug 31, 2009 9:58 pm

If you are using one of the standard dataset classes, it is all taken care of for you. Just call the methods that update the dataset and the correct event notification will be sent.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

riadhhwajdii
Posts: 12
Joined: Tue Aug 25, 2009 9:12 am
antibot: No, of course not.

Re: how to update my Linechart

Post by riadhhwajdii » Tue Sep 01, 2009 8:12 am

could you please explain more
there is my class:

Code: Select all

public class TestJfreeChart implements DatasetChangeListener {
	XYDataset dataset;
	
	public TestJfreeChart()
	{
		
	}
	
	public String generateChartMethod(){
		System.out.println("Appel de generateChartMethod");
	
		 JFreeChart chart = ChartFactory.createTimeSeriesChart( 
	                "Test",  	// title 
	                "Date",		// x-axis label 
	                "Nombre",   // y-axis label 
	                getDataset(),	// data 
	                true,       // create legend? 
	                true,       // generate tooltips? 
	                false       // generate URLs? 
	        		); 
		 
		 XYPlot plot = chart.getXYPlot();
		 plot.setBackgroundPaint(Color.white);
		 plot.setDomainGridlinePaint(Color.GRAY);
		 plot.setRangeGridlinePaint(Color.GRAY);
		 HttpSession s=SessionUtil.getSession();
		 String adressePagesWeb=s.getServletContext().getRealPath("/")+"pages/";
	 
		 try {
		         File file1 = new File(adressePagesWeb+"piechart.png");
                         ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, null);
                       } catch (Exception e) {
                                 e.printStackTrace();
                                   }
               return "jfreechart";
		}
	
	public XYDataset getDataset() {
		
		HttpSession session=SessionUtil.getSession();
		TimeSeriesCollection dataset;
		try{
			 dataset=(TimeSeriesCollection)session.getAttribute("jfreechart");
			 dataset.toString();
		}catch(NullPointerException e)
		{
			dataset=new TimeSeriesCollection();
			TimeSeries s3 = new TimeSeries("Series 3");
		       s3.add(new Minute(1,new Hour()),5);
		        s3.add(new Minute(2,new Hour()),7);
		        s3.add(new Minute(3,new Hour()),8);
		        s3.add(new Minute(4,new Hour()),10);
		        s3.add(new Minute(5,new Hour()),20);
		        s3.add(new Minute(6,new Hour()),50);
		        s3.add(new Minute(7,new Hour()),55);
		        s3.add(new Minute(8,new Hour()),100);
		        s3.add(new Minute(22,new Hour()),150);
		        s3.add(new Minute(23,new Hour()),400);
		        s3.add(new Minute(35,new Hour()),450);
		        s3.add(new Minute(55,new Hour()),100);
	           dataset.addSeries(s3);
	         session.setAttribute("jfreechart",dataset);
		}
		
		return dataset;
	}

	public void setDataset(XYDataset dataset) {
		this.dataset = dataset;
	}
		public void update()
		{
			HttpSession session=SessionUtil.getSession();
			TimeSeriesCollection dataset;
			Calendar calendrier = Calendar.getInstance();
			int min=calendrier.MINUTE;
			dataset=(TimeSeriesCollection)session.getAttribute("jfreechart");
			
			try{
				dataset.getSeries(0).add(new Minute(59,new Hour()),600);
				session.setAttribute("jfreechart",dataset);
				generateChartMethod();
			}catch(Exception e){
				e.printStackTrace();
			}
		}

		public void datasetChanged(DatasetChangeEvent arg0) {
			
		}
	
}


paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: how to update my Linechart

Post by paradoxoff » Tue Sep 01, 2009 9:24 pm

In your getDataset method, you are trying to get a dataset from your session. If that fails, you create a new dataset, and write that back to the session.
In your update method, you are retrieving a dataset from the session, add a value to it and then write it back to the session. No check for a null dataset is performed.
You then call generateChartMethod() which constructs a JFreeChart using the return value of getDataset as dataset, and then save the chart to disk.

Seems ok so far. When you call generateChartMethod() for the first time, the dataset stored in the session is probably null, and you will thus create a new one.
If you invoke update, you should get a reference to the same dataset, add a value, and write it back, i. e. you should effectively update your dataset.
If you then call generateChartMethod() again, I would expect that a chart is generated with the additional value.
You haven´t told us what you are doing exactly, most importantly when you are calling the three methods mentioned above, and whether you call these methods at all. Does the sequence
generateChartMethod()-update()-generateChartMethod() lead to two different charts?
If so, you could add the following lines to your getDataset() method, specifically to your catch block

Code: Select all

dataset.addChangeListener(this);
and then implement the datasetChanged(DatasetChangeEvent arg0) method as follows

Code: Select all

public void datasetChanged(DatasetChangeEvent arg0) {
    generateChartMethod();         
}
If the sequence of methods mentioned above produces two identical charts (maybe you should make sure that you use two different names), then something else is wrong.

Locked