SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
EmpiricalSeriesCollection.java
1/*
2 * Class: EmpiricalSeriesCollection
3 * Description:
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.charts;
26
27import umontreal.ssj.stat.TallyStore;
28
29import org.jfree.data.xy.XYSeriesCollection;
30import org.jfree.data.xy.XYSeries;
31import cern.colt.list.DoubleArrayList;
32
33import java.util.Locale;
34import java.util.Formatter;
35import java.awt.Color;
36
49
50 // for the zero point: fix its x a little smaller than the first point;
51 // later when we know where x-axis begins, reset its x at the
52 // beginning of x-axis; its y is always 0.
53 private final double EMPIR_EPS = 0.015625;
54
55 private String[] marksType; // marks on points (+, x, *...)
56 private String[] dashPattern; // line dashing (solid, dotted, densely dotted, loosely dotted,
57 // dashed, densely dashed, loosely dashed)
58
59 private double setZeroPoint(double x1) {
60 // set temporary 0-point with x EPS smaller than x1
61 return x1 - EMPIR_EPS * Math.abs(x1);
62 }
63
68 renderer = new EmpiricalRenderer();
69 seriesCollection = new XYSeriesCollection();
70 }
71
79 public EmpiricalSeriesCollection(double[]... data) {
80 seriesCollection = new XYSeriesCollection();
81 renderer = new EmpiricalRenderer();
82 XYSeriesCollection tempSeriesCollection = (XYSeriesCollection) seriesCollection;
83 for (int j = 0; j < data.length; j++) {
84 XYSeries serie = new XYSeries(" ");
85 serie.add(setZeroPoint(data[j][0]), 0); // correct x-value of 0-point will be set later
86 for (int k = 0; k < data[j].length; k++)
87 serie.add(data[j][k], (double) (k + 1) / data[j].length);
88 tempSeriesCollection.addSeries(serie);
89 }
90
91 /* set default colors */
92 for (int i = 0; i < tempSeriesCollection.getSeriesCount(); i++) {
93 renderer.setSeriesPaint(i, getDefaultColor(i));
94 }
95
96 /* set default plot style */
97 marksType = new String[tempSeriesCollection.getSeriesCount()];
98 dashPattern = new String[tempSeriesCollection.getSeriesCount()];
99 for (int i = 0; i < tempSeriesCollection.getSeriesCount(); i++) {
100 marksType[i] = "*";
101 dashPattern[i] = "solid";
102 }
103 }
104
114 public EmpiricalSeriesCollection(double[] data, int numPoints) {
115 seriesCollection = new XYSeriesCollection();
116 renderer = new EmpiricalRenderer();
117 XYSeriesCollection tempSeriesCollection = (XYSeriesCollection) seriesCollection;
118 XYSeries serie = new XYSeries(" ");
119 serie.add(setZeroPoint(data[0]), 0); // correct x-value of zero point will be set later
120 for (int k = 0; k < numPoints; k++)
121 serie.add(data[k], (double) (k + 1) / numPoints);
122 tempSeriesCollection.addSeries(serie);
123
124 // set default colors
125 renderer.setSeriesPaint(0, getDefaultColor(0));
126
127 // set default plot style
128 marksType = new String[1];
129 dashPattern = new String[1];
130 marksType[0] = "*";
131 dashPattern[0] = "solid";
132 }
133
144 public EmpiricalSeriesCollection(DoubleArrayList... data) {
145 seriesCollection = new XYSeriesCollection();
146 renderer = new EmpiricalRenderer();
147 XYSeriesCollection tempSeriesCollection = (XYSeriesCollection) seriesCollection;
148
149 for (int j = 0; j < data.length; j++) {
150 XYSeries serie = new XYSeries(" ");
151 serie.add(setZeroPoint(data[j].get(0)), 0); // correct x-value of zero point will be set later
152 for (int k = 0; k < data[j].size(); k++)
153 serie.add(data[j].get(k), (double) (k + 1) / data[j].size());
154 tempSeriesCollection.addSeries(serie);
155 }
156
157 /* set default colors */
158 for (int i = 0; i < tempSeriesCollection.getSeriesCount(); i++) {
159 renderer.setSeriesPaint(i, getDefaultColor(i));
160 }
161
162 /* set default plot style */
163 marksType = new String[tempSeriesCollection.getSeriesCount()];
164 dashPattern = new String[tempSeriesCollection.getSeriesCount()];
165 for (int i = 0; i < tempSeriesCollection.getSeriesCount(); i++) {
166 marksType[i] = "*";
167 dashPattern[i] = "solid";
168 }
169 }
170
180 seriesCollection = new XYSeriesCollection();
181 renderer = new EmpiricalRenderer();
182 XYSeriesCollection tempSeriesCollection = (XYSeriesCollection) seriesCollection;
183
184 for (int j = 0; j < tallies.length; j++) {
185 TallyStore temp = tallies[j];
186 temp.quickSort();
187 double[] array = temp.getArray();
188 XYSeries serie = new XYSeries(" ");
189 serie.add(setZeroPoint(array[0]), 0); // correct x-value of zero point will be set later
190 for (int k = 0; k < tallies[j].numberObs(); k++)
191 serie.add(array[k], (double) (k + 1) / tallies[j].numberObs());
192 tempSeriesCollection.addSeries(serie);
193 }
194
195 /* set default colors */
196 for (int i = 0; i < tempSeriesCollection.getSeriesCount(); i++) {
197 renderer.setSeriesPaint(i, getDefaultColor(i));
198 }
199
200 /* set default plot style */
201 marksType = new String[tempSeriesCollection.getSeriesCount()];
202 dashPattern = new String[tempSeriesCollection.getSeriesCount()];
203 for (int i = 0; i < tempSeriesCollection.getSeriesCount(); i++) {
204 marksType[i] = "*";
205 dashPattern[i] = "solid";
206 }
207 }
208
216 public EmpiricalSeriesCollection(XYSeriesCollection data) {
217 renderer = new EmpiricalRenderer();
218 seriesCollection = data;
219
220 /* set default colors */
221 for (int i = 0; i < data.getSeriesCount(); i++) {
222 renderer.setSeriesPaint(i, getDefaultColor(i));
223 XYSeries ser = data.getSeries(i);
224 ser.add(setZeroPoint(ser.getX(0).doubleValue()), 0); // add zero point; will set its correct x-value later
225 }
226
227 /* set default plot style */
228 marksType = new String[data.getSeriesCount()];
229 dashPattern = new String[data.getSeriesCount()];
230 for (int i = 0; i < data.getSeriesCount(); i++) {
231 marksType[i] = "*";
232 dashPattern[i] = "solid";
233 }
234 }
235
239
247 public int add(double[] observationSet) {
248 return add(observationSet, observationSet.length);
249 }
250
260 public int add(double[] observationSet, int numPoints) {
261 XYSeriesCollection tempSeriesCollection = (XYSeriesCollection) seriesCollection;
262
263 XYSeries serie = new XYSeries(" ");
264 serie.add(setZeroPoint(observationSet[0]), 0); // coordinates of first point will be reset later
265 for (int k = 0; k < numPoints; k++)
266 serie.add(observationSet[k], (double) (k + 1) / numPoints);
267 tempSeriesCollection.addSeries(serie);
268
269 // color
270 int j = seriesCollection.getSeriesCount() - 1;
271 renderer.setSeriesPaint(j, getDefaultColor(j));
272
273 String[] newMarksType = new String[seriesCollection.getSeriesCount()];
274 String[] newDashPattern = new String[seriesCollection.getSeriesCount()];
275 for (j = 0; j < seriesCollection.getSeriesCount() - 1; j++) {
276 newMarksType[j] = marksType[j];
277 newDashPattern[j] = dashPattern[j];
278 }
279
280 newMarksType[j] = "*";
281 newDashPattern[j] = "solid";
282 marksType = newMarksType;
283 dashPattern = newDashPattern;
284
285 return seriesCollection.getSeriesCount() - 1;
286 }
287
295 public int add(DoubleArrayList observationSet) {
296 return add(observationSet.elements(), observationSet.size());
297 }
298
306 public int add(TallyStore tally) {
307 tally.quickSort();
308 return add(tally.getArray(), tally.numberObs());
309 }
310
314
318
325 public String getMarksType(int series) {
326 return marksType[series];
327 }
328
338 public void setMarksType(int series, String marksType) {
339 this.marksType[series] = marksType;
340 }
341
348 public String getDashPattern(int series) {
349 return dashPattern[series];
350 }
351
360 public void setDashPattern(int series, String dashPattern) {
361 this.dashPattern[series] = dashPattern;
362 }
363
364 public String toLatex(double XScale, double YScale, double XShift, double YShift, double xmin, double xmax,
365 double ymin, double ymax) {
366
367 // Calcule les bornes reelles du graphique, en prenant en compte la position des
368 // axes
369 xmin = Math.min(XShift, xmin);
370 xmax = Math.max(XShift, xmax);
371 ymin = Math.min(YShift, ymin);
372 ymax = Math.max(YShift, ymax);
373
374 XYSeriesCollection tempSeriesCollection = (XYSeriesCollection) seriesCollection;
375 Formatter formatter = new Formatter(Locale.US);
376 for (int i = tempSeriesCollection.getSeriesCount() - 1; i >= 0; i--) {
377
378 Color color = (Color) renderer.getSeriesPaint(i);
379 String colorString = detectXColorClassic(color);
380 if (colorString == null) {
381 colorString = "color" + i;
382 formatter.format("\\definecolor{%s}{rgb}{%.2f, %.2f, %.2f}%n", colorString, color.getRed() / 255.0,
383 color.getGreen() / 255.0, color.getBlue() / 255.0);
384 }
385
386 double currentX, nextX;
387 for (int j = 0; j < tempSeriesCollection.getItemCount(i); j++) {
388 currentX = tempSeriesCollection.getXValue(i, j);
389 if (j == tempSeriesCollection.getItemCount(i) - 1)
390 nextX = xmax;
391 else
392 nextX = Math.min(tempSeriesCollection.getXValue(i, j + 1), xmax);
393
394 if ((currentX >= xmin && currentX <= xmax)) {
395 formatter.format("\\draw [color=%s] plot[mark=%s] (%.4f, %.4f) --plot[style=%s] (%.4f, %.4f); %%%n",
396 colorString, marksType[i], (currentX - XShift) * XScale,
397 (tempSeriesCollection.getYValue(i, j) - YShift) * YScale, dashPattern[i],
398 (nextX - XShift) * XScale, (tempSeriesCollection.getYValue(i, j) - YShift) * YScale);
399 }
400 }
401 }
402 return formatter.toString();
403 }
404}
405
A renderer that draws horizontal lines between points and/or draws shapes at each data point to provi...
EmpiricalSeriesCollection()
Creates a new EmpiricalSeriesCollection instance with empty dataset.
String getMarksType(int series)
Returns the mark type associated with the series-th data series.
EmpiricalSeriesCollection(double[] data, int numPoints)
Creates a new EmpiricalSeriesCollection instance with default parameters and a given series data.
int add(DoubleArrayList observationSet)
Adds a data series into the series collection.
EmpiricalSeriesCollection(DoubleArrayList... data)
Creates a new EmpiricalSeriesCollection instance with default parameters and given data.
String toLatex(double XScale, double YScale, double XShift, double YShift, double xmin, double xmax, double ymin, double ymax)
Formats and returns a string containing a LaTeX-compatible source code which represents this data ser...
String getDashPattern(int series)
Returns the dash pattern associated with the series-th data series.
int add(double[] observationSet)
Adds a data series into the series collection.
int add(double[] observationSet, int numPoints)
Adds a data series into the series collection.
EmpiricalSeriesCollection(double[]... data)
Creates a new EmpiricalSeriesCollection instance with default parameters and given data series.
void setMarksType(int series, String marksType)
Adds marks on points to a data series.
int add(TallyStore tally)
Adds a data series into the series collection.
void setDashPattern(int series, String dashPattern)
Selects dash pattern for a data series.
EmpiricalSeriesCollection(TallyStore... tallies)
Creates a new EmpiricalSeriesCollection instance with default parameters and given data.
EmpiricalSeriesCollection(XYSeriesCollection data)
Creates a new EmpiricalSeriesCollection instance with default parameters and given data series.
static Color getDefaultColor(int index)
Gives the default color associated with a series.
static String detectXColorClassic(Color color)
Converts a java Color object into a friendly and readable LaTeX/xcolor string.
This class is a variant of Tally for which the individual observations are stored in a list implement...
void quickSort()
Sorts the elements of this probe using the quicksort from Colt.
double[] getArray()
Returns the observations stored in this probe.
int numberObs()
Returns the number of observations given to this probe since its last initialization.
Definition Tally.java:143