1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.jstats.charts.xy;
11
12 import java.awt.Color;
13 import java.awt.Container;
14 import java.awt.GradientPaint;
15 import java.rmi.RemoteException;
16
17 import nl.tudelft.simulation.event.EventProducerInterface;
18 import nl.tudelft.simulation.event.EventType;
19 import nl.tudelft.simulation.jstats.Swingable;
20 import nl.tudelft.simulation.jstats.statistics.Persistent;
21
22 import org.jfree.chart.ChartFactory;
23 import org.jfree.chart.ChartPanel;
24 import org.jfree.chart.JFreeChart;
25 import org.jfree.chart.axis.LogarithmicAxis;
26 import org.jfree.chart.plot.PlotOrientation;
27
28 /***
29 * The xyChart specifies the xyChart in DSOL.
30 * <p>
31 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
32 * University of Technology </a>, the Netherlands. <br>
33 * See for project information <a
34 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
35 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
36 * License (GPL) </a>, no warranty <br>
37 *
38 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
39 * Jacobs </a>
40 * @version 1.2 Apr 26, 2004
41 * @since 1.4
42 */
43 public class XYChart implements Swingable
44 {
45 /*** x-axis is linear y-axis is linear */
46 public static final short XLINEAR_YLINEAR = 0;
47
48 /*** x-axis is linear y-axis is logarithmic */
49 public static final short XLINEAR_YLOGARITHMIC = 1;
50
51 /*** x-axis is logarithmic y-axis is linear */
52 public static final short XLOGARITHMIC_YLINEAR = 2;
53
54 /*** x-axis is logarithmic y-axis is logarithmic */
55 public static final short XLOGARITHMIC_YLOGARITHMIC = 3;
56
57 /*** LABEL_X_AXIS is the label on the X-axis */
58 public static final String LABEL_X_AXIS = "X";
59
60 /*** LABEL_Y_AXIS is the label on the Y-axis */
61 public static final String LABEL_Y_AXIS = "value";
62
63 /*** chart refers to the chart */
64 protected JFreeChart chart = null;
65
66 /*** dataset refers to the dataset */
67 protected XYDataset dataset = new XYDataset();
68
69 /*** the axis type of the chart */
70 protected short axisType = XLINEAR_YLINEAR;
71
72 /***
73 * constructs a new XYChart
74 *
75 * @param title the title
76 */
77 public XYChart(final String title)
78 {
79 this(title, null, null);
80 }
81
82 /***
83 * constructs a new XYChart
84 *
85 * @param title the title
86 * @param axisType the axisType
87 */
88 public XYChart(final String title, final short axisType)
89 {
90 this(title, null, null, axisType);
91 }
92
93 /***
94 * constructs a new XYChart
95 *
96 * @param title tht title
97 * @param domain the domain
98 */
99 public XYChart(final String title, final double[] domain)
100 {
101 this(title, domain, null);
102 }
103
104 /***
105 * constructs a new XYChart
106 *
107 * @param title tht title
108 * @param domain the domain
109 * @param axisType the axisType to use.
110 */
111 public XYChart(final String title, final double[] domain,
112 final short axisType)
113 {
114 this(title, domain, null, axisType);
115 }
116
117 /***
118 * constructs a new XYChart
119 *
120 * @param title the title
121 * @param domain the domain
122 * @param range the range
123 */
124 public XYChart(final String title, final double[] domain,
125 final double[] range)
126 {
127 this(title, domain, range, XYChart.XLINEAR_YLINEAR);
128 }
129
130 /***
131 * constructs a new XYChart
132 *
133 * @param title the title
134 * @param domain the domain
135 * @param range the range
136 * @param axisType the type of the axsis
137 */
138 public XYChart(final String title, final double[] domain,
139 final double[] range, final short axisType)
140 {
141 super();
142 this.chart = ChartFactory.createXYLineChart(title, LABEL_X_AXIS,
143 LABEL_Y_AXIS, this.dataset, PlotOrientation.VERTICAL, true,
144 true, true);
145 this.chart.setBackgroundPaint(new GradientPaint(0.0F, 0.0F,
146 Color.white, 1000F, 0.0F, Color.blue));
147 this.axisType = axisType;
148 switch (this.axisType)
149 {
150 case XYChart.XLINEAR_YLOGARITHMIC :
151 this.chart.getXYPlot().setRangeAxis(
152 new LogarithmicAxis(XYChart.LABEL_Y_AXIS));
153 break;
154 case XYChart.XLOGARITHMIC_YLINEAR :
155 this.chart.getXYPlot().setDomainAxis(
156 new LogarithmicAxis(XYChart.LABEL_X_AXIS));
157 break;
158 case XYChart.XLOGARITHMIC_YLOGARITHMIC :
159 this.chart.getXYPlot().setDomainAxis(
160 new LogarithmicAxis(XYChart.LABEL_X_AXIS));
161 this.chart.getXYPlot().setRangeAxis(
162 new LogarithmicAxis(XYChart.LABEL_Y_AXIS));
163 break;
164 default :
165 break;
166 }
167
168 if (domain != null)
169 {
170 this.chart.getXYPlot().getDomainAxis().setAutoRange(false);
171 this.chart.getXYPlot().getDomainAxis().setLowerBound(domain[0]);
172 this.chart.getXYPlot().getDomainAxis().setUpperBound(domain[1]);
173 } else
174 {
175 this.chart.getXYPlot().getDomainAxis().setAutoRange(true);
176 }
177 if (range != null)
178 {
179 this.chart.getXYPlot().getRangeAxis().setAutoRange(false);
180 this.chart.getXYPlot().getRangeAxis().setLowerBound(range[0]);
181 this.chart.getXYPlot().getRangeAxis().setUpperBound(range[1]);
182 } else
183 {
184 this.chart.getXYPlot().getRangeAxis().setAutoRange(true);
185 }
186 this.dataset.addChangeListener(this.chart.getXYPlot());
187 this.getChart().fireChartChanged();
188 }
189
190 /***
191 * adds a tally to the xyChart
192 *
193 * @param persistent the persistent
194 */
195 public void add(final Persistent persistent)
196 {
197 XYSeries set = new XYSeries(persistent.getDescription(), this.axisType);
198 persistent.addListener(set, Persistent.VALUE_EVENT, false);
199 this.getDataset().addSeries(set);
200 }
201
202 /***
203 * adds an eventProducer to the xyChart
204 *
205 * @param description the description of the eventProducer
206 * @param source the souce
207 * @param eventType the event
208 * @throws RemoteException on network failure
209 */
210 public void add(final String description,
211 final EventProducerInterface source, final EventType eventType)
212 throws RemoteException
213 {
214 XYSeries set = new XYSeries(description, this.axisType);
215 source.addListener(set, eventType,
216 EventProducerInterface.FIRST_POSITION, false);
217 this.getDataset().addSeries(set);
218 }
219
220 /***
221 * returns the chart
222 *
223 * @return JFreeChart
224 */
225 public JFreeChart getChart()
226 {
227 return this.chart;
228 }
229
230 /***
231 * returns the chartPanel of this xyChart
232 *
233 * @return ChartPanel
234 */
235 public Container getSwingPanel()
236 {
237 ChartPanel result = new ChartPanel(this.chart);
238 result.setMouseZoomable(true, false);
239 return result;
240 }
241
242 /***
243 * returns the dataset of a xyChart
244 *
245 * @return HistogramSeries
246 */
247 public XYDataset getDataset()
248 {
249 return this.dataset;
250 }
251 }