25package umontreal.ssj.charts;
27import umontreal.ssj.stat.*;
28import umontreal.ssj.util.Num;
30import org.jfree.chart.renderer.xy.XYBarRenderer;
31import org.jfree.data.statistics.HistogramBin;
33import cern.colt.list.DoubleArrayList;
35import java.util.Locale;
36import java.util.Formatter;
38import java.util.ListIterator;
54 protected boolean[] filled;
55 protected double[] lineWidth;
56 protected int numBin = 20;
58 private int calcNumBins(
int n) {
60 int numbins = (int) Math.ceil(1.0 +
Num.
log2(n));
68 final double binwidth = (hist.
getB() - hist.
getA()) / nb;
71 for (
int i = 1; i <= nb; i++)
73 double[] data =
new double[sum];
77 for (
int i = 1; i <= nb; i++) {
79 base = hist.
getA() + (i - 1) * binwidth;
83 for (
int j = 0; j < count[i]; j++)
84 data[k++] = hist.
getB() - j * h;
86 for (
int j = 0; j < count[i]; j++)
87 data[k++] = base + j * h;
97 renderer =
new XYBarRenderer();
111 renderer =
new XYBarRenderer();
113 for (
int i = 0; i < data.length; i++) {
114 numBin = calcNumBins(data[i].length);
115 tempSeriesCollection.
addSeries(i, data[i], numBin);
124 filled =
new boolean[seriesCollection.getSeriesCount()];
125 lineWidth =
new double[seriesCollection.getSeriesCount()];
144 renderer =
new XYBarRenderer();
146 numBin = calcNumBins(numPoints);
147 tempSeriesCollection.
addSeries(0, data, numPoints, numBin);
155 filled =
new boolean[seriesCollection.getSeriesCount()];
156 lineWidth =
new double[seriesCollection.getSeriesCount()];
174 renderer =
new XYBarRenderer();
177 for (
int i = 0; i < data.length; i++) {
178 numBin = calcNumBins(data[i].size());
179 tempSeriesCollection.
addSeries(i, data[i].elements(), data[i].size(), numBin);
188 filled =
new boolean[seriesCollection.getSeriesCount()];
189 lineWidth =
new double[seriesCollection.getSeriesCount()];
207 renderer =
new XYBarRenderer();
211 for (
int i = 0; i < tallies.length; i++) {
213 h = 3.5 * tallies[i].standardDeviation() / Math.pow(tallies[i].numberObs(), 1.0 / 3.0);
214 numBin = (int) ((tallies[i].max() - tallies[i].min()) / (1.5 * h));
215 tempSeriesCollection.
addSeries(i, tallies[i].getArray(), tallies[i].numberObs(), numBin, tallies[i].min(),
225 filled =
new boolean[seriesCollection.getSeriesCount()];
226 lineWidth =
new double[seriesCollection.getSeriesCount()];
245 renderer =
new XYBarRenderer();
249 for (
int i = 0; i < tallies.length; i++) {
250 data = countersToArray(tallies[i]);
251 tempSeriesCollection.
addSeries(i, data, data.length, tallies[i].getNumBins(), tallies[i].getA(),
261 filled =
new boolean[seriesCollection.getSeriesCount()];
262 lineWidth =
new double[seriesCollection.getSeriesCount()];
278 renderer =
new XYBarRenderer();
279 seriesCollection = data;
287 filled =
new boolean[seriesCollection.getSeriesCount()];
288 lineWidth =
new double[seriesCollection.getSeriesCount()];
306 public int add(
double[] data) {
307 return add(data, data.length);
318 public int add(
double[] data,
int numPoints) {
322 boolean[] newFilled =
new boolean[seriesCollection.getSeriesCount()];
323 double[] newLineWidth =
new double[seriesCollection.getSeriesCount()];
326 int j = seriesCollection.getSeriesCount() - 1;
328 newFilled[j] =
false;
329 newLineWidth[j] = 0.5;
331 for (j = 0; j < seriesCollection.getSeriesCount() - 1; j++) {
332 newFilled[j] = filled[j];
333 newLineWidth[j] = lineWidth[j];
337 lineWidth = newLineWidth;
339 return seriesCollection.getSeriesCount() - 1;
349 public int add(DoubleArrayList observationSet) {
350 return add(observationSet.elements(), observationSet.size());
415 public void setBins(
int series,
int bins,
double minimum,
double maximum) {
429 public void setBins(
int series, HistogramBin[] binsTable) {
498 return filled[series];
510 this.filled[series] = filled;
518 return ((XYBarRenderer) renderer).getMargin();
528 ((XYBarRenderer) renderer).
setMargin(margin);
537 return lineWidth[series];
547 this.lineWidth[series] = outline;
550 public String
toLatex(
double XScale,
double YScale,
double XShift,
double YShift,
double xmin,
double xmax,
551 double ymin,
double ymax) {
555 xmin = Math.min(XShift, xmin);
556 xmax = Math.max(XShift, xmax);
557 ymin = Math.min(YShift, ymin);
558 ymax = Math.max(YShift, ymax);
561 Formatter formatter =
new Formatter(Locale.US);
563 double margin = ((XYBarRenderer) renderer).
getMargin();
565 for (
int i = tempSeriesCollection.
getSeriesCount() - 1; i >= 0; i--) {
566 List temp = tempSeriesCollection.
getBins(i);
567 ListIterator iter = temp.listIterator();
569 Color color = (Color) renderer.getSeriesPaint(i);
571 if (colorString ==
null) {
572 colorString =
"color" + i;
573 formatter.format(
"\\definecolor{%s}{rgb}{%.2f, %.2f, %.2f}%n", colorString, color.getRed() / 255.0,
574 color.getGreen() / 255.0, color.getBlue() / 255.0);
577 HistogramBin currentBin =
null;
578 while (iter.hasNext()) {
579 double currentMargin;
580 currentBin = (HistogramBin) iter.next();
581 currentMargin = ((margin * (currentBin.getEndBoundary() - currentBin.getStartBoundary()))) * XScale;
582 if ((currentBin.getStartBoundary() >= xmin && currentBin.getStartBoundary() <= xmax)
583 && (currentBin.getCount() >= ymin && currentBin.getCount() <= ymax)) {
584 var = Math.min(currentBin.getEndBoundary(), xmax);
587 "\\filldraw [line width=%.2fpt, opacity=%.2f, color=%s] ([xshift=%.4f] %.4f, %.4f) rectangle ([xshift=-%.4f] %.4f, %.4f); %%%n",
588 lineWidth[i], (color.getAlpha() / 255.0), colorString, currentMargin,
589 (currentBin.getStartBoundary() - XShift) * XScale, 0.0, currentMargin, (var - XShift) * XScale,
590 (currentBin.getCount() - YShift) * YScale);
593 "\\draw [line width=%.2fpt, color=%s] ([xshift=%.4f] %.4f, %.4f) rectangle ([xshift=-%.4f] %.4f, %.4f); %%%n",
594 lineWidth[i], colorString, currentMargin, (currentBin.getStartBoundary() - XShift) * XScale,
595 0.0, currentMargin, (var - XShift) * XScale, (currentBin.getCount() - YShift) * YScale);
597 }
else if ((currentBin.getStartBoundary() >= xmin && currentBin.getStartBoundary() <= xmax)
598 && (currentBin.getCount() >= ymin && currentBin.getCount() > ymax)) {
601 var = Math.min(currentBin.getEndBoundary(), xmax);
604 "\\filldraw [line width=%.2fpt, opacity=%.2f, color=%s] ([xshift=%.4f] %.4f, %.4f) rectangle ([xshift=-%.4f] %.4f, %.4f); %%%n",
605 lineWidth[i], (color.getAlpha() / 255.0), colorString, currentMargin,
606 (currentBin.getStartBoundary() - XShift) * XScale, 0.0, currentMargin, (var - XShift) * XScale,
607 (ymax - YShift) * YScale);
609 "\\draw [line width=%.2fpt, color=%s, style=dotted] ([xshift=%.4f] %.4f, %.4f) rectangle ([yshift=3mm, xshift=-%.4f] %.4f, %.4f); %%%n",
610 lineWidth[i], colorString, currentMargin, (currentBin.getStartBoundary() - XShift) * XScale,
611 (ymax - YShift) * YScale, currentMargin, (var - XShift) * XScale, (ymax - YShift) * YScale);
614 "\\draw [line width=%.2fpt, color=%s] ([xshift=%.4f] %.4f, %.4f) rectangle ([xshift=-%.4f] %.4f, %.4f); %%%n",
615 lineWidth[i], colorString, currentMargin, (currentBin.getStartBoundary() - XShift) * XScale,
616 0.0, currentMargin, (var - XShift) * XScale, (ymax - YShift) * YScale);
619 "\\draw [line width=%.2fpt, color=%s, style=dotted] ([xshift=%.4f] %.4f, %.4f) rectangle ([yshift=3mm, xshift=-%.4f] %.4f, %.4f); %%%n",
620 lineWidth[i], colorString, currentMargin, (currentBin.getStartBoundary() - XShift) * XScale,
621 (ymax - YShift) * YScale, currentMargin, (var - XShift) * XScale, (ymax - YShift) * YScale);
626 return formatter.toString();
A dataset that can be used for creating histograms.
void addSeries(Comparable key, double values[], int bins)
Adds a series to the dataset, using the specified number of bins.
int getSeriesCount()
Returns the number of series in the dataset.
List getBins(int series)
Returns the bins for a series.
List getBins(int series)
Returns the bins for a series.
CustomHistogramDataset getSeriesCollection()
Returns the CustomHistogramDataset object associated with the current variable.
int add(double[] data, int numPoints)
Adds a data series into the series collection.
void setBins(int series, HistogramBin[] binsTable)
Links bins given by table binsTable to a series.
double getMargin()
Returns the margin which is a percentage amount by which the bars are trimmed.
void setMargin(double margin)
Sets the margin which is a percentage amount by which the bars are trimmed for all series.
void setBins(int series, int bins)
Sets bins periodic bins from the observation minimum values to the observation maximum value for a se...
void setValues(int series, List valuesList)
Sets a new values set to a series from a List variable.
HistogramSeriesCollection()
Creates a new HistogramSeriesCollection instance with empty dataset.
boolean getFilled(int series)
Returns the filled flag associated with the series-th data series.
void setValues(int series, double[] values)
Sets a new values set to a series from a table.
void setOutlineWidth(int series, double outline)
Sets the outline width in pt.
void setBins(int series, int bins, double minimum, double maximum)
Sets bins periodic bins from minimum to maximum for a series.
int add(double[] data)
Adds a data series into the series collection.
List getValuesList(int series)
Returns the values for a series.
double[] getValues(int series)
Returns the values for a series.
double getOutlineWidth(int series)
Returns the outline width in pt.
void setFilled(int series, boolean filled)
Sets the filled flag.
String toLatex(double XScale, double YScale, double XShift, double YShift, double xmin, double xmax, double ymin, double ymax)
Formats and returns a string containing a LaTeX-compatible source code which represents this data ser...
int add(DoubleArrayList observationSet)
Adds a data series into the series collection.
HistogramSeriesCollection(TallyStore... tallies)
Creates a new HistogramSeriesCollection instance with default parameters and given data.
HistogramSeriesCollection(DoubleArrayList... data)
Creates a new HistogramSeriesCollection.
HistogramSeriesCollection(double[]... data)
Creates a new HistogramSeriesCollection instance with given data series.
HistogramSeriesCollection(double[] data, int numPoints)
Creates a new HistogramSeriesCollection instance with the given data series data.
HistogramSeriesCollection(CustomHistogramDataset data)
Creates a new HistogramSeriesCollection instance.
HistogramSeriesCollection(TallyHistogram... tallies)
Creates a new HistogramSeriesCollection instance with default parameters and given data.
int add(TallyStore tally)
Adds a data series into the series collection.
Stores data used in a XYChart.
static Color getDefaultColor(int index)
Gives the default color associated with a series.
static String detectXColorClassic(Color color)
Converts a java Color object into a friendly and readable LaTeX/xcolor string.
This class extends Tally.
int getNumBins()
Returns the number of bins .
int[] getCounters()
Returns the array of bin counters.
double getA()
Returns the left boundary of the interval .
double getB()
Returns the right boundary of the interval .
This class is a variant of Tally for which the individual observations are stored in a list implement...
double[] getArray()
Returns the observations stored in this probe.
int numberObs()
Returns the number of observations given to this probe since its last initialization.
This class provides various constants and methods to compute numerical quantities such as factorials,...
static double log2(double x)
Returns x .