Millisecond() class loads data via STDIN - no plot till end

A free public discussion forum for the JFreeChart class library.

Millisecond() class loads data via STDIN - no plot till end

Postby jearlcalkins » Tue Jul 04, 2006 9:31 pm

I am a JAVA newbie with dynamic chart repaint issues; working on minimal additions to a plagiarized and very much appreciated DynamicDemoData1.java application.

I'm reading STDIN with BufferedReader(), parsing logged process datapoints and loading them with the add() method into the Millisecond() class. I don’t observe the chart data paint until my piped file (STDIN) is exhausted and my readstdio() process has read it’s last line of datapoints. I expected a chart repaint every time add() executes.

In advance, thanks for everyone's time and help.

Datapoints are read using the following method:

public void readstdio() {
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while ((line = in.readLine( )) != null) {
List l = parse(line); /* my csv parser */
String str_msec = "0"; /* get the time */
String str_sec = String.valueOf(l.get(5));
String str_min = String.valueOf(l.get(4));
String str_hr = String.valueOf(l.get(3));
String str_day = String.valueOf(l.get(1));
String str_month = String.valueOf(l.get(0));
String str_yr = String.valueOf(l.get(2));
String strv = String.valueOf(l.get(20)); /* parsed datapoint */

float val = Float.parseFloat(strv); /* convert parsed data */
int msec_ = Integer.parseInt(str_msec);
int sec_ = Integer.parseInt(str_sec);
int min_ = Integer.parseInt(str_min);
int hr_ = Integer.parseInt(str_hr);
int day_ = Integer.parseInt(str_day);
int month_ = Integer.parseInt(str_month);
int yr_ = Integer.parseInt(str_yr);

this.lastValue = val;
Millisecond now = new Millisecond(msec_, sec_, min_, hr_ , day_, month_, yr_);

this.series.add(now, this.lastValue);
}
}
Jeff Calkins
jearlcalkins
 
Posts: 3
Joined: Mon Jun 26, 2006 3:49 pm
Location: Evergreen, CO

Postby david.gilbert » Wed Jul 05, 2006 1:34 pm

