SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
MultipleDatasetChart.java
1/*
2 * Class: MultipleDatasetChart
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 org.jfree.chart.JFreeChart;
28import org.jfree.chart.ChartPanel;
29import org.jfree.chart.ChartFactory;
30import org.jfree.chart.axis.NumberAxis;
31import org.jfree.chart.plot.XYPlot;
32import org.jfree.chart.plot.PlotOrientation;
33
34import java.util.Locale;
35import java.util.Formatter;
36import java.util.ArrayList;
37import javax.swing.JFrame;
38
48
49 protected ArrayList<SSJXYSeriesCollection> datasetList;
50 protected Axis XAxis;
51 protected Axis YAxis;
52 protected JFreeChart chart;
53 protected boolean latexDocFlag = true;
54
55 protected boolean autoRange = true;
56 protected double[] manualRange;
57
58 protected boolean grid = false;
59 protected double xstepGrid;
60 protected double ystepGrid;
61
66 super();
67
68 // create the chart...
69 chart = ChartFactory.createXYLineChart(null, // chart title
70 null, // x axis label
71 null, // y axis label
72 null, // data
73 PlotOrientation.VERTICAL, true, // include legend
74 true, // tool tips
75 false // urls
76 );
77
78 datasetList = new ArrayList<SSJXYSeriesCollection>();
79 // Initialize axis variables
80 XAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getDomainAxis(), Axis.ORIENTATION_HORIZONTAL);
81 YAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis(), Axis.ORIENTATION_VERTICAL);
82 }
83
93 public MultipleDatasetChart(String title, String XLabel, String YLabel) {
94 // create the chart...
95 chart = ChartFactory.createXYLineChart(title, // chart title
96 XLabel, // x axis label
97 YLabel, // y axis label
98 null, // data
99 PlotOrientation.VERTICAL, true, // include legend
100 true, // tool tips
101 false // urls
102 );
103
104 datasetList = new ArrayList<SSJXYSeriesCollection>();
105 // Initialize axis variables
106 XAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getDomainAxis(), Axis.ORIENTATION_HORIZONTAL);
107 YAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis(), Axis.ORIENTATION_VERTICAL);
108 }
109
115 public JFreeChart getJFreeChart() {
116 return chart;
117 }
118
124 public Axis getXAxis() {
125 return XAxis;
126 }
127
133 public Axis getYAxis() {
134 return YAxis;
135 }
136
142 public String getTitle() {
143 return chart.getTitle().getText();
144 }
145
152 public void setTitle(String title) {
153 chart.setTitle(title);
154 }
155
159 public void setAutoRange() {
160 autoRange = true;
161 double[][] temp = new double[2][datasetList.size()];
162 for (int i = 0; i < datasetList.size(); i++) {
163 temp[0][i] = (datasetList.get(i).getDomainBounds())[0];
164 temp[1][i] = (datasetList.get(i).getDomainBounds())[1];
165 }
166 XAxis.getAxis().setLowerBound(min(temp[0]));
167 XAxis.getAxis().setUpperBound(max(temp[1]));
168
169 for (int i = 0; i < datasetList.size(); i++) {
170 temp[0][i] = (datasetList.get(i).getRangeBounds())[0];
171 temp[1][i] = (datasetList.get(i).getRangeBounds())[1];
172 }
173 YAxis.getAxis().setLowerBound(min(temp[0]));
174 YAxis.getAxis().setUpperBound(max(temp[1]));
175 }
176
183 public void setManualRange(double[] axisRange) {
184 if (axisRange.length != 4)
185 throw new IllegalArgumentException("axisRange must share the format: [xmin, xmax, ymin, ymax]");
186 autoRange = false;
187 XAxis.getAxis().setLowerBound(axisRange[0]);
188 XAxis.getAxis().setUpperBound(axisRange[1]);
189 YAxis.getAxis().setLowerBound(axisRange[2]);
190 YAxis.getAxis().setUpperBound(axisRange[3]);
191 }
192
200 public int add(SSJXYSeriesCollection dataset) {
201 ((XYPlot) chart.getPlot()).setDataset(datasetList.size(), dataset.getSeriesCollection());
202 ((XYPlot) chart.getPlot()).setRenderer(datasetList.size(), dataset.getRenderer());
203 datasetList.add(dataset);
204 if (datasetList.size() == 1) {
205 XAxis.setLabelsAuto();
206 YAxis.setLabelsAuto();
207 }
208 return datasetList.size() - 1;
209 }
210
217 return datasetList.get(0);
218 }
219
226 public void set(SSJXYSeriesCollection dataset) {
227 ((XYPlot) chart.getPlot()).setDataset(dataset.getSeriesCollection());
228 ((XYPlot) chart.getPlot()).setRenderer(dataset.getRenderer());
229 datasetList.set(0, dataset);
230 }
231
238 public SSJXYSeriesCollection get(int datasetNum) {
239 return datasetList.get(datasetNum);
240 }
241
249 public void set(int datasetNum, SSJXYSeriesCollection dataset) {
250 ((XYPlot) chart.getPlot()).setDataset(datasetNum, dataset.getSeriesCollection());
251 ((XYPlot) chart.getPlot()).setRenderer(datasetNum, dataset.getRenderer());
252 datasetList.add(datasetNum, dataset);
253 }
254
260 public ArrayList<SSJXYSeriesCollection> getList() {
261 return datasetList;
262 }
263
273 public JFrame view(int width, int height) {
274 JFrame myFrame;
275 if (chart.getTitle() != null)
276 myFrame = new JFrame("MultipleDatasetChart from SSJ: " + chart.getTitle().getText());
277 else
278 myFrame = new JFrame("MultipleDatasetChart from SSJ");
279 ChartPanel chartPanel = new ChartPanel(chart);
280 chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
281 myFrame.setContentPane(chartPanel);
282 myFrame.pack();
283 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
284 myFrame.setLocationRelativeTo(null);
285 myFrame.setVisible(true);
286 return myFrame;
287 }
288
299 public void enableGrid(double xstep, double ystep) {
300 this.grid = true;
301 this.xstepGrid = xstep;
302 this.ystepGrid = ystep;
303 }
304
308 public void disableGrid() {
309 this.grid = false;
310 }
311
315
322 public String toLatex(double width, double height) {
323 double xunit, yunit;
324 double[] save = new double[4];
325
326 if (datasetList.size() == 0)
327 throw new IllegalArgumentException("Empty chart");
328
329 // Calcul des parametres d'echelle et de decalage
330 double XScale = computeXScale(XAxis.getTwinAxisPosition());
331 double YScale = computeYScale(YAxis.getTwinAxisPosition());
332
333 xunit = width / ((Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition()) * XScale)
334 - (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition()) * XScale));
335 // taille d'une unite en x et en cm dans l'objet "tikzpicture"
336 yunit = height / ((Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition()) * YScale)
337 - (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition()) * YScale));
338 // taille d'une unite en y et en cm dans l'objet "tikzpicture"
339
340 Formatter formatter = new Formatter(Locale.US);
341
342 /* Entete du document */
343 if (latexDocFlag) {
344 formatter.format("\\documentclass[12pt]{article}%n%n");
345 formatter.format("\\usepackage{tikz}%n\\usetikzlibrary{plotmarks}%n\\begin{document}%n%n");
346 }
347 if (chart.getTitle() != null)
348 formatter.format("%% PGF/TikZ picture from SSJ : %s%n", chart.getTitle().getText());
349 else
350 formatter.format("%% PGF/TikZ picture from SSJ %n");
351 formatter.format("%% XScale = %s, YScale = %s, XShift = %s, YShift = %s%n", XScale, YScale,
352 XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition());
353 formatter.format("%% Therefore, thisFileXValue = (originalSeriesXValue+XShift)*XScale%n");
354 formatter.format("%% and thisFileYValue = (originalSeriesYValue+YShift)*YScale%n%n");
355 if (chart.getTitle() != null)
356 formatter.format("\\begin{figure}%n");
357 formatter.format("\\begin{center}%n");
358 formatter.format("\\begin{tikzpicture}[x=%scm, y=%scm]%n", xunit, yunit);
359 formatter.format("\\footnotesize%n");
360 if (grid)
361 formatter.format("\\draw[color=lightgray] (%s, %s) grid[xstep = %s, ystep=%s] (%s, %s);%n",
362 (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition())
363 - XAxis.getTwinAxisPosition()) * XScale,
364 (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition())
365 - YAxis.getTwinAxisPosition()) * YScale,
366 xstepGrid * XScale, ystepGrid * YScale,
367 (Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition())
368 - XAxis.getTwinAxisPosition()) * XScale,
369 (Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition())
370 - YAxis.getTwinAxisPosition()) * YScale);
371
372 formatter.format("%s", XAxis.toLatex(XScale));
373 formatter.format("%s", YAxis.toLatex(YScale));
374
375 for (int i = 0; i < datasetList.size(); i++)
376 formatter.format("%s",
377 datasetList.get(i).toLatex(XScale, YScale, XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition(),
378 XAxis.getAxis().getLowerBound(), XAxis.getAxis().getUpperBound(), YAxis.getAxis().getLowerBound(),
379 YAxis.getAxis().getUpperBound()));
380
381 formatter.format("\\end{tikzpicture}%n");
382 formatter.format("\\end{center}%n");
383 if (chart.getTitle() != null) {
384 formatter.format("\\caption{");
385 formatter.format(chart.getTitle().getText());
386 formatter.format("}%n\\end{figure}%n");
387 }
388 if (latexDocFlag)
389 formatter.format("\\end{document}%n");
390 return formatter.toString();
391 }
392
396 public void setLatexDocFlag(boolean flag) {
397 latexDocFlag = flag;
398 }
399
400 protected double computeXScale(double position) {
401 double[] bounds = new double[2];
402 bounds[0] = XAxis.getAxis().getLowerBound();
403 bounds[1] = XAxis.getAxis().getUpperBound();
404
405 if (position < bounds[0])
406 bounds[0] = position;
407 if (position > bounds[1])
408 bounds[1] = position;
409 bounds[0] -= position;
410 bounds[1] -= position;
411 return computeScale(bounds);
412 }
413
414 protected double computeYScale(double position) {
415 double[] bounds = new double[2];
416 bounds[0] = YAxis.getAxis().getLowerBound();
417 bounds[1] = YAxis.getAxis().getUpperBound();
418
419 if (position < bounds[0])
420 bounds[0] = position;
421 if (position > bounds[1])
422 bounds[1] = position;
423 bounds[0] -= position;
424 bounds[1] -= position;
425 return computeScale(bounds);
426 }
427
428 protected double computeScale(double[] bounds) {
429 int tenPowerRatio = 0;
430 // echelle < 1 si les valeurs sont grandes
431 while (bounds[1] > 1000 || bounds[0] < -1000) {
432 bounds[1] /= 10;
433 bounds[0] /= 10;
434 tenPowerRatio++;
435 }
436 // echelle > 1 si les valeurs sont petites
437 while (bounds[1] < 100 && bounds[0] > -100) {
438 bounds[1] *= 10;
439 bounds[0] *= 10;
440 tenPowerRatio--;
441 }
442 return 1 / Math.pow(10, tenPowerRatio);
443 }
444
445 private static double max(double[] t) {
446 double aux = t[0];
447 for (int i = 1; i < t.length; i++)
448 if (t[i] > aux)
449 aux = t[i];
450 return aux;
451 }
452
453 private static double min(double[] t) {
454 double aux = t[0];
455 for (int i = 1; i < t.length; i++)
456 if (t[i] < aux)
457 aux = t[i];
458 return aux;
459 }
460}
461
Represents an axis of a chart encapsulated by an instance of XYChart.
Definition Axis.java:42
NumberAxis getAxis()
Returns the NumberAxis instance (from JFreeChart) linked with the current variable.
Definition Axis.java:98
void setManualRange(double[] axisRange)
Sets new -axis and -axis bounds, with format: axisRange = [xmin, xmax, ymin, ymax].
void enableGrid(double xstep, double ystep)
Puts grid on the background.
void setAutoRange()
Sets chart range to automatic values.
MultipleDatasetChart(String title, String XLabel, String YLabel)
Initializes a new MultipleDatasetChart instance.
ArrayList< SSJXYSeriesCollection > getList()
Returns the dataset list.
void disableGrid()
Disables the background grid.
JFrame view(int width, int height)
Displays chart on the screen using Swing.
void setTitle(String title)
Sets a title to the chart.
String getTitle()
Gets the current chart title.
MultipleDatasetChart()
Initializes a new MultipleDatasetChart.
Axis getXAxis()
Returns the chart’s domain axis ( -axis) object.
void setLatexDocFlag(boolean flag)
Same as in XYChart.
Axis getYAxis()
Returns the chart’s range axis ( -axis) object.
String toLatex(double width, double height)
Same as in XYChart.
JFreeChart getJFreeChart()
Returns the JFreeChart variable associated with this chart.
int add(SSJXYSeriesCollection dataset)
Adds a new dataset to the chart at the end of the list and returns its position.
XYDataset getSeriesCollection()
Returns the XYDataset object associated with the current object.
XYItemRenderer getRenderer()
Returns the XYItemRenderer object associated with the current object.