25package umontreal.ssj.charts;
27import umontreal.ssj.stat.*;
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;
36import cern.colt.list.DoubleArrayList;
38import java.util.ListIterator;
39import java.util.Locale;
40import java.util.Formatter;
41import javax.swing.JFrame;
51 protected void init(String title, String XLabel, String YLabel) {
53 chart = ChartFactory.createXYLineChart(title,
56 dataset.getSeriesCollection(),
57 PlotOrientation.VERTICAL,
true,
61 ((XYPlot) chart.getPlot()).setRenderer(dataset.getRenderer());
63 XAxis =
new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getDomainAxis(),
Axis.ORIENTATION_HORIZONTAL);
64 YAxis =
new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis(),
Axis.ORIENTATION_VERTICAL);
74 init(
null,
null,
null);
90 public HistogramChart(String title, String XLabel, String YLabel,
double[]... data) {
93 init(title, XLabel, YLabel);
110 public HistogramChart(String title, String XLabel, String YLabel,
double[] data,
int numPoints) {
112 double[] datan =
new double[numPoints];
113 System.arraycopy(data, 0, datan, 0, numPoints);
115 init(title, XLabel, YLabel);
128 public HistogramChart(String title, String XLabel, String YLabel, DoubleArrayList... data) {
131 init(title, XLabel, YLabel);
147 init(title, XLabel, YLabel);
165 init(title, XLabel, YLabel);
182 public HistogramChart(String title, String XLabel, String YLabel,
int[] count,
double[] bound) {
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;
188 for (
int i = 0; i < nb; i++)
190 double[] data =
new double[sum];
194 for (
int i = 0; i < nb; i++) {
195 h = bound[i + 1] - bound[i];
199 for (
int j = 0; j < count[i]; j++)
200 data[k++] = bound[i + 1] - j * h;
202 for (
int j = 0; j < count[i]; j++)
203 data[k++] = bound[i] + j * h;
208 init(title, XLabel, YLabel);
227 init(title, XLabel, YLabel);
231 throw new UnsupportedOperationException(
232 "You can't use setAutoRange with HistogramChart class, use setAutoRange().");
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).");
255 this.dataset = dataset;
267 DoubleArrayList newTicks =
new DoubleArrayList();
270 HistogramBin prec = (HistogramBin) binsIter.next();
272 newTicks.add(prec.getStartBoundary());
273 newTicks.add(var = prec.getEndBoundary());
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());
283 XAxis.setLabels(newTicks.elements());
291 double w = (B[1] - B[0]) / n;
309 public JFrame
view(
int width,
int height) {
311 if (chart.getTitle() !=
null)
312 myFrame =
new JFrame(
"HistogramChart from SSJ: " + chart.getTitle().getText());
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);
319 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
320 myFrame.setLocationRelativeTo(
null);
321 myFrame.setVisible(
true);
328 public String
toLatex(
double width,
double height) {
331 if (dataset.getSeriesCollection().getSeriesCount() == 0)
332 throw new IllegalArgumentException(
"Empty chart");
333 if (YAxis.getTwinAxisPosition() < 0)
334 YAxis.setTwinAxisPosition(0);
337 double XScale = computeXScale(XAxis.getTwinAxisPosition());
338 double YScale = computeYScale(YAxis.getTwinAxisPosition());
341 xunit = width / ((Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition()) * XScale)
342 - (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition()) * XScale));
344 yunit = height / ((Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition()) * YScale)
345 - (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition()) * YScale));
347 Formatter formatter =
new Formatter(Locale.US);
351 formatter.format(
"\\documentclass[12pt]{article}%n%n");
352 formatter.format(
"\\usepackage{tikz}%n\\usetikzlibrary{plotmarks}%n\\begin{document}%n%n");
354 if (chart.getTitle() !=
null)
355 formatter.format(
"%% PGF/TikZ picture from SSJ: %s%n", chart.getTitle().getText());
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");
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);
379 formatter.format(
"%s", XAxis.toLatex(XScale));
380 formatter.format(
"%s", YAxis.toLatex(YScale));
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()));
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");
395 formatter.format(
"\\end{document}%n");
396 return formatter.toString();
Represents an axis of a chart encapsulated by an instance of XYChart.
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.
Stores data used in a HistogramChart.
This class provides tools to create charts from data in a simple way.
void setAutoRange()
The and the ranges of the chart are set automatically.
This class extends Tally.
This class is a variant of Tally for which the individual observations are stored in a list implement...