Dynamic line chart update

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Jbacnet
Posts: 1
Joined: Thu May 20, 2010 3:37 pm
antibot: No, of course not.

Dynamic line chart update

Post by Jbacnet » Thu May 20, 2010 3:48 pm

hello ,

I have a problem to automatic update :

Code: Select all

package com.sauter_controls.jbacnet.chart;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.time.*;
import org.jfree.ui.*;
import org.jfree.util.UnitType;



public class DynamicWithLignes extends ApplicationFrame
{
	public static JButton jbutton;
	public static double val_pv;
	

	static class MyDemoPanel extends MyPanel implements ActionListener	
	{
	 
		public void actionPerformed(ActionEvent actionevent)
        {
            
            if(actionevent.getActionCommand().equals("ADD_ALL"))
            {
                Millisecond millisecond = new Millisecond();
                System.out.println("Now button= " + millisecond.toString()+"\n Valeur Pv= "+val_pv);
                timeseries.add(new Millisecond(),val_pv);
               
                
            }
        }
		
		//public static final int SUBPLOT_COUNT = 1;
		private static TimeSeriesCollection datasets;
		//private double pv;
		private TimeSeries timeseries;
	
		

		public static TimeSeriesCollection getDatasets() {
			return datasets;
		}
		public static void setDatasets(TimeSeriesCollection datasets) {
			MyDemoPanel.datasets = datasets;
		}
		public TimeSeries getTimeseries() {
			return timeseries;
		}
		public void setTimeseries(TimeSeries timeseries) {
			this.timeseries = timeseries;
		}
		public MyDemoPanel( )
		{
			super(new BorderLayout());

			CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(new DateAxis("Time"));
			//datasets = new TimeSeriesCollection();

			//pv = 0D;
			timeseries = new TimeSeries("pv");
			datasets = new TimeSeriesCollection(timeseries);
			NumberAxis numberaxis = new NumberAxis("Present value");
			numberaxis.setAutoRangeIncludesZero(false);
			XYPlot xyplot = new XYPlot(datasets, null, numberaxis, new StandardXYItemRenderer());
			xyplot.setBackgroundPaint(Color.lightGray);
			xyplot.setDomainGridlinePaint(Color.white);
			xyplot.setRangeGridlinePaint(Color.white);
			combineddomainxyplot.add(xyplot);


			JFreeChart jfreechart = new JFreeChart("Present Values ", combineddomainxyplot);
			addChart(jfreechart);
			LegendTitle legendtitle = (LegendTitle)jfreechart.getSubtitle(0);
			legendtitle.setPosition(RectangleEdge.BOTTOM);
			legendtitle.setMargin(new RectangleInsets(UnitType.ABSOLUTE, 0.0D, 4D, 0.0D, 4D));
			jfreechart.setBorderPaint(Color.black);
			jfreechart.setBorderVisible(true);
			ValueAxis valueaxis = combineddomainxyplot.getDomainAxis();
			valueaxis.setAutoRange(true);
			valueaxis.setFixedAutoRange(20000D);
			ChartUtilities.applyCurrentTheme(jfreechart);
			ChartPanel chartpanel = new ChartPanel(jfreechart);
			add(chartpanel);
			JPanel jpanel = new JPanel(new FlowLayout());
      
            jbutton = new JButton("ALL");
            jbutton.setActionCommand("ADD_ALL");
            jbutton.addActionListener(this);
            jpanel.add(jbutton);
            add(jpanel, "South");
            
            
            
			chartpanel.setPreferredSize(new Dimension(500, 270));
			chartpanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
			
			

		}
		public void update(double pv) {
			Millisecond millisecond = new Millisecond(); 
			val_pv = pv;
	        //datasets.getSeries(0).add(new Millisecond(), pv);
	        //timeseries.add(new Millisecond(),pv);
	        //datasets.addSeries(timeseries);
	        System.out.println("Now update= " + millisecond.toString());
			
		}
		
		
		
	}

	private static MyDemoPanel pane ;
	
	public DynamicWithLignes(String s)
	{
		super(s);
		setContentPane(createDemoPanel());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static JPanel createDemoPanel()
	{
		pane=new MyDemoPanel();
		return pane;
	}

	public static void main(String args[])
	{
		DynamicWithLignes dynamic = new DynamicWithLignes("Present values");
		dynamic.pack();
		RefineryUtilities.centerFrameOnScreen(dynamic);
		dynamic.setVisible(true);
	}

	public static void upChart(double pv) {
		//pane.getTimeseries().add(new Millisecond(),pv);
		//System.out.println("passe dans upChart");
		pane.update(pv);
		jbutton.doClick();
		//pane.revalidate();
		//pane.repaint();
	}
	
}

for add a new value:
DynamicWithLignes.upChart(Double.parseDouble(pv));

for display the graph:
add(DynamicWithLignes.createDemoPanel(), BorderLayout.CENTER);


When I click on the bouton the value the line draw without any problem but when I simulate a click the actionlisterner is performed but the line don't draw.
The dataset and timeseries are correctely fulled.
Any Idea?

Locked