SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
HistogramChartToLatex.java
1package umontreal.ssj.stat;
2
3//import org.jfree.chart.ChartFactory;
4//import org.jfree.chart.ChartPanel;
5//import org.jfree.chart.axis.NumberAxis;
6//import org.jfree.chart.plot.XYPlot;
7//import org.jfree.chart.plot.PlotOrientation;
8//import org.jfree.data.statistics.HistogramBin;
9//import org.omg.CORBA.portable.OutputStream;
10//import cern.colt.list.DoubleArrayList;
11//import java.util.ListIterator;
12import java.util.Locale;
13import java.io.File;
14import java.io.FileOutputStream;
15import java.io.IOException;
16import java.io.OutputStreamWriter;
17import java.io.PrintWriter;
18import java.util.Formatter;
19
21
22 /*
23 * return the min of the array of height
24 *
25 */
26 public double getMinY(double[] height) {
27 double min = height[0];
28 for (int i = 1; i < height.length; i++) {
29 if (height[i] < min)
30 min = height[i];
31 }
32 return min;
33 }
34
35 /*
36 *
37 * Return the max of array
38 */
39 public double getMaxY(double[] height) {
40 double max = height[0];
41 for (int i = 1; i < height.length; i++) {
42 if (height[i] > max)
43 max = height[i];
44 }
45 return max;
46 }
47
48 /*
49 *
50 * return a array that contain the bound of the histogram with an TallyHistogram
51 * object
52 *
53 * @param scaledH Scaledhistogram
54 */
55 public double[] getHistogramBound(ScaledHistogram scaledH) {
56 double a = scaledH.getA();
57 double b = scaledH.getB();
58 int nBin = scaledH.getNumBins();
59 double h = (b - a) / nBin;
60 double bound[] = new double[nBin + 1];
61 bound[0] = a;
62 for (int i = 1; i <= nBin; i++)
63 bound[i] = bound[i - 1] + h;
64 return bound;
65 }
66
67 /*
68 *
69 * return tex file for an histogram by using a Scaledhistogram object and can
70 * add the polygonal histogram if the flag 'poly' is false.
71 *
72 * @param scaledH a ScaledHistogram object
73 *
74 * @param poly Plots a polygonal (piecewise linear) interpolation of the
75 * histogram if 'poly=true'
76 *
77 * @param hist Plot the histogram if 'hist=true'
78 *
79 * If 'poly=true' and 'hist=false', the method plot only the Polygonal
80 * interpolation of the histogram If 'poly=false' and 'hist=true', the method
81 * plot only the Histogram 'poly=true' and 'hist=true', the plot the histogram
82 * and the the polygonal We need to plot some things. If 'poly=false' and
83 * 'hist=false', the method plot the histogram only.
84 *
85 *
86 */
87
88 public String toLatex(ScaledHistogram scaledH, boolean poly, boolean hist) {
89
90 double height[] = scaledH.getHeights();
91 double bound[] = getHistogramBound(scaledH);
92 return toLatex(bound, height, poly, hist);
93
94 }
95
96 /*
97 *
98 *
99 * return tex file for an histogram by using the array of bound and the array of
100 * height of the histogram
101 *
102 * @param bound The array that contain the bound of the histogram
103 *
104 * @param height The array of rescaled counters: height[j] is the height of bin
105 * j.
106 *
107 * @param poly Plots a polygonal (piecewise linear) interpolation of the
108 * histogram if 'poly=true'
109 *
110 * @param hist Plot the histogram if 'hist=true'
111 *
112 *
113 * If 'poly=true' and 'hist=false', the method plot only the Polygonal
114 * interpolation of the histogram If 'poly=false' and 'hist=true', the method
115 * plot only the Histogram 'poly=true' and 'hist=true', the method plot the
116 * histogram and the the polygonal We need to plot some things. If 'poly=false'
117 * and 'hist=false', the method plot the histogram only.
118 *
119 *
120 */
121 public String toLatex(double[] bound, double[] height, boolean poly, boolean hist) {
122 if (poly == false && hist == false) {
123 hist = true;
124 }
125
126 double h = (bound[1] - bound[0]) / 2;
127 double yMin = getMinY(height);
128 double yMax = getMaxY(height) + 0.2 * getMaxY(height);
129 Formatter formatter = new Formatter(Locale.US);
130 formatter.format("\\documentclass[border=3mm, %n");
131 formatter.format(" tikz, %n");
132 formatter.format(" preview %n");
133 formatter.format(" ]{standalone} %n%n");
134 formatter.format("\\usepackage{pgfplots}");
135 formatter.format("%n%n");
136 formatter.format("\\begin{document}%n%n");
137 formatter.format("%%---------------------------------------------------------------%%%n");
138 formatter.format("\\begin{tikzpicture} %n%n");
139 formatter.format("\\begin{axis}[ %n");
140 formatter.format(" ymin=%s, ymax=%s,%n", yMin, yMax);
141 formatter.format(" minor y tick num = 3, %n");
142 formatter.format(" %%area style, %n");
143 formatter.format(" ] %n");
144 if (hist) {
145 formatter.format("\\addplot+[ybar interval,mark=no] plot coordinates { ");
146 for (int i = 0; i < height.length; i++)
147 formatter.format("\n (%s,%s) ", bound[i], height[i]);
148 formatter.format("};%n%n");
149 }
150 if (poly) {
151 formatter.format("\\addplot+[sharp plot] plot coordinates { ");
152 formatter.format("(%s,%s) ", bound[0], height[0]);
153 for (int i = 0; i < height.length; i++)
154 formatter.format("\n (%s,%s) ", bound[i] + h, height[i]);
155 formatter.format("(%s,%s) ", bound[bound.length - 1], height[height.length - 1]);
156 formatter.format("};%n%n");
157 }
158 formatter.format("\\end{axis} %n%n");
159 formatter.format("\\end{tikzpicture} %n%n");
160 formatter.format("\\end{document}%n");
161
162 String ch = formatter.toString();
163 formatter.close();
164 return ch;
165 }
166
167 /*
168 *
169 *
170 * write a string in a file
171 *
172 * @param name The name of the generate tex file. You can find it in your
173 * current working Directory you must change this path
174 *
175 * @param chaine is the String to write
176 */
177 public void writeStringTofile(String name, String chaine) throws IOException {
178 String currentDir = System.getProperty("user.dir");
179 File file = new File(currentDir + "/" + name + ".tex");
180 if (file.exists()) {
181 file.delete(); // you might want to check if delete was successfull
182 }
183 file.createNewFile();
184 final FileOutputStream stream = new FileOutputStream(file, true);
185 final PrintWriter out = new PrintWriter(new OutputStreamWriter(stream));
186 out.println(chaine); // Ajouter
187 out.close();
188 }
189
190}
This class provides histograms for which the bin counts (heights of rectangles) are replaced by real-...
double[] getHeights()
return the array counts of the histogram.
double getB()
Returns the right boundary of interval .
int getNumBins()
Returns the number of bins dividing the interval.
double getA()
Returns the left boundary of interval .