SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
XYLineChart.java
1/*
2 * Class: XYLineChart
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.*;
28import org.jfree.chart.axis.*;
29import org.jfree.chart.plot.*;
30import org.jfree.chart.renderer.xy.*;
31import org.jfree.data.xy.*;
32// import org.jfree.data.category.*;
33// import org.jfree.chart.renderer.category.*;
34import java.util.Locale;
35import java.util.Formatter;
36import java.lang.Math;
37import java.awt.*;
38import java.awt.geom.*;
39import cern.colt.list.DoubleArrayList;
40import javax.swing.JFrame;
41
48public class XYLineChart extends XYChart {
49
50 protected void init(String title, String XLabel, String YLabel) {
51 // create the chart...
52 chart = ChartFactory.createXYLineChart(title, // chart title
53 XLabel, // x axis label
54 YLabel, // y axis label
55 dataset.getSeriesCollection(), // data
56 PlotOrientation.VERTICAL, false, // include legend
57 true, // tooltips
58 false // urls
59 );
60
61 if (null != title) {
62 if (title.startsWith("cdf") || title.startsWith("prob") || title.startsWith("density"))
63 setprobFlag(true);
64 }
65
66 ((XYPlot) chart.getPlot()).setRenderer(dataset.getRenderer());
67 // Initialize axis variables
68 initAxis();
69 }
70
71 protected void initAxis() {
72 XAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getDomainAxis(), Axis.ORIENTATION_HORIZONTAL);
73 YAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis(), Axis.ORIENTATION_VERTICAL);
74 setAutoRange(true, true);
75 }
76
80 public XYLineChart() {
81 super();
82 dataset = new XYListSeriesCollection();
83 init(null, null, null);
84 }
85
116 public XYLineChart(String title, String XLabel, String YLabel, double[][]... data) {
117 super();
118 dataset = new XYListSeriesCollection(data);
119 init(title, XLabel, YLabel);
120 }
121
141 public XYLineChart(String title, String XLabel, String YLabel, double[][] data, int numPoints) {
142 super();
143 dataset = new XYListSeriesCollection(data, numPoints);
144 init(title, XLabel, YLabel);
145 }
146
164 public XYLineChart(String title, String XLabel, String YLabel, double[][] data, int x, int y) {
165 super();
166 int len = data[0].length;
167 double[][] proj = new double[2][len];
168 for (int i = 0; i < len; i++) {
169 proj[0][i] = data[x][i];
170 proj[1][i] = data[y][i];
171 }
172 dataset = new XYListSeriesCollection(proj);
173 init(title, XLabel, YLabel);
174 }
175
187 public XYLineChart(String title, String XLabel, String YLabel, DoubleArrayList... data) {
188 super();
189 dataset = new XYListSeriesCollection(data);
190 init(title, XLabel, YLabel);
191 }
192
204 public XYLineChart(String title, String XLabel, String YLabel, XYSeriesCollection data) {
205 super();
206 dataset = new XYListSeriesCollection(data);
207 init(title, XLabel, YLabel);
208 }
209
223 public int add(double[] x, double[] y, String name, String plotStyle) {
224 int seriesIndex = add(x, y);
225 getSeriesCollection().setName(seriesIndex, name);
226 getSeriesCollection().setPlotStyle(seriesIndex, plotStyle);
227 return seriesIndex;
228 }
229
240 public int add(double[] x, double[] y) {
241 int seriesIndex = getSeriesCollection().add(x, y);
242 initAxis();
243 return seriesIndex;
244 }
245
258 public int add(double[] x, double[] y, int numPoints) {
259 int seriesIndex = getSeriesCollection().add(x, y, numPoints);
260 initAxis();
261 return seriesIndex;
262 }
263
275 public int add(double[][] data) {
276 int seriesIndex = getSeriesCollection().add(data);
277 initAxis();
278 return seriesIndex;
279 }
280
295 public int add(double[][] data, int numPoints) {
296 int seriesIndex = getSeriesCollection().add(data, numPoints);
297 initAxis();
298 return seriesIndex;
299 }
300
309
316 this.dataset = dataset;
317 }
318
324 public void setTicksSynchro(int s) {
325 XYSeriesCollection seriesCollection = (XYSeriesCollection) this.dataset.getSeriesCollection();
326 double[] values = new double[seriesCollection.getItemCount(s)];
327
328 for (int i = 0; i < seriesCollection.getItemCount(s); i++)
329 values[i] = seriesCollection.getXValue(s, i);
330
331 XAxis.setLabels(values);
332 }
333
346 public JFrame view(int width, int height) {
347 JFrame myFrame;
348 if (chart.getTitle() != null)
349 myFrame = new JFrame("XYLineChart from SSJ: " + chart.getTitle().getText());
350 else
351 myFrame = new JFrame("XYLineChart from SSJ");
352 ChartPanel chartPanel = new ChartPanel(chart);
353 chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
354 myFrame.setContentPane(chartPanel);
355 myFrame.pack();
356 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
357 myFrame.setLocationRelativeTo(null);
358 myFrame.setVisible(true);
359 return myFrame;
360 }
361
374 public JFrame viewBar(int width, int height) {
375 JFrame myFrame;
376 if (chart.getTitle() != null)
377 myFrame = new JFrame("XYLineChart from SSJ: " + chart.getTitle().getText());
378 else
379 myFrame = new JFrame("XYLineChart from SSJ");
380
381 XYPlot plot = (XYPlot) chart.getPlot();
382
383 // Create the bar
384 plot.setDataset(0, dataset.getSeriesCollection());
385 final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(false, true);
386 renderer.setSeriesPaint(0, Color.ORANGE);
387 renderer.setSeriesShape(0, new Line2D.Double(0, 0, 0, 1000));
388 plot.setRenderer(0, renderer);
389
390 // Create the points
391 plot.setDataset(1, dataset.getSeriesCollection());
392 final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(false, true);
393 renderer2.setSeriesPaint(0, Color.ORANGE);
394 renderer2.setSeriesShape(0, new Ellipse2D.Double(-2.0, -2.0, 4.0, 4.0));
395 plot.setRenderer(1, renderer2);
396
397 ChartPanel chartPanel = new ChartPanel(chart);
398 chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
399 myFrame.setContentPane(chartPanel);
400 myFrame.pack();
401 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
402 myFrame.setLocationRelativeTo(null);
403 myFrame.setVisible(true);
404 return myFrame;
405 }
406
410 public String toLatex(double width, double height) {
411 double xunit = 0, yunit = 0;
412 double[] save = new double[4];
413
414 if (dataset.getSeriesCollection().getSeriesCount() == 0)
415 throw new IllegalArgumentException("Empty chart");
416
417 // Calcul des parametres d'echelle et de decalage
418 double XScale = computeXScale(XAxis.getTwinAxisPosition());
419 double YScale = computeYScale(YAxis.getTwinAxisPosition());
420
421 // taille d'une unite en x et en cm dans l'objet "tikzpicture"
422 xunit = width / ((Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition()) * XScale)
423 - (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition()) * XScale));
424 // taille d'une unite en y et en cm dans l'objet "tikzpicture"
425 yunit = height / ((Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition()) * YScale)
426 - (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition()) * YScale));
427
428 Formatter formatter = new Formatter(Locale.US);
429
430 /* Entete du document */
431 if (latexDocFlag) {
432 formatter.format("\\documentclass[12pt]{article}%n%n");
433 formatter.format("\\usepackage{tikz}%n\\usetikzlibrary{plotmarks}%n\\begin{document}%n%n");
434 }
435 if (chart.getTitle() != null)
436 formatter.format("%% PGF/TikZ picture from SSJ: %s%n", chart.getTitle().getText());
437 else
438 formatter.format("%% PGF/TikZ picture from SSJ %n");
439 formatter.format("%% XScale = %s, YScale = %s, XShift = %s, YShift = %s%n", XScale, YScale,
440 XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition());
441 formatter.format("%% Therefore, thisFileXValue = (originalSeriesXValue+XShift)*XScale%n");
442 formatter.format("%% and thisFileYValue = (originalSeriesYValue+YShift)*YScale%n%n");
443 if (chart.getTitle() != null)
444 formatter.format("\\begin{figure}%n");
445 formatter.format("\\begin{center}%n");
446 formatter.format("\\begin{tikzpicture}[x=%scm, y=%scm]%n", xunit, yunit);
447 formatter.format("\\footnotesize%n");
448 if (grid)
449 formatter.format("\\draw[color=lightgray] (%s, %s) grid[xstep = %s, ystep=%s] (%s, %s);%n",
450 (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition())
451 - XAxis.getTwinAxisPosition()) * XScale,
452 (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition())
453 - YAxis.getTwinAxisPosition()) * YScale,
454 xstepGrid * XScale, ystepGrid * YScale,
455 (Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition())
456 - XAxis.getTwinAxisPosition()) * XScale,
457 (Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition())
458 - YAxis.getTwinAxisPosition()) * YScale);
459 setTick0Flags();
460 formatter.format("%s", XAxis.toLatex(XScale));
461 formatter.format("%s", YAxis.toLatex(YScale));
462
463 formatter.format("%s",
464 dataset.toLatex(XScale, YScale, XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition(),
465 XAxis.getAxis().getLowerBound(), XAxis.getAxis().getUpperBound(), YAxis.getAxis().getLowerBound(),
466 YAxis.getAxis().getUpperBound()));
467
468 formatter.format("\\end{tikzpicture}%n");
469 formatter.format("\\end{center}%n");
470 if (chart.getTitle() != null) {
471 formatter.format("\\caption{");
472 formatter.format(chart.getTitle().getText());
473 formatter.format("}%n\\end{figure}%n");
474 }
475 if (latexDocFlag)
476 formatter.format("\\end{document}%n");
477 return formatter.toString();
478 }
479
480}
481
Represents an axis of a chart encapsulated by an instance of XYChart.
Definition Axis.java:42
This class provides tools to create charts from data in a simple way.
Definition XYChart.java:56
void setprobFlag(boolean flag)
Must be set true when plotting probabilities, false otherwise.
Definition XYChart.java:133
void setAutoRange()
The and the ranges of the chart are set automatically.
Definition XYChart.java:140
int add(double[][] data)
Adds the new collection of data series data into the series collection.
int add(double[] x, double[] y)
Adds a data series into the series collection.
JFrame viewBar(int width, int height)
Displays bar chart on the screen using Swing.
XYLineChart()
Initializes a new XYLineChart instance with an empty data set.
XYLineChart(String title, String XLabel, String YLabel, double[][] data, int numPoints)
Initializes a new XYLineChart instance with sets of points data.
String toLatex(double width, double height)
Exports the chart to a LaTeX source code using PGF/TikZ.
int add(double[] x, double[] y, String name, String plotStyle)
Adds a data series into the series collection.
XYLineChart(String title, String XLabel, String YLabel, double[][]... data)
Initializes a new XYLineChart instance with sets of points data.
void setTicksSynchro(int s)
Synchronizes -axis ticks to the -th series.
void setSeriesCollection(XYListSeriesCollection dataset)
Links a new dataset to the current chart.
JFrame view(int width, int height)
Displays chart on the screen using Swing.
XYLineChart(String title, String XLabel, String YLabel, DoubleArrayList... data)
Initializes a new XYLineChart instance with data data.
XYLineChart(String title, String XLabel, String YLabel, double[][] data, int x, int y)
Initializes a new XYLineChart instance using subsets of data.
XYLineChart(String title, String XLabel, String YLabel, XYSeriesCollection data)
Initializes a new XYLineChart instance with data data.
int add(double[][] data, int numPoints)
Adds the new collection of data series data into the series collection.
int add(double[] x, double[] y, int numPoints)
Adds a data series into the series collection.
XYListSeriesCollection getSeriesCollection()
Returns the chart’s dataset.
XYListSeriesCollection()
Stores data used in a XYLineChart or in other related charts, and provides complementary tools to dra...