SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
HistogramChart.java
1/*
2 * Class: HistogramChart
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.*;
28
29import org.jfree.chart.ChartFactory;
30import org.jfree.chart.ChartPanel;
31import org.jfree.chart.axis.NumberAxis;
32import org.jfree.chart.plot.XYPlot;
33import org.jfree.chart.plot.PlotOrientation;
34import org.jfree.data.statistics.HistogramBin;
35
36import cern.colt.list.DoubleArrayList;
37
38import java.util.ListIterator;
39import java.util.Locale;
40import java.util.Formatter;
41import javax.swing.JFrame;
42
49public class HistogramChart extends XYChart {
50
51 protected void init(String title, String XLabel, String YLabel) {
52 // create the chart...
53 chart = ChartFactory.createXYLineChart(title, // chart title
54 XLabel, // x axis label
55 YLabel, // y axis label
56 dataset.getSeriesCollection(), // data
57 PlotOrientation.VERTICAL, true, // include legend
58 true, // tooltips
59 false // urls
60 );
61 ((XYPlot) chart.getPlot()).setRenderer(dataset.getRenderer());
62 // Initialize axis variables
63 XAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getDomainAxis(), Axis.ORIENTATION_HORIZONTAL);
64 YAxis = new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis(), Axis.ORIENTATION_VERTICAL);
65 setAutoRange(false, true, true, true);
66 }
67
71 public HistogramChart() {
72 super();
73 dataset = new HistogramSeriesCollection();
74 init(null, null, null);
75 }
76
90 public HistogramChart(String title, String XLabel, String YLabel, double[]... data) {
91 super();
92 dataset = new HistogramSeriesCollection(data);
93 init(title, XLabel, YLabel);
94 }
95
110 public HistogramChart(String title, String XLabel, String YLabel, double[] data, int numPoints) {
111 super();
112 double[] datan = new double[numPoints];
113 System.arraycopy(data, 0, datan, 0, numPoints);
114 dataset = new HistogramSeriesCollection(datan);
115 init(title, XLabel, YLabel);
116 }
117
128 public HistogramChart(String title, String XLabel, String YLabel, DoubleArrayList... data) {
129 super();
130 dataset = new HistogramSeriesCollection(data);
131 init(title, XLabel, YLabel);
132 }
133
144 public HistogramChart(String title, String XLabel, String YLabel, TallyStore... tallies) {
145 super();
146 dataset = new HistogramSeriesCollection(tallies);
147 init(title, XLabel, YLabel);
148 }
149
162 public HistogramChart(String title, String XLabel, String YLabel, CustomHistogramDataset data) {
163 super();
164 dataset = new HistogramSeriesCollection(data);
165 init(title, XLabel, YLabel);
166 }
167
182 public HistogramChart(String title, String XLabel, String YLabel, int[] count, double[] bound) {
183 super();
184 if (bound.length != count.length + 1)
185 throw new IllegalArgumentException("bound.length must be equal to count.length + 1");
186 final int nb = count.length;
187 int sum = 0;
188 for (int i = 0; i < nb; i++)
189 sum += count[i];
190 double[] data = new double[sum];
191
192 int k = 0;
193 double h;
194 for (int i = 0; i < nb; i++) {
195 h = bound[i + 1] - bound[i];
196 if (count[i] > 0)
197 h /= count[i];
198 if (i == nb - 1) {
199 for (int j = 0; j < count[i]; j++)
200 data[k++] = bound[i + 1] - j * h;
201 } else {
202 for (int j = 0; j < count[i]; j++)
203 data[k++] = bound[i] + j * h;
204 }
205 }
206
207 dataset = new HistogramSeriesCollection(data, sum);
208 init(title, XLabel, YLabel);
209 ((HistogramSeriesCollection) dataset).setBins(0, nb);
210 }
211
224 public HistogramChart(String title, String XLabel, String YLabel, TallyHistogram... tallies) {
225 super();
226 dataset = new HistogramSeriesCollection(tallies);
227 init(title, XLabel, YLabel);
228 }
229
230 public void setAutoRange(boolean right, boolean top) {
231 throw new UnsupportedOperationException(
232 "You can't use setAutoRange with HistogramChart class, use setAutoRange().");
233 }
234
235 public void setManuelRange(double[] range, boolean right, boolean top) {
236 throw new UnsupportedOperationException(
237 "You can't use setManuelRange with HistogramChart class, use setManuelRange(range).");
238 }
239
248
255 this.dataset = dataset;
256 }
257
265 public void setTicksSynchro(int s) {
266 if (((CustomHistogramDataset) this.dataset.getSeriesCollection()).getBinWidth(s) == -1) {
267 DoubleArrayList newTicks = new DoubleArrayList();
268 ListIterator binsIter = ((HistogramSeriesCollection) this.dataset).getBins(s).listIterator();
269
270 HistogramBin prec = (HistogramBin) binsIter.next();
271 double var;
272 newTicks.add(prec.getStartBoundary());
273 newTicks.add(var = prec.getEndBoundary());
274 HistogramBin temp;
275 while (binsIter.hasNext()) {
276 temp = (HistogramBin) binsIter.next();
277 if (temp.getStartBoundary() != var) {
278 newTicks.add(var = temp.getStartBoundary());
279 } else if (temp.getEndBoundary() != var) {
280 newTicks.add(var = temp.getEndBoundary());
281 }
282 }
283 XAxis.setLabels(newTicks.elements());
284 } else {
285 // set a label-tick for each bin, if num bins is <= 10
286 int n = ((HistogramSeriesCollection) this.dataset).getBins(s).size();
287 if (n > 10) {
288 // number of bins is too large, set ~10 labels-ticks for histogram
289 n = 10;
290 double[] B = ((HistogramSeriesCollection) this.dataset).getDomainBounds();
291 double w = (B[1] - B[0]) / n;
292 XAxis.setLabels(w);
293 } else {
294 XAxis.setLabels(((CustomHistogramDataset) this.dataset.getSeriesCollection()).getBinWidth(s));
295 }
296 }
297 }
298
309 public JFrame view(int width, int height) {
310 JFrame myFrame;
311 if (chart.getTitle() != null)
312 myFrame = new JFrame("HistogramChart from SSJ: " + chart.getTitle().getText());
313 else
314 myFrame = new JFrame("HistogramChart from SSJ");
315 ChartPanel chartPanel = new ChartPanel(chart);
316 chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
317 myFrame.setContentPane(chartPanel);
318 myFrame.pack();
319 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
320 myFrame.setLocationRelativeTo(null);
321 myFrame.setVisible(true);
322 return myFrame;
323 }
324
328 public String toLatex(double width, double height) {
329 double xunit, yunit;
330
331 if (dataset.getSeriesCollection().getSeriesCount() == 0)
332 throw new IllegalArgumentException("Empty chart");
333 if (YAxis.getTwinAxisPosition() < 0)
334 YAxis.setTwinAxisPosition(0);
335
336 // Calcul des parametres d'echelle et de decalage
337 double XScale = computeXScale(XAxis.getTwinAxisPosition());
338 double YScale = computeYScale(YAxis.getTwinAxisPosition());
339
340 // taille d'une unite en x et en cm dans l'objet "tikzpicture"
341 xunit = width / ((Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition()) * XScale)
342 - (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition()) * XScale));
343 // taille d'une unite en y et en cm dans l'objet "tikzpicture"
344 yunit = height / ((Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition()) * YScale)
345 - (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition()) * YScale));
346
347 Formatter formatter = new Formatter(Locale.US);
348
349 /* Entete du document */
350 if (latexDocFlag) {
351 formatter.format("\\documentclass[12pt]{article}%n%n");
352 formatter.format("\\usepackage{tikz}%n\\usetikzlibrary{plotmarks}%n\\begin{document}%n%n");
353 }
354 if (chart.getTitle() != null)
355 formatter.format("%% PGF/TikZ picture from SSJ: %s%n", chart.getTitle().getText());
356 else
357 formatter.format("%% PGF/TikZ picture from SSJ %n");
358 formatter.format("%% XScale = %s, YScale = %s, XShift = %s, YShift = %s%n", XScale, YScale,
359 XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition());
360 formatter.format("%% Therefore, thisFileXValue = (originalSeriesXValue+XShift)*XScale%n");
361 formatter.format("%% and thisFileYValue = (originalSeriesYValue+YShift)*YScale%n%n");
362 if (chart.getTitle() != null)
363 formatter.format("\\begin{figure}%n");
364 formatter.format("\\begin{center}%n");
365 formatter.format("\\begin{tikzpicture}[x=%scm, y=%scm]%n", xunit, yunit);
366 formatter.format("\\footnotesize%n");
367 if (grid)
368 formatter.format("\\draw[color=lightgray] (%s, %s) grid[xstep = %s, ystep=%s] (%s, %s);%n",
369 (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition())
370 - XAxis.getTwinAxisPosition()) * XScale,
371 (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition())
372 - YAxis.getTwinAxisPosition()) * YScale,
373 xstepGrid * XScale, ystepGrid * YScale,
374 (Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition())
375 - XAxis.getTwinAxisPosition()) * XScale,
376 (Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition())
377 - YAxis.getTwinAxisPosition()) * YScale);
378 setTick0Flags();
379 formatter.format("%s", XAxis.toLatex(XScale));
380 formatter.format("%s", YAxis.toLatex(YScale));
381
382 formatter.format("%s",
383 dataset.toLatex(XScale, YScale, XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition(),
384 XAxis.getAxis().getLowerBound(), XAxis.getAxis().getUpperBound(), YAxis.getAxis().getLowerBound(),
385 YAxis.getAxis().getUpperBound()));
386
387 formatter.format("\\end{tikzpicture}%n");
388 formatter.format("\\end{center}%n");
389 if (chart.getTitle() != null) {
390 formatter.format("\\caption{");
391 formatter.format(chart.getTitle().getText());
392 formatter.format("}%n\\end{figure}%n");
393 }
394 if (latexDocFlag)
395 formatter.format("\\end{document}%n");
396 return formatter.toString();
397 }
398
399}
400
Represents an axis of a chart encapsulated by an instance of XYChart.
Definition Axis.java:42
A dataset that can be used for creating histograms.
HistogramChart()
Initializes a new HistogramChart instance with an empty data set.
HistogramChart(String title, String XLabel, String YLabel, TallyStore... tallies)
Initializes a new HistogramChart instance with data arrays contained in each umontreal....
HistogramChart(String title, String XLabel, String YLabel, DoubleArrayList... data)
Initializes a new HistogramChart instance with data data.
void setSeriesCollection(HistogramSeriesCollection dataset)
Links a new dataset to the current chart.
HistogramChart(String title, String XLabel, String YLabel, TallyHistogram... tallies)
Initializes a new HistogramChart instance with data arrays contained in each umontreal....
HistogramChart(String title, String XLabel, String YLabel, double[] data, int numPoints)
Initializes a new HistogramChart instance with input data.
HistogramChart(String title, String XLabel, String YLabel, int[] count, double[] bound)
Initializes a new HistogramChart instance with data count and bound.
HistogramSeriesCollection getSeriesCollection()
Returns the chart’s dataset.
JFrame view(int width, int height)
Displays chart on the screen using Swing.
HistogramChart(String title, String XLabel, String YLabel, CustomHistogramDataset data)
Initializes a new HistogramChart instance with data data.
void setTicksSynchro(int s)
Synchronizes -axis ticks to the -th histogram bins if the number of bins is not larger than 10; other...
void setAutoRange(boolean right, boolean top)
The and the ranges of the chart are set automatically.
HistogramChart(String title, String XLabel, String YLabel, double[]... data)
Initializes a new HistogramChart instance with input data.
String toLatex(double width, double height)
Exports the chart to a LaTeX source code using PGF/TikZ.
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
This class is a variant of Tally for which the individual observations are stored in a list implement...