-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiSeriesGraph.java
More file actions
172 lines (150 loc) · 6.06 KB
/
MultiSeriesGraph.java
File metadata and controls
172 lines (150 loc) · 6.06 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.data.xy.*;
class MultiSeriesGraph {
/*@ specification MultiSeriesGraph {
double x;
alias (double) ys;
void init_ready, drawing_ready, paintAll, done, updated;
boolean repaintImmediately, axisAlwaysIncludeZero, showSeparateAxis;
boolean autoSort, allowDuplicates;
float lineThickness;
//NB! seriesNames if defined must match ys
String[] seriesNames;
String domainName;
//----default settings
repaintImmediately = true;
axisAlwaysIncludeZero = true;
showSeparateAxis = false;
lineThickness = 1;
autoSort = false;
allowDuplicates = true;
domainName = "";
//change settings
ys.length, lineThickness, axisAlwaysIncludeZero,
showSeparateAxis, autoSort, allowDuplicates -> init_ready {init};
//setup axis lables
init_ready, domainName, seriesNames, showSeparateAxis -> updated {setSeriesName};
//add coordinates
init_ready, x, ys, repaintImmediately -> drawing_ready, (Exception) {draw};
//paint all at once
paintAll, repaintImmediately -> done {drawAll};
}@*/
private JFreeChart chart;
private ChartFrame frame;
private XYPlot plot;
private List<XYSeries> series = new ArrayList<XYSeries>();
private void initChart() {
chart = ChartFactory.createXYLineChart( null, null, null,
null, PlotOrientation.VERTICAL,
true, //legend
true, //tooltip
false );//url
plot = chart.getXYPlot();
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();
}
} );
}
public void init( int length, float lineThickness,
boolean axisAlwaysIncludeZero, boolean showSeparateAxis,
boolean autoSort, boolean allowDuplicates ) {
initChart();
if (showSeparateAxis) {
for (int i = 0; i < length; i++) {
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries ser = new XYSeries("" + i, autoSort, allowDuplicates);
dataset.addSeries(ser);
series.add( ser );
plot.setDataset(i, dataset);
}
} else {
XYSeriesCollection dataset = new XYSeriesCollection();
for (int i = 0; i < length; i++) {
XYSeries ser = new XYSeries("" + i, autoSort, allowDuplicates);
dataset.addSeries(ser);
series.add( ser );
}
plot.setDataset(0, dataset);
}
Stroke stroke = new BasicStroke( lineThickness );
NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
domainAxis.setAutoRangeIncludesZero( axisAlwaysIncludeZero );
if ( showSeparateAxis && length > 1 ) {
chart.removeLegend();
for ( int i = 0, len = series.size(); i < len; i++ ) {
XYSeries ser = series.get( i );
Paint color = plot.getDrawingSupplier().getNextPaint();
NumberAxis axis = new NumberAxis( ser.getKey().toString() );
axis.setAutoRangeIncludesZero( axisAlwaysIncludeZero );
axis.setLabelPaint( color );
axis.setTickLabelPaint( color );
if (i < len / 2) {
plot.setRangeAxisLocation(i, AxisLocation.BOTTOM_OR_LEFT);
}
plot.setRangeAxis( i, axis );
plot.mapDatasetToRangeAxis( i, i);
XYLineAndShapeRenderer rend = new XYLineAndShapeRenderer(true, false);
rend.setSeriesStroke( 0, stroke );
rend.setSeriesPaint( 0, color );
plot.setRenderer( i, rend );
}
} else {
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setAutoRangeIncludesZero( axisAlwaysIncludeZero );
plot.getRenderer().setStroke( stroke );
}
frame.pack();
frame.setVisible( true );
}
public void draw( final double x, final double[] ys,
final boolean repaintImmediately ) throws Exception {
for ( int i = 0; i < ys.length; i++ ) {
series.get( i ).add( x, ys[i], repaintImmediately );
}
}
public void setSeriesName( String domain, String[] names, boolean showSeparateAxis ) {
if(domain != null && domain.length() > 0) {
NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
domainAxis.setLabel( domain );
}
for (int i = 0; i < names.length; i++) {
if(showSeparateAxis) {
plot.getRangeAxis(i).setLabel(names[i]);
}
series.get( i ).setKey(names[i]);
}
}
public void drawAll( boolean repaintImmediately ) {
if ( !repaintImmediately ) {
for( XYSeries ser : series ){
ser.fireSeriesChanged();
}
}
}
/**
* @param args
* @throws Exception
*/
public static void main( String[] args ) throws Exception {
MultiSeriesGraph g = new MultiSeriesGraph();
String[] range = new String[] { "one", "two", "333" };
boolean showSeparateAxis = false;
g.init( range.length, 4f, false, showSeparateAxis, false, true );
g.setSeriesName( "domain", range, showSeparateAxis );
g.draw( 1, new double[] { 0.2, 6, 60 }, true );
g.draw( 2, new double[] { 0.3, 8, 40 }, true );
g.draw( 3, new double[] { 0.6, 7, 12 }, true );
}
}