SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ScatterChart.java
1/*
2 * Class: ScatterChart
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.axis.NumberAxis;
28import org.jfree.chart.ChartFactory;
29import org.jfree.chart.ChartPanel;
30import org.jfree.chart.plot.XYPlot;
31import org.jfree.chart.plot.PlotOrientation;
32import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
33import org.jfree.chart.renderer.xy.XYDotRenderer;
34import org.jfree.data.xy.XYSeriesCollection;
35import java.util.Locale;
36import java.util.Formatter;
37import cern.colt.list.DoubleArrayList;
38import javax.swing.JFrame;
39
46public class ScatterChart extends XYChart {
47
48 protected void init(String title, String XLabel, String YLabel) {
49 // create the chart...
50 chart = ChartFactory.createScatterPlot(title, // chart title
51 XLabel, // x axis label
52 YLabel, // y axis label
53 dataset.getSeriesCollection(), // data
54 PlotOrientation.VERTICAL, true, // include legend
55 true, // tooltips
56 false // urls
57 );
58
59 ((XYPlot) chart.getPlot()).setRenderer(dataset.getRenderer());
60 // Initialize axis variables
61 initAxis();
62
63 int nb = getSeriesCollection().getSeriesCollection().getSeriesCount();
64 for (int i = 0; i < nb; i++) {
65 getSeriesCollection().setDashPattern(i, "only marks");
66 getSeriesCollection().setMarksType(i, "+");
67 }
68 }
69
70 protected void initAxis() {
71 XAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getDomainAxis(), Axis.ORIENTATION_HORIZONTAL);
72 YAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis(), Axis.ORIENTATION_VERTICAL);
73 setAutoRange(true, true);
74 }
75
79 public ScatterChart() {
80 super();
81 dataset = new XYListSeriesCollection();
82 init(null, null, null);
83 }
84
107 public ScatterChart(String title, String XLabel, String YLabel, double[][]... data) {
108 super();
109 dataset = new XYListSeriesCollection(data);
110 init(title, XLabel, YLabel);
111 }
112
134 public ScatterChart(String title, String XLabel, String YLabel, double[][] data, int numPoints) {
135 super();
136 dataset = new XYListSeriesCollection(data, numPoints);
137 init(title, XLabel, YLabel);
138 }
139
157 public ScatterChart(String title, String XLabel, String YLabel, double[][] data, int x, int y) {
158 super();
159 int len = data[0].length;
160 double[][] proj = new double[2][len];
161 for (int i = 0; i < len; i++) {
162 proj[0][i] = data[x][i];
163 proj[1][i] = data[y][i];
164 }
165 dataset = new XYListSeriesCollection(proj);
166 init(title, XLabel, YLabel);
167 }
168
180 public ScatterChart(String title, String XLabel, String YLabel, DoubleArrayList... data) {
181 super();
182 dataset = new XYListSeriesCollection(data);
183 init(title, XLabel, YLabel);
184 }
185
197 public ScatterChart(String title, String XLabel, String YLabel, XYSeriesCollection data) {
198 super();
199 dataset = new XYListSeriesCollection(data);
200 init(title, XLabel, YLabel);
201 }
202
216 public int add(double[] x, double[] y, String name, String plotStyle) {
217 int seriesIndex = add(x, y);
218 getSeriesCollection().setName(seriesIndex, name);
219 getSeriesCollection().setPlotStyle(seriesIndex, plotStyle);
220 return seriesIndex;
221 }
222
233 public int add(double[] x, double[] y) {
234 return add(x, y, x.length);
235 }
236
249 public int add(double[] x, double[] y, int numPoints) {
250 int seriesIndex = getSeriesCollection().add(x, y, numPoints);
251 initAxis();
252 getSeriesCollection().setMarksType(seriesIndex, "+");
253 getSeriesCollection().setDashPattern(seriesIndex, "only marks");
254 return seriesIndex;
255 }
256
265
272 this.dataset = dataset;
273 }
274
280 public void setTicksSynchro(int s) {
281 XYSeriesCollection seriesCollection = (XYSeriesCollection) this.dataset.getSeriesCollection();
282 double[] values = new double[seriesCollection.getItemCount(s)];
283
284 for (int i = 0; i < seriesCollection.getItemCount(s); i++)
285 values[i] = seriesCollection.getXValue(s, i);
286
287 XAxis.setLabels(values);
288 }
289
302 public JFrame view(int width, int height) {
303 JFrame myFrame;
304 if (chart.getTitle() != null)
305 myFrame = new JFrame("ScatterChart from SSJ: " + chart.getTitle().getText());
306 else
307 myFrame = new JFrame("ScatterChart from SSJ");
308 XYPlot plot = chart.getXYPlot();
309
310 /*
311 * // The drawn points are somewhat big, of different shapes, unfilled
312 * XYLineAndShapeRenderer shape = new XYLineAndShapeRenderer(false, true); int
313 * nb = getSeriesCollection().getSeriesCollection().getSeriesCount(); for (int i
314 * = 0 ; i < nb ; i++) { shape.setSeriesShapesFilled(i, false);
315 * plot.setRenderer(i, shape); }
316 */
317 // The drawn points are all square, filled
318 XYDotRenderer shape = new XYDotRenderer();
319 final int dotSize = 3;
320 shape.setDotWidth(dotSize);
321 shape.setDotHeight(dotSize);
322 int nb = getSeriesCollection().getSeriesCollection().getSeriesCount();
323 for (int i = 0; i < nb; i++)
324 plot.setRenderer(i, shape);
325
326 ChartPanel chartPanel = new ChartPanel(chart);
327 chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
328 myFrame.setContentPane(chartPanel);
329 myFrame.pack();
330 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
331 myFrame.setLocationRelativeTo(null);
332 myFrame.setVisible(true);
333 return myFrame;
334 }
335
339 public String toLatex(double width, double height) {
340 double xunit = 0, yunit = 0;
341 double[] save = new double[4];
342
343 if (dataset.getSeriesCollection().getSeriesCount() == 0)
344 throw new IllegalArgumentException("Empty chart");
345
346 // Calcul des parametres d'echelle et de decalage
347 double XScale = computeXScale(XAxis.getTwinAxisPosition());
348 double YScale = computeYScale(YAxis.getTwinAxisPosition());
349
350 // taille d'une unite en x et en cm dans l'objet "tikzpicture"
351 xunit = width / ((Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition()) * XScale)
352 - (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition()) * XScale));
353 // taille d'une unite en y et en cm dans l'objet "tikzpicture"
354 yunit = height / ((Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition()) * YScale)
355 - (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition()) * YScale));
356
357 Formatter formatter = new Formatter(Locale.US);
358
359 /* Entete du document */
360 if (latexDocFlag) {
361 formatter.format("\\documentclass[12pt]{article}%n%n");
362 formatter.format("\\usepackage{tikz}%n\\usetikzlibrary{plotmarks}%n\\begin{document}%n%n");
363 }
364 if (chart.getTitle() != null)
365 formatter.format("%% PGF/TikZ picture from SSJ: %s%n", chart.getTitle().getText());
366 else
367 formatter.format("%% PGF/TikZ picture from SSJ %n");
368 formatter.format("%% XScale = %s, YScale = %s, XShift = %s, YShift = %s%n", XScale, YScale,
369 XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition());
370 formatter.format("%% Therefore, thisFileXValue = (originalSeriesXValue+XShift)*XScale%n");
371 formatter.format("%% and thisFileYValue = (originalSeriesYValue+YShift)*YScale%n%n");
372 if (chart.getTitle() != null)
373 formatter.format("\\begin{figure}%n");
374 formatter.format("\\begin{center}%n");
375 formatter.format("\\begin{tikzpicture}[x=%scm, y=%scm]%n", xunit, yunit);
376 formatter.format("\\footnotesize%n");
377 if (grid)
378 formatter.format("\\draw[color=lightgray] (%s, %s) grid[xstep = %s, ystep=%s] (%s, %s);%n",
379 (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition())
380 - XAxis.getTwinAxisPosition()) * XScale,
381 (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition())
382 - YAxis.getTwinAxisPosition()) * YScale,
383 xstepGrid * XScale, ystepGrid * YScale,
384 (Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition())
385 - XAxis.getTwinAxisPosition()) * XScale,
386 (Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition())
387 - YAxis.getTwinAxisPosition()) * YScale);
388 setTick0Flags();
389 formatter.format("%s", XAxis.toLatex(XScale));
390 formatter.format("%s", YAxis.toLatex(YScale));
391
392 formatter.format("%s",
393 dataset.toLatex(XScale, YScale, XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition(),
394 XAxis.getAxis().getLowerBound(), XAxis.getAxis().getUpperBound(), YAxis.getAxis().getLowerBound(),
395 YAxis.getAxis().getUpperBound()));
396
397 formatter.format("\\end{tikzpicture}%n");
398 formatter.format("\\end{center}%n");
399 if (chart.getTitle() != null) {
400 formatter.format("\\caption{");
401 formatter.format(chart.getTitle().getText());
402 formatter.format("}%n\\end{figure}%n");
403 }
404 if (latexDocFlag)
405 formatter.format("\\end{document}%n");
406 return formatter.toString();
407 }
408
409}
410
Represents an axis of a chart encapsulated by an instance of XYChart.
Definition Axis.java:42
ScatterChart()
Initializes a new ScatterChart instance with an empty data set.
ScatterChart(String title, String XLabel, String YLabel, DoubleArrayList... data)
Initializes a new ScatterChart instance with data data.
JFrame view(int width, int height)
Displays chart on the screen using Swing.
void setTicksSynchro(int s)
Synchronizes -axis ticks to the -th series.
ScatterChart(String title, String XLabel, String YLabel, XYSeriesCollection data)
Initializes a new ScatterChart instance with data data.
int add(double[] x, double[] y)
Adds a data series into the series collection.
ScatterChart(String title, String XLabel, String YLabel, double[][] data, int x, int y)
Initializes a new ScatterChart instance using subsets of data.
void setSeriesCollection(XYListSeriesCollection dataset)
Links a new dataset to the current chart.
ScatterChart(String title, String XLabel, String YLabel, double[][] data, int numPoints)
Initializes a new ScatterChart instance with sets of points data.
ScatterChart(String title, String XLabel, String YLabel, double[][]... data)
Initializes a new ScatterChart instance with data data.
String toLatex(double width, double height)
Exports the chart to a LaTeX source code using PGF/TikZ.
int add(double[] x, double[] y, int numPoints)
Adds a data series into the series collection.
XYListSeriesCollection getSeriesCollection()
Returns the chart’s dataset.
int add(double[] x, double[] y, String name, String plotStyle)
Adds a data series into the series collection.
This class provides tools to create charts from data in a simple way.
Definition XYChart.java:56
void setAutoRange()
The and the ranges of the chart are set automatically.
Definition XYChart.java:140
XYListSeriesCollection()
Stores data used in a XYLineChart or in other related charts, and provides complementary tools to dra...