SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
EmpiricalChart.java
1/*
2 * Class: EmpiricalChart
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.chart.ChartPanel;
30import org.jfree.chart.ChartFactory;
31import org.jfree.chart.axis.NumberAxis;
32import org.jfree.chart.plot.XYPlot;
33import org.jfree.chart.plot.PlotOrientation;
34import org.jfree.data.xy.XYSeriesCollection;
35import org.jfree.data.xy.XYSeries;
36
37import cern.colt.list.DoubleArrayList;
38
39import java.util.Locale;
40import java.util.Formatter;
41import javax.swing.JFrame;
42
50public class EmpiricalChart extends XYChart {
51
52 protected void init(String title, String XLabel, String YLabel) {
53 // create the chart...
54 chart = ChartFactory.createXYLineChart(title, // chart title
55 XLabel, // x axis label
56 YLabel, // y axis label
57 dataset.getSeriesCollection(), // data
58 PlotOrientation.VERTICAL, true, // include legend
59 true, // tool tips
60 false // urls
61 );
62 ((XYPlot) chart.getPlot()).setRenderer(dataset.getRenderer());
63 // Initialize axis variables
64 XAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getDomainAxis(), Axis.ORIENTATION_HORIZONTAL);
65 YAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis(), Axis.ORIENTATION_VERTICAL);
66 fixZeroPoint();
67 }
68
69 private void fixZeroPoint() {
70 // reset the first point (x0, 0) with x0 at the beginning of x-axis
71 double xmin = Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition());
72 XYSeriesCollection col = (XYSeriesCollection) dataset.getSeriesCollection();
73 for (int i = 0; i < col.getSeriesCount(); i++) {
74 XYSeries ser = col.getSeries(i);
75 ser.remove(0); // remove temporary 0-point
76 ser.add(xmin, 0); // replace
77 }
78 }
79
83 public EmpiricalChart() {
84 super();
85 dataset = new EmpiricalSeriesCollection();
86 init(null, null, null);
87 }
88
104 public EmpiricalChart(String title, String XLabel, String YLabel, double[]... data) {
105 super();
106 dataset = new EmpiricalSeriesCollection(data);
107 init(title, XLabel, YLabel);
108 }
109
124 public EmpiricalChart(String title, String XLabel, String YLabel, double[] data, int numPoints) {
125 super();
126 dataset = new EmpiricalSeriesCollection(data, numPoints);
127 init(title, XLabel, YLabel);
128 }
129
141 public EmpiricalChart(String title, String XLabel, String YLabel, DoubleArrayList... data) {
142 super();
143 dataset = new EmpiricalSeriesCollection(data);
144 init(title, XLabel, YLabel);
145 }
146
159 public EmpiricalChart(String title, String XLabel, String YLabel, TallyStore... tallies) {
160 super();
161 dataset = new EmpiricalSeriesCollection(tallies);
162 init(title, XLabel, YLabel);
163 }
164
176 public EmpiricalChart(String title, String XLabel, String YLabel, XYSeriesCollection data) {
177 super();
178 dataset = new EmpiricalSeriesCollection(data);
179 init(title, XLabel, YLabel);
180 }
181
190
197 this.dataset = dataset;
198 }
199
205 public void setTicksSynchro(int s) {
206 XYSeriesCollection seriesCollection = (XYSeriesCollection) this.dataset.getSeriesCollection();
207 double[] values = new double[seriesCollection.getItemCount(s)];
208
209 for (int i = 0; i < seriesCollection.getItemCount(s); i++)
210 values[i] = seriesCollection.getXValue(s, i);
211
212 XAxis.setLabels(values);
213 }
214
225 public JFrame view(int width, int height) {
226 JFrame myFrame;
227 if (chart.getTitle() != null)
228 myFrame = new JFrame("EmpiricalChart from SSJ : " + chart.getTitle().getText());
229 else
230 myFrame = new JFrame("EmpiricalChart from SSJ");
231 ChartPanel chartPanel = new ChartPanel(chart);
232 chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
233 myFrame.setContentPane(chartPanel);
234 myFrame.pack();
235 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
236 myFrame.setLocationRelativeTo(null);
237 myFrame.setVisible(true);
238 return myFrame;
239 }
240
244 public String toLatex(double width, double height) {
245 double xunit, yunit;
246
247 if (dataset.getSeriesCollection().getSeriesCount() == 0)
248 throw new IllegalArgumentException("Empty chart");
249
250 // Calcul des parametres d'echelle et de decalage
251 double XScale = computeXScale(XAxis.getTwinAxisPosition());
252 double YScale = computeYScale(YAxis.getTwinAxisPosition());
253
254 xunit = width / ((Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition()) * XScale)
255 - (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition()) * XScale));
256 // taille d'une unite en x et en cm dans l'objet "tikzpicture"
257 yunit = height / ((Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition()) * YScale)
258 - (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition()) * YScale));
259 // taille d'une unite en y et en cm dans l'objet "tikzpicture"
260
261 Formatter formatter = new Formatter(Locale.US);
262
263 /* Entete du document */
264 if (latexDocFlag) {
265 formatter.format("\\documentclass[12pt]{article}%n%n");
266 formatter.format("\\usepackage{tikz}%n\\usetikzlibrary{plotmarks}%n\\begin{document}%n%n");
267 }
268 if (chart.getTitle() != null)
269 formatter.format("%% PGF/TikZ picture from SSJ : %s%n", chart.getTitle().getText());
270 else
271 formatter.format("%% PGF/TikZ picture from SSJ %n");
272 formatter.format("%% XScale = %s, YScale = %s, XShift = %s, YShift = %s%n", XScale, YScale,
273 XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition());
274 formatter.format("%% Therefore, thisFileXValue = (originalSeriesXValue+XShift)*XScale%n");
275 formatter.format("%% and thisFileYValue = (originalSeriesYValue+YShift)*YScale%n%n");
276 if (chart.getTitle() != null)
277 formatter.format("\\begin{figure}%n");
278 formatter.format("\\begin{center}%n");
279 formatter.format("\\begin{tikzpicture}[x=%scm, y=%scm]%n", xunit, yunit);
280 formatter.format("\\footnotesize%n");
281 if (grid)
282 formatter.format("\\draw[color=lightgray] (%s, %s) grid[xstep = %s, ystep=%s] (%s, %s);%n",
283 (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition())
284 - XAxis.getTwinAxisPosition()) * XScale,
285 (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition())
286 - YAxis.getTwinAxisPosition()) * YScale,
287 xstepGrid * XScale, ystepGrid * YScale,
288 (Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition())
289 - XAxis.getTwinAxisPosition()) * XScale,
290 (Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition())
291 - YAxis.getTwinAxisPosition()) * YScale);
292 setTick0Flags();
293 formatter.format("%s", XAxis.toLatex(XScale));
294 formatter.format("%s", YAxis.toLatex(YScale));
295
296 formatter.format("%s",
297 dataset.toLatex(XScale, YScale, XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition(),
298 XAxis.getAxis().getLowerBound(), XAxis.getAxis().getUpperBound(), YAxis.getAxis().getLowerBound(),
299 YAxis.getAxis().getUpperBound()));
300
301 formatter.format("\\end{tikzpicture}%n");
302 formatter.format("\\end{center}%n");
303 if (chart.getTitle() != null) {
304 formatter.format("\\caption{");
305 formatter.format(chart.getTitle().getText());
306 formatter.format("}%n\\end{figure}%n");
307 }
308 if (latexDocFlag)
309 formatter.format("\\end{document}%n");
310 formatter.close(); // Ok ????????
311 return formatter.toString();
312 }
313
314}
315
Represents an axis of a chart encapsulated by an instance of XYChart.
Definition Axis.java:42
EmpiricalChart(String title, String XLabel, String YLabel, double[] data, int numPoints)
Initializes a new EmpiricalChart instance with a set of points data.
EmpiricalChart(String title, String XLabel, String YLabel, TallyStore... tallies)
Initializes a new EmpiricalChart instance with data arrays contained in each umontreal....
JFrame view(int width, int height)
Displays chart on the screen using Swing.
EmpiricalChart()
Initializes a new EmpiricalChart instance with an empty data set.
EmpiricalChart(String title, String XLabel, String YLabel, double[]... data)
Initializes a new EmpiricalChart instance with data data.
EmpiricalChart(String title, String XLabel, String YLabel, DoubleArrayList... data)
Similar to the above constructor, but with DoubleArrayList.
void setSeriesCollection(EmpiricalSeriesCollection dataset)
Links a new dataset to the current chart.
String toLatex(double width, double height)
Exports the chart to a LaTeX source code using PGF/TikZ.
void setTicksSynchro(int s)
Synchronizes -axis ticks to the -th series.
EmpiricalSeriesCollection getSeriesCollection()
Returns the chart’s dataset.
EmpiricalChart(String title, String XLabel, String YLabel, XYSeriesCollection data)
Initializes a new EmpiricalChart instance with data data.
This class provides tools to create charts from data in a simple way.
Definition XYChart.java:56
This class is a variant of Tally for which the individual observations are stored in a list implement...