-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiSeriesGraph.java
More file actions
137 lines (115 loc) · 4.45 KB
/
MultiSeriesGraph.java
File metadata and controls
137 lines (115 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.awt.*;
import java.awt.event.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;
class MultiSeriesGraph {
/*@ specification MultiSeriesGraph {
double x;
alias (double) ys;
void init_ready, drawing_ready, paintAll, done, updated;
boolean repaintImmediately;
//NB! seriesNames if defined must match ys
String[] seriesNames;
seriesNames -> init_ready{setSeriesName};
x, ys, repaintImmediately -> drawing_ready, (Exception){draw};
paintAll, repaintImmediately ->done{drawAll};
boolean axisAlwaysIncludeZero, showSeparateAxis;
float lineThickness;
lineThickness, axisAlwaysIncludeZero, showSeparateAxis -> updated {updateParams};
}@*/
XYSeriesCollection dataset;
ChartFrame frame;
XYPlot plot;
boolean isInitialized;
MultiSeriesGraph() {
}
private void init( int length ) {
dataset = new XYSeriesCollection();
for ( int i = 0; i < length; i++ ) {
dataset.addSeries( new XYSeries( "" + i, false, true ) );
}
JFreeChart chart = ChartFactory.createXYLineChart( "", "time(min)", "value",
dataset, PlotOrientation.VERTICAL, true, true, false );
chart.removeLegend();
plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.black);
plot.setRangeGridlinePaint(Color.darkGray);
plot.setDomainGridlinePaint(Color.darkGray);
frame = new ChartFrame( "Graph", chart );
frame.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing( WindowEvent e ) {
System.out.println( "Chart frame closed - terminating program" );
frame.dispose();
ee.ioc.cs.vsle.api.ProgramContext.terminate();
}
} );
frame.pack();
frame.setVisible( true );
isInitialized = true;
}
public void updateParams( float lineThickness,
boolean axisAlwaysIncludeZero, boolean showSeparateAxis ) {
plot.getRenderer().setStroke( new BasicStroke( lineThickness ) );
NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
domainAxis.setAutoRangeIncludesZero( axisAlwaysIncludeZero );
if ( showSeparateAxis && dataset.getSeriesCount() > 1 ) {
for ( int i = 0, len = dataset.getSeriesCount(); i < len; i++ ) {
NumberAxis axis = new NumberAxis( dataset.getSeries( i )
.getKey().toString() );
axis.setAutoRangeIncludesZero( axisAlwaysIncludeZero );
plot.setRangeAxis( i, axis );
}
} else {
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setAutoRangeIncludesZero( axisAlwaysIncludeZero );
}
}
public void draw( final double x, final double[] ys,
final boolean repaintImmediately ) throws Exception {
if ( !isInitialized ) {
init( ys.length );
}
int count = dataset.getItemCount( 0 );
int skip = 50;
boolean repaint = repaintImmediately && ( count < skip || count % skip == 0 );
for ( int i = 0; i < ys.length; i++ ) {
dataset.getSeries( i ).add( x , ys[i], repaint );
}
}
public void setSeriesName( String[] names ) {
if ( !isInitialized ) {
init( names.length );
}
for ( int i = 0; i < names.length; i++ ) {
dataset.getSeries( i ).setKey( names[i] );
}
}
public void drawAll( boolean repaintImmediately ) {
if ( !repaintImmediately ) {
try {
dataset.validateObject();
} catch ( Exception e ) {
System.err.println( "DrawAll failed: " + e.getMessage() );
}
// for( int i = 0, len = dataset.getSeriesCount(); i < len; i++ ) {
// dataset.getSeries(i).fireSeriesChanged();
// }
}
}
/**
* @param args
* @throws Exception
*/
/** public static void main( String[] args ) throws Exception {
* MultiSeriesGraph g = new MultiSeriesGraph();
* g.init( 2 );
* g.setSeriesName( new String[] { "one", "two" } );
* g.updateParams( 2f, false, false );
* g.draw( 1, new double[] { 2, 3 }, true );
* g.draw( 2, new double[] { 4, 5 }, true );
* g.draw( 3, new double[] { 6, 7 }, true );
}*/
}