Updating and refreshing the graph per second

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Niisha
Posts: 2
Joined: Sat Mar 31, 2012 8:51 am
antibot: No, of course not.

Updating and refreshing the graph per second

Post by Niisha » Tue Apr 17, 2012 2:02 pm

Hi

I am doing a project using jfreechart. I am able to plot the scatter plot from taking the input data from the file. As of now my requirement is to plot the graph by taking the input from a file per second and updating and refreshing the graph per second.My code is as follows.

Code: Select all


import java.awt.Color;
import java.io.*;
import java.lang.*;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


public class LineChartDemo6 extends ApplicationFrame
{
	File inputFile = new File("C:/Documents and Settings/All Users/Desktop/sample.txt"); //The CSV file.
	String separator =",";

	public LineChartDemo6(final String title)
	{
		super(title);
		XYDataset dataset = createDataset();
		JFreeChart chart = createChart(dataset);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		setContentPane(chartPanel);
	}

	private XYDataset createDataset()
	{

		XYSeries series1 = new XYSeries("1st Error");
		XYSeries series2 = new XYSeries("2nd Error");
		XYSeries series3 = new XYSeries("3rd Error");

		try
		{
			DataInputStream dis = null;
			String record = null;
			int recCount = 0;

			File file = inputFile.getAbsoluteFile();
			String path = String.valueOf(file);
			System.out.println(path);


			// File f = new File(path); This is not needed as we already have the inputFile variable having the file.
			FileInputStream fis = new FileInputStream(inputFile); // You can directly pass the file
			BufferedInputStream bis = new BufferedInputStream(fis);
			dis = new DataInputStream(bis);
			record=dis.readLine();
			String[] values;

			Double x_val;
			Double y_val;

			while ( (record = dis.readLine()) != null )
			{

				recCount++;

				// String values = record; // Why you need another string variable. record is already declared as String


				//int location= values.indexOf(separator);

				//String xvalue=values.substring(0,location);
				//String yvalue=values.substring(location+1);

				values = record.split(separator);

				x_val= Double.valueOf(values[0]);

				// First y value
				y_val= Double.valueOf(values[1]);
				series1.add(x_val,y_val);

				// Second y value
				y_val = Double.valueOf(values[2]);
				series2.add(x_val, y_val);

				// Third y value
				y_val = Double.valueOf(values[3]);
				series3.add(x_val, y_val);

			}

		}
		catch(Exception e)
		{
			System.out.println("Exception occured in reading data from the file");
		}

		XYSeriesCollection dataset = new XYSeriesCollection();

		dataset.addSeries(series1);
		dataset.addSeries(series2);
		dataset.addSeries(series3);

		return dataset;

}

/*
* Creates a chart.
*
* @param dataset the data for the chart.
*
* @return a chart.
*/

private JFreeChart createChart(final XYDataset dataset)
{

// create the chart...
final JFreeChart chart = ChartFactory.createScatterPlot(
"Satellite Position Accuracy -PRNID",
"Time",
"Error",
dataset,
PlotOrientation.VERTICAL,
//Using PlotOrientation.HORIZONTAL would display 'x' values 'vertically' & 'y' values horizontally.
true,
true,
false
);

/* createXYLineChart(String title,
String xAxisLabel,
String yAxisLabel,XYDataset dataset,
PlotOrientation,
boolean

legend,boolean tooltips,
boolean urls) */


// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
chart.setBackgroundPaint(Color.white);

// final StandardLegend legend = (StandardLegend) chart.getLegend();
// legend.setDisplaySeriesShapes(true);

// get a reference to the plot for further customisation...
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(1,true);
renderer.setSeriesShapesVisible(1,false);
plot.setRenderer(renderer);

// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// OPTIONAL CUSTOMISATION COMPLETED.

return chart;

}


// Starting point for the demonstration application.
// @param args ignored.

public static void main(final String args[])
{


final LineChartDemo6 frame = new LineChartDemo6("Satellite Performance");
frame.pack();
RefineryUtilities.centerFrameOnScreen(frame);
frame.setVisible(true);


}


}
/// Sample data
X ,Y1 ,Y2 ,Y3
1.0,1.0,1.5,6.0
1.2,2.3,2.5,5.0
2.5,5.6,4.0,5.5
4.7,3.8,6.1,4.9
5.7,4.8,5.0,3.5
////

Could you please help me to refresh and update the graph per second. i have been trying for long time. It involves multithreading concept, i am not sure how exactly to go with. could you plz modify or guide me accordingly. Please do help me!!!!!!!!! :!: Waiting for your reply...very important ....

remiohead
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm
antibot: No, of course not.

Re: Updating and refreshing the graph per second

Post by remiohead » Tue Apr 17, 2012 7:37 pm

Take a look at java.util.Timer.

For the chart dataset you need to update/append with the new data. This will fire various change events that will ultimately repaint the chart view.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Updating and refreshing the graph per second

Post by John Matthews » Thu Apr 19, 2012 10:13 pm

As an alternative to java.util.Timer, suggested by remiohead, javax.swing.Timer offers several advantages outlined in How to Use Swing Timers. DTSCTest is an example.

Niisha
Posts: 2
Joined: Sat Mar 31, 2012 8:51 am
antibot: No, of course not.

Re: Updating and refreshing the graph per second

Post by Niisha » Sun Apr 29, 2012 9:19 am

Thankyou for your response!!!

Hi

I am able to plot the graph per second by using multi threading concept in run method.

Code: Select all

series.addOrUpdate(x1_val, y1_val);
    	try {
    		repaint();
    		
           Thread.sleep(1000);
         
      	}
I would like to know, what ever graph that i get in Jpanel i.e updating and refreshing the graph per second from a file can it be displayed in web browser.

can i transform the code to display in the form of Applet and then use JSP/HTML to call the Applet to display?
I am not sure how exactly to proceed. Could you please guide me in this regard!!!!! :!: :!: :!: . waiting for your response!!!!

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Updating and refreshing the graph per second

Post by John Matthews » Tue May 01, 2012 3:36 pm

I prefer `javax.swing.Timer`; if you `sleep()` on a separate thread, you need to use `invokeLater()` to run on the event dispatch thread. Java Web Start is a good deployment strategy.

Locked