OpenFPM_io  0.2.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Functions Variables Typedefs
Plot_unit_tests.hpp
1 /*
2  * Plot_unit_tests.hpp
3  *
4  * Created on: Jan 9, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_PLOT_PLOT_UNIT_TESTS_HPP_
9 #define OPENFPM_DATA_SRC_PLOT_PLOT_UNIT_TESTS_HPP_
10 
11 #include "GoogleChart.hpp"
12 #include "Plot/util.hpp"
13 
14 BOOST_AUTO_TEST_SUITE( plot_unit_test )
15 
16 BOOST_AUTO_TEST_CASE( google_chart )
17 {
19 
20  openfpm::vector<std::string> x;
21  openfpm::vector<openfpm::vector<size_t>> y;
22  openfpm::vector<std::string> yn;
23 
24  x.add("colum1");
25  x.add("colum2");
26  x.add("colum3");
27  x.add("colum4");
28  x.add("colum5");
29  x.add("colum6");
30 
31  // Each colum can have multiple data set (in this case 4 dataset)
32  // Each dataset can have a name
33  yn.add("dataset1");
34  yn.add("dataset2");
35  yn.add("dataset3");
36  yn.add("dataset4");
37 
38  // Each colums can have multiple data-set
39  y.add({2,3,5,6});
40  y.add({5,6,1,6});
41  y.add({2,1,6,9});
42  y.add({1,6,3,2});
43  y.add({3,3,0,6});
44  y.add({2,1,4,6});
45 
46  // Google charts options
47  GCoptions options;
48 
49  options.title = std::string("Example");
50  options.yAxis = std::string("Y Axis");
51  options.xAxis = std::string("X Axis");
52  options.stype = std::string("bars");
53 
54  // it say that the colum4 must me represented with a line
55  options.stypeext = std::string("{3: {type: 'line'}}");
56 
57  GoogleChart cg;
58  cg.AddHistGraph(x,y,yn,options);
59  cg.write("gc_out.html");
60 
62 
63  bool test = compare("gc_out.html","gc_out_test.html");
64  BOOST_REQUIRE_EQUAL(true,test);
65 }
66 
67 BOOST_AUTO_TEST_CASE( google_chart2 )
68 {
69  openfpm::vector<std::string> x;
70  openfpm::vector<openfpm::vector<float>> y;
71  openfpm::vector<std::string> yn;
72 
73  x.add("colum1");
74  x.add("colum2");
75  x.add("colum3");
76  x.add("colum4");
77  x.add("colum5");
78  x.add("colum6");
79 
80  // Each colum can have multiple data set (in this case 4 dataset)
81  // Each dataset can have a name
82  yn.add("dataset1");
83  yn.add("dataset2");
84  yn.add("dataset3");
85  yn.add("dataset4");
86 
87  // Each colums can have multiple data-set
88  y.add({2.2,1.3,4.5,0.6});
89  y.add({5.0,6.1,1.3,2.6});
90  y.add({2.1,1.0,6.1,9.3});
91  y.add({1.1,6.1,3.0,2.0});
92  y.add({3.3,0.3,0.0,6.2});
93  y.add({2.0,1.1,4.0,6.1});
94 
95  // Google charts options
96  GCoptions options;
97 
98  options.title = std::string("Example");
99  options.yAxis = std::string("Y Axis");
100  options.xAxis = std::string("X Axis");
101  options.stype = std::string("bars");
102 
103  GoogleChart cg;
104  cg.AddHistGraph(x,y,yn,options);
105  cg.write("gc_out2.html");
106 
107  bool test = compare("gc_out2.html","gc_out2_test.html");
108  BOOST_REQUIRE_EQUAL(true,test);
109 }
110 
111 BOOST_AUTO_TEST_CASE( google_chart3 )
112 {
113  openfpm::vector<std::string> x;
114  openfpm::vector<openfpm::vector<float>> y;
115  openfpm::vector<std::string> yn;
116 
117  x.add("colum1");
118  x.add("colum2");
119  x.add("colum3");
120  x.add("colum4");
121  x.add("colum5");
122  x.add("colum6");
123 
124  // Each colum can have multiple data set (in this case 4 dataset)
125  // Each dataset can have a name
126  yn.add("dataset1");
127  yn.add("dataset2");
128  yn.add("dataset3");
129  yn.add("dataset4");
130 
131  // Each colums can have multiple data-set
132  y.add({2.2,1.3,4.5,0.6});
133  y.add({5.0,6.1,1.3,2.6});
134  y.add({2.1,1.0,6.1,9.3});
135  y.add({1.1,6.1,3.0,2.0});
136  y.add({3.3,0.3,0.0,6.2});
137  y.add({2.0,1.1,4.0,6.1});
138 
139  // Google charts options
140  GCoptions options;
141 
142  options.title = std::string("Example");
143  options.yAxis = std::string("Y Axis");
144  options.xAxis = std::string("X Axis");
145 
146  GoogleChart cg;
147  cg.AddHistGraph(x,y,yn,options);
148  cg.write("gc_out3.html");
149 
150  bool test = compare("gc_out3.html","gc_out3_test.html");
151  BOOST_REQUIRE_EQUAL(true,test);
152 }
153 
154 BOOST_AUTO_TEST_CASE( google_chart4 )
155 {
156  openfpm::vector<std::string> x;
157  openfpm::vector<openfpm::vector<float>> y;
158  openfpm::vector<std::string> yn;
159 
160  x.add("colum1");
161  x.add("colum2");
162  x.add("colum3");
163  x.add("colum4");
164  x.add("colum5");
165  x.add("colum6");
166 
167  // Each colum can have multiple data set (in this case 4 dataset)
168  // Each dataset can have a name
169  yn.add("dataset1");
170  yn.add("dataset2");
171  yn.add("dataset3");
172  yn.add("dataset4");
173 
174  // Each colums can have multiple data-set
175  y.add({2.2,1.3,4.5,0.6});
176  y.add({5.0,6.1,1.3,2.6});
177  y.add({2.1,1.0,6.1,9.3});
178  y.add({1.1,6.1,3.0,2.0});
179  y.add({3.3,0.3,0.0,6.2});
180  y.add({2.0,1.1,4.0,6.1});
181 
182  GoogleChart cg;
183  cg.AddHistGraph(x,y,yn);
184  cg.write("gc_out4.html");
185 
186  bool test = compare("gc_out4.html","gc_out4_test.html");
187  BOOST_REQUIRE_EQUAL(true,test);
188 }
189 
190 BOOST_AUTO_TEST_CASE( google_chart5 )
191 {
192  openfpm::vector<std::string> x;
193  openfpm::vector<openfpm::vector<float>> y;
194 
195  x.add("colum1");
196  x.add("colum2");
197  x.add("colum3");
198  x.add("colum4");
199  x.add("colum5");
200  x.add("colum6");
201 
202  // Each colums can have multiple data-set
203  y.add({2.2,1.3,4.5,0.6});
204  y.add({5.0,6.1,1.3,2.6});
205  y.add({2.1,1.0,6.1,9.3});
206  y.add({1.1,6.1,3.0,2.0});
207  y.add({3.3,0.3,0.0,6.2});
208  y.add({2.0,1.1,4.0,6.1});
209 
210  GoogleChart cg;
211  cg.AddHistGraph(x,y);
212  cg.write("gc_out5.html");
213 
214  bool test = compare("gc_out5.html","gc_out5_test.html");
215  BOOST_REQUIRE_EQUAL(true,test);
216 }
217 
218 BOOST_AUTO_TEST_CASE( google_chart6 )
219 {
220  openfpm::vector<openfpm::vector<float>> y;
221 
222  // Each colums can have multiple data-set
223  y.add({2.2,1.3,4.5,0.6});
224  y.add({5.0,6.1,1.3,2.6});
225  y.add({2.1,1.0,6.1,9.3});
226  y.add({1.1,6.1,3.0,2.0});
227  y.add({3.3,0.3,0.0,6.2});
228  y.add({2.0,1.1,4.0,6.1});
229 
230  GoogleChart cg;
231  cg.AddHistGraph(y);
232  cg.write("gc_out6.html");
233 
234  bool test = compare("gc_out6.html","gc_out6_test.html");
235  BOOST_REQUIRE_EQUAL(true,test);
236 }
237 
238 BOOST_AUTO_TEST_CASE( google_chart_with_inject_HTML )
239 {
241 
242  openfpm::vector<std::string> x;
243  openfpm::vector<openfpm::vector<size_t>> y;
244  openfpm::vector<std::string> yn;
245 
246  x.add("colum1");
247  x.add("colum2");
248  x.add("colum3");
249  x.add("colum4");
250  x.add("colum5");
251  x.add("colum6");
252 
253  // Each colum can have multiple data set (in this case 4 dataset)
254  // Each dataset can have a name
255  yn.add("dataset1");
256  yn.add("dataset2");
257  yn.add("dataset3");
258  yn.add("dataset4");
259 
260  // Each colums can have multiple data-set
261  y.add({2,3,5,6});
262  y.add({5,6,1,6});
263  y.add({2,1,6,9});
264  y.add({1,6,3,2});
265  y.add({3,3,0,6});
266  y.add({2,1,4,6});
267 
268  // Google charts options
269  GCoptions options;
270 
271  options.title = std::string("Example");
272  options.yAxis = std::string("Y Axis");
273  options.xAxis = std::string("X Axis");
274  options.stype = std::string("bars");
275 
276  // it say that the colum4 must me represented with a line
277  options.stypeext = std::string("{3: {type: 'line'}}");
278 
279  GoogleChart cg;
280  //
281  cg.addHTML("<h2>Before first graph</h2>");
282  cg.AddHistGraph(x,y,yn,options);
283  cg.addHTML("<h2>Before second graph</h2>");
284  cg.AddHistGraph(x,y,yn,options);
285  cg.addHTML("<h2>Before third graph</h2>");
286  cg.AddHistGraph(x,y,yn,options);
287  cg.addHTML("<h2>At the end</h2>");
288  cg.write("gc_out7.html");
289 
291 
292  bool test = compare("gc_out7.html","gc_out7_test.html");
293  BOOST_REQUIRE_EQUAL(true,test);
294 }
295 
296 BOOST_AUTO_TEST_CASE( google_chart_linear_plot )
297 {
299 
300  openfpm::vector<std::string> x;
301  openfpm::vector<openfpm::vector<double>> y;
302  openfpm::vector<std::string> yn;
303 
304  x.add("colum1");
305  x.add("colum2");
306  x.add("colum3");
307  x.add("colum4");
308  x.add("colum5");
309  x.add("colum6");
310 
311  // Here we specify how many lines we have
312  // first Line
313  yn.add("line1");
314 
315  // second line + 2 intervals (Error bands)
316  yn.add("line2");
317  yn.add("interval");
318  yn.add("interval");
319  yn.add("interval");
320  yn.add("interval");
321 
322  // third line + 1 interval (Error bands)
323  yn.add("line3");
324  yn.add("interval");
325  yn.add("interval");
326 
327  // Each line can have multiple intervals or error bars
328  // The first number specify the bottom line
329  // The last three numbers specify the top line + error band (min, max)
330  // The middle 5 line specify the middle lines + one external error band + one internal error band
331 
332  y.add({0.10,0.20,0.19,0.22,0.195,0.215,0.35,0.34,0.36});
333  y.add({0.11,0.21,0.18,0.22,0.19,0.215,0.36,0.35,0.37});
334  y.add({0.12,0.22,0.21,0.23,0.215,0.225,0.35,0.34,0.36});
335  y.add({0.15,0.25,0.20,0.26,0.22,0.255,0.36,0.35,0.37});
336  y.add({0.09,0.29,0.25,0.30,0.26,0.295,0.35,0.34,0.36});
337  y.add({0.08,0.28,0.27,0.29,0.275,0.285,0.36,0.35,0.37});
338 
339  // Google charts options
340  GCoptions options;
341 
342  options.title = std::string("Example");
343  options.yAxis = std::string("Y Axis");
344  options.xAxis = std::string("X Axis");
345  options.lineWidth = 1.0;
346  options.intervalext = std::string("{'i2': { 'color': '#4374E0', 'style':'bars', 'lineWidth':4, 'fillOpacity':1 } }");
347 
348  GoogleChart cg;
349  cg.AddLinesGraph(x,y,yn,options);
350  cg.write("gc_plot_out.html");
351 
353 
354  bool test = compare("gc_plot_out.html","gc_plot_out_test.html");
355  BOOST_REQUIRE_EQUAL(true,test);
356 }
357 
358 BOOST_AUTO_TEST_CASE( google_chart_linear_plot2 )
359 {
361 
362  openfpm::vector<std::string> x;
363  openfpm::vector<openfpm::vector<double>> y;
364 
365  x.add("colum1");
366  x.add("colum2");
367  x.add("colum3");
368  x.add("colum4");
369  x.add("colum5");
370  x.add("colum6");
371 
372  // Each line can have multiple intervals or error bars
373  // The first number specify the bottom line
374  // The last three numbers specify the top line + error band (min, max)
375  // The middle 5 line specify the middle lines + one external error band + one internal error band
376 
377  y.add({0.10,0.20,0.19,0.22,0.195,0.215,0.35,0.34,0.36});
378  y.add({0.11,0.21,0.18,0.22,0.19,0.215,0.36,0.35,0.37});
379  y.add({0.12,0.22,0.21,0.23,0.215,0.225,0.35,0.34,0.36});
380  y.add({0.15,0.25,0.20,0.26,0.22,0.255,0.36,0.35,0.37});
381  y.add({0.09,0.29,0.25,0.30,0.26,0.295,0.35,0.34,0.36});
382  y.add({0.08,0.28,0.27,0.29,0.275,0.285,0.36,0.35,0.37});
383 
384  // Google charts options
385  GCoptions options;
386 
387  options.title = std::string("Example");
388  options.yAxis = std::string("Y Axis");
389  options.xAxis = std::string("X Axis");
390  options.lineWidth = 1.0;
391 
392  GoogleChart cg;
393  cg.AddLinesGraph(x,y,options);
394  cg.write("gc_plot2_out.html");
395 
397 
398  bool test = compare("gc_plot2_out.html","gc_plot2_out_test.html");
399  BOOST_REQUIRE_EQUAL(true,test);
400 }
401 
403 
404 double f(double x)
405 {
406  return x*x;
407 }
408 
410 
411 BOOST_AUTO_TEST_CASE( plot_util )
412 {
414 
415  openfpm::vector<double> x;
416 
417  Fill1D(0.0,2.0,5,x);
418 
419  BOOST_REQUIRE_EQUAL(x.get(0),0.0);
420  BOOST_REQUIRE_EQUAL(x.get(1),0.5);
421  BOOST_REQUIRE_EQUAL(x.get(2),1.0);
422  BOOST_REQUIRE_EQUAL(x.get(3),1.5);
423  BOOST_REQUIRE_EQUAL(x.get(4),2.0);
424 
426 
427  x.clear();
428 
430 
431  Fill1D(0.0,2.0,5,x,f);
432 
433  BOOST_REQUIRE_EQUAL(x.get(0),0.0);
434  BOOST_REQUIRE_EQUAL(x.get(1),0.25);
435  BOOST_REQUIRE_EQUAL(x.get(2),1.0);
436  BOOST_REQUIRE_EQUAL(x.get(3),2.25);
437  BOOST_REQUIRE_EQUAL(x.get(4),4.0);
438 
440 }
441 
442 BOOST_AUTO_TEST_SUITE_END()
443 
444 #endif /* OPENFPM_DATA_SRC_PLOT_PLOT_UNIT_TESTS_HPP_ */
void addHTML(const std::string &html)
Add HTML text.
void AddLinesGraph(openfpm::vector< X > &x, openfpm::vector< Y > &y, const GCoptions &opt)
Add a simple lines graph.
std::string title
Title of the chart.
Definition: GoogleChart.hpp:23
std::string stypeext
Definition: GoogleChart.hpp:38
std::string yAxis
Y axis name.
Definition: GoogleChart.hpp:25
Small class to produce graph with Google chart in HTML.
std::string intervalext
Definition: GoogleChart.hpp:56
size_t lineWidth
Width of the line.
Definition: GoogleChart.hpp:48
void write(std::string file)
It write the graphs on file in html format using Google charts.
std::string stype
Definition: GoogleChart.hpp:33
void AddHistGraph(openfpm::vector< Y > &y)
Add an histogram graph.
std::string xAxis
X axis name.
Definition: GoogleChart.hpp:27
Google chart options.
Definition: GoogleChart.hpp:20