Can you post all the code (if it isn't too large)? Then I can try running it. I'm not sure what is preventing the update, but I don't think it is something in the code you already posted.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11302
Joined: Fri Mar 14, 2003 10:29 am

Millisecond() class loads data via STDIN - no plot till end

Postby jearlcalkins » Wed Jul 05, 2006 8:54 pm

Dave,
Thanks for taking a look. Below is my hack of your original code. A commented out csv dataset at the end of the code generates a good chart, but I don't see the data plot until the dataset is read in it's entirety.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
* A demonstration application showing a time series chart where you can
* dynamically add (random) data by clicking on a button.
*/
public class Dynamic2 extends ApplicationFrame {


public static final String CSV_PATTERN = "\"([^\"]+?)\",?|([^,]+),?|,";
public static Pattern csvRE;

/**
* Constructs a new demonstration application.
* @param title the frame title.
*/
public Dynamic2(String title) {
super(title);
DemoPanel demoPanel = new DemoPanel();
setContentPane(demoPanel);
}

static class DemoPanel extends JPanel implements ActionListener {

public TimeSeries series; /** The time series data. */
private double lastValue = 100.0; /** The most recent value added. */

private int yr_ = 2006; /** yr **/
private int mon_ = 6; /** month **/
private int day_ = 20; /** day **/
private int hr_ = 0; /** one hr **/
private int min_ = 0; /** one minute **/
private int sec_ = 0; /** one second **/
private int msec_ = 0; /** millisecond **/

public static final String CSV_PATTERN = "\"([^\"]+?)\",?|([^,]+),?|,";
public static Pattern csvRE;


/**
* Creates a new instance.
*/
public DemoPanel() {
super(new BorderLayout());
this.series = new TimeSeries("TRANSACTION THROUGHPUT", Millisecond.class);
TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
ChartPanel chartPanel = new ChartPanel(createChart(dataset));
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JButton button = new JButton("Grab STDIO");
button.setActionCommand("ADD_DATA");
button.addActionListener(this);
buttonPanel.add(button);

JButton button2 = new JButton("repaint");
button2.setActionCommand("REPAINT");
button2.addActionListener(this);
buttonPanel.add(button2);

add(chartPanel);
add(buttonPanel, BorderLayout.SOUTH);
}

/**
* Creates a sample chart.
* @param dataset the dataset.
* @return A sample chart.
*/
public JFreeChart createChart(XYDataset dataset) {
JFreeChart result = ChartFactory.createTimeSeriesChart(
"MACHINE THROUGHPUT",
"Time",
"Value",
dataset,
true,
true,
false
);
XYPlot plot = result.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(600000.0); // 600 seconds
axis = plot.getRangeAxis();
axis.setRange(0.0, 10.0);
return result;
}

public static List parse(String line) { /** Parse one line @return List of Strings, minus their double quotes */
List list = new ArrayList( );
Matcher m = csvRE.matcher(line);

// For each field
while (m.find( )) {
// System.out.println(m.groupCount( ));
String match = m.group( );

if (match == null)
break;
if (match.endsWith(",")) {// trim trailing ,
match = match.substring(0, match.length( ) - 1);
}
if (match.startsWith("\"")) { // assume also ends with
match = match.substring(1, match.length( ) - 1);
}
if (match.length( ) == 0)
match = null;
list.add(match);
}
return list;
}

/**
* read a file for data
*/
public void readstdio() {
String line;

csvRE = Pattern.compile(CSV_PATTERN);

try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

while ((line = in.readLine( )) != null) { /** used to be while */

List l = parse(line); // l.size( ) is avail
System.out.print(line);
System.out.println( );

String str_msec = "0";
String str_sec = String.valueOf(l.get(5));
String str_min = String.valueOf(l.get(4));
String str_hr = String.valueOf(l.get(3));
String str_day = String.valueOf(l.get(1));
String str_month = String.valueOf(l.get(0));
String str_yr = String.valueOf(l.get(2));
String strv = String.valueOf(l.get(20));

float val = Float.parseFloat(strv);
int msec_ = Integer.parseInt(str_msec);
int sec_ = Integer.parseInt(str_sec);
int min_ = Integer.parseInt(str_min);
int hr_ = Integer.parseInt(str_hr);
int day_ = Integer.parseInt(str_day);
int month_ = Integer.parseInt(str_month);
int yr_ = Integer.parseInt(str_yr);

/* System.out.print(val); */
/* System.out.println( ); */

this.lastValue = val;
Millisecond now = new Millisecond(msec_, sec_, min_, hr_ , day_, month_, yr_); /** Millisecond now = new Millisecond(); **/
System.out.println("Now:" + now.toString() + " value:" + this.lastValue);
this.series.addOrUpdate(now, this.lastValue); /** OR this.series.add(now, this.lastValue); */
try { /** slow it down */
Thread.sleep(500);
}
catch(Exception e)
{ }
}

}
catch(IOException e){
System.out.println("could NOT read a line");
}
}


/**
* Handles a click on the button by adding new (random) data.
* @param e the action event.
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("ADD_DATA")) {
readstdio();
}
if (e.getActionCommand().equals("REPAINT")) {
/** nothing for now */
}
}
}


public static void main(String[] args) {
Dynamic2 demo = new Dynamic2("Dynamic Data Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);

}
}

/**
dataset example looks like below ...
I pipe the dataset to the application like this: type csv.txt | java Dynamic2
I am only grabbing the 1st 6 comma seperated variables
The application is currently running on an XP box.

the csv.txt file looks like this:

05,10,2006,10,28,22,760, 0.35, 1, ,*errors:, 1,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,002.9
05,10,2006,10,28,23,760, 0.43, 1, ,*errors:, 2,0000.0, sec,ratio,000.0%,*trans:, 0,0000.4, sec,002.6
05,10,2006,10,28,30,760, 0.31, 1, ,*errors:, 3,0000.0, sec,ratio,000.0%,*trans:, 0,0000.4, sec,002.8
05,10,2006,10,28,31,760, 0.43, 1, ,*errors:, 4,0000.0, sec,ratio,000.0%,*trans:, 0,0000.4, sec,002.6
05,10,2006,10,28,32,760, 0.32, 1, ,*errors:, 5,0000.0, sec,ratio,000.0%,*trans:, 0,0000.4, sec,002.7
05,10,2006,10,28,33,760, 0.32, 1, ,*errors:, 6,0000.0, sec,ratio,000.0%,*trans:, 0,0000.4, sec,002.8
05,10,2006,10,28,34,760, 0.35, 1, ,*errors:, 7,0000.0, sec,ratio,000.0%,*trans:, 0,0000.4, sec,002.8
05,10,2006,10,28,35,760, 0.32, 1, ,*errors:, 8,0000.0, sec,ratio,000.0%,*trans:, 0,0000.4, sec,002.8
05,10,2006,10,28,40,760, 0.43, 1, ,*errors:, 9,0000.0, sec,ratio,000.0%,*trans:, 0,0000.4, sec,002.8
05,10,2006,10,28,45,760, 0.32, 1, ,*errors:, 10,0000.0, sec,ratio,000.0%,*trans:, 0,0000.4, sec,002.8
05,10,2006,10,28,46,760, 0.27, 1, ,*errors:, 13,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,002.9
05,10,2006,10,28,47,760, 0.36, 1, ,*errors:, 14,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,002.9
05,10,2006,10,28,48,760, 0.36, 1, ,*errors:, 15,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,002.9
05,10,2006,10,28,50, 1, 0.00, 1, ,*errors:, 16,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.1
05,10,2006,10,28,56,760, 0.48, 1, ,*errors:, 17,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.0
05,10,2006,10,28,57,760, 0.17, 1, ,*errors:, 19,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.3
05,10,2006,10,28,58,760, 0.14, 1, ,*errors:, 20,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.4
05,10,2006,10,28,59,760, 0.33, 1, ,*errors:, 21,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.3
05,10,2006,10,29,00,760, 0.32, 1, ,*errors:, 22,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.3
05,10,2006,10,29,01,760, 0.31, 1, ,*errors:, 25,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.3
05,10,2006,10,29,07,760, 0.42, 1, ,*errors:, 26,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.2
05,10,2006,10,29,11,760, 0.41, 1, ,*errors:, 27,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.2
05,10,2006,10,29,12,760, 0.35, 1, ,*errors:, 29,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.1
05,10,2006,10,29,17,760, 0.42, 1, ,*errors:, 31,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.1
05,10,2006,10,29,18,760, 0.33, 1, ,*errors:, 32,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.1
05,10,2006,10,29,19,760, 0.33, 1, ,*errors:, 34,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.1
05,10,2006,10,29,21, 1, 0.00, 1, ,*errors:, 35,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.2
05,10,2006,10,29,23,760, 0.32, 1, ,*errors:, 36,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.2
05,10,2006,10,29,24,760, 0.35, 1, ,*errors:, 37,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.2
05,10,2006,10,29,27,760, 0.47, 1, ,*errors:, 38,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.1
05,10,2006,10,29,32,760, 0.27, 1, ,*errors:, 39,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.1
05,10,2006,10,29,33,760, 0.34, 1, ,*errors:, 40,0000.0, sec,ratio,000.0%,*trans:, 0,0000.3, sec,003.1

*/
Jeff Calkins
jearlcalkins
 
Posts: 3
Joined: Mon Jun 26, 2006 3:49 pm
Location: Evergreen, CO


Return to JFreeChart - General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 14 guests