OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
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 
17 BOOST_AUTO_TEST_CASE( google_chart_bar_string )
18 {
19  Vcluster & v_cl = create_vcluster();
20 
21  if (v_cl.getProcessUnitID() != 0)
22  return;
23 
25 
29 
30  x.add("colum1");
31  x.add("colum2");
32  x.add("colum3");
33  x.add("colum4");
34  x.add("colum5");
35  x.add("colum6");
36 
37  // Each colum can have multiple data set (in this case 4 dataset)
38  // Each dataset can have a name
39  yn.add("dataset1");
40  yn.add("dataset2");
41  yn.add("dataset3");
42  yn.add("dataset4");
43 
44  // Each colums can have multiple data-set
45  y.add({2,3,5,6});
46  y.add({5,6,1,6});
47  y.add({2,1,6,9});
48  y.add({1,6,3,2});
49  y.add({3,3,0,6});
50  y.add({2,1,4,6});
51 
52  // Google charts options
53  GCoptions options;
54 
55  options.title = std::string("Example");
56  options.yAxis = std::string("Y Axis");
57  options.xAxis = std::string("X Axis");
58  options.stype = std::string("bars");
59  options.barWD = true;
60 
61  // it say that the colum4 must me represented with a line
62  options.stypeext = std::string("{3: {type: 'line'}}");
63 
64  GoogleChart cg;
65  cg.AddHistGraph(x,y,yn,options);
66  cg.write("gc_out_sc.html");
67 
69 
70  bool test = compare("gc_out_sc.html","test_data/gc_out_sc_test.html");
71  BOOST_REQUIRE_EQUAL(true,test);
72 }
73 
74 
75 BOOST_AUTO_TEST_CASE( google_chart )
76 {
77  Vcluster & v_cl = create_vcluster();
78 
79  if (v_cl.getProcessUnitID() != 0)
80  return;
81 
83 
87 
88  x.add("colum1");
89  x.add("colum2");
90  x.add("colum3");
91  x.add("colum4");
92  x.add("colum5");
93  x.add("colum6");
94 
95  // Each colum can have multiple data set (in this case 4 dataset)
96  // Each dataset can have a name
97  yn.add("dataset1");
98  yn.add("dataset2");
99  yn.add("dataset3");
100  yn.add("dataset4");
101 
102  // Each colums can have multiple data-set
103  y.add({2,3,5,6});
104  y.add({5,6,1,6});
105  y.add({2,1,6,9});
106  y.add({1,6,3,2});
107  y.add({3,3,0,6});
108  y.add({2,1,4,6});
109 
110  // Google charts options
111  GCoptions options;
112 
113  options.title = std::string("Example");
114  options.yAxis = std::string("Y Axis");
115  options.xAxis = std::string("X Axis");
116  options.stype = std::string("bars");
117 
118  // it say that the colum4 must me represented with a line
119  options.stypeext = std::string("{3: {type: 'line'}}");
120 
121  GoogleChart cg;
122  cg.AddHistGraph(x,y,yn,options);
123  cg.write("gc_out.html");
124 
126 
127  bool test = compare("gc_out.html","gc_out_test.html");
128  BOOST_REQUIRE_EQUAL(true,test);
129 }
130 
131 BOOST_AUTO_TEST_CASE( google_chart2 )
132 {
133  Vcluster & v_cl = create_vcluster();
134 
135  if (v_cl.getProcessUnitID() != 0)
136  return;
137 
141 
142  x.add("colum1");
143  x.add("colum2");
144  x.add("colum3");
145  x.add("colum4");
146  x.add("colum5");
147  x.add("colum6");
148 
149  // Each colum can have multiple data set (in this case 4 dataset)
150  // Each dataset can have a name
151  yn.add("dataset1");
152  yn.add("dataset2");
153  yn.add("dataset3");
154  yn.add("dataset4");
155 
156  // Each colums can have multiple data-set
157  y.add({2.2,1.3,4.5,0.6});
158  y.add({5.0,6.1,1.3,2.6});
159  y.add({2.1,1.0,6.1,9.3});
160  y.add({1.1,6.1,3.0,2.0});
161  y.add({3.3,0.3,0.0,6.2});
162  y.add({2.0,1.1,4.0,6.1});
163 
164  // Google charts options
165  GCoptions options;
166 
167  options.title = std::string("Example");
168  options.yAxis = std::string("Y Axis");
169  options.xAxis = std::string("X Axis");
170  options.stype = std::string("bars");
171 
172  GoogleChart cg;
173  cg.AddHistGraph(x,y,yn,options);
174  cg.write("gc_out2.html");
175 
176  bool test = compare("gc_out2.html","gc_out2_test.html");
177  BOOST_REQUIRE_EQUAL(true,test);
178 }
179 
180 BOOST_AUTO_TEST_CASE( google_chart3 )
181 {
182  Vcluster & v_cl = create_vcluster();
183 
184  if (v_cl.getProcessUnitID() != 0)
185  return;
186 
190 
191  x.add("colum1");
192  x.add("colum2");
193  x.add("colum3");
194  x.add("colum4");
195  x.add("colum5");
196  x.add("colum6");
197 
198  // Each colum can have multiple data set (in this case 4 dataset)
199  // Each dataset can have a name
200  yn.add("dataset1");
201  yn.add("dataset2");
202  yn.add("dataset3");
203  yn.add("dataset4");
204 
205  // Each colums can have multiple data-set
206  y.add({2.2,1.3,4.5,0.6});
207  y.add({5.0,6.1,1.3,2.6});
208  y.add({2.1,1.0,6.1,9.3});
209  y.add({1.1,6.1,3.0,2.0});
210  y.add({3.3,0.3,0.0,6.2});
211  y.add({2.0,1.1,4.0,6.1});
212 
213  // Google charts options
214  GCoptions options;
215 
216  options.title = std::string("Example");
217  options.yAxis = std::string("Y Axis");
218  options.xAxis = std::string("X Axis");
219 
220  GoogleChart cg;
221  cg.AddHistGraph(x,y,yn,options);
222  cg.write("gc_out3.html");
223 
224  bool test = compare("gc_out3.html","gc_out3_test.html");
225  BOOST_REQUIRE_EQUAL(true,test);
226 }
227 
228 BOOST_AUTO_TEST_CASE( google_chart4 )
229 {
230  Vcluster & v_cl = create_vcluster();
231 
232  if (v_cl.getProcessUnitID() != 0)
233  return;
234 
238 
239  x.add("colum1");
240  x.add("colum2");
241  x.add("colum3");
242  x.add("colum4");
243  x.add("colum5");
244  x.add("colum6");
245 
246  // Each colum can have multiple data set (in this case 4 dataset)
247  // Each dataset can have a name
248  yn.add("dataset1");
249  yn.add("dataset2");
250  yn.add("dataset3");
251  yn.add("dataset4");
252 
253  // Each colums can have multiple data-set
254  y.add({2.2,1.3,4.5,0.6});
255  y.add({5.0,6.1,1.3,2.6});
256  y.add({2.1,1.0,6.1,9.3});
257  y.add({1.1,6.1,3.0,2.0});
258  y.add({3.3,0.3,0.0,6.2});
259  y.add({2.0,1.1,4.0,6.1});
260 
261  GoogleChart cg;
262  cg.AddHistGraph(x,y,yn);
263  cg.write("gc_out4.html");
264 
265  bool test = compare("gc_out4.html","gc_out4_test.html");
266  BOOST_REQUIRE_EQUAL(true,test);
267 }
268 
269 BOOST_AUTO_TEST_CASE( google_chart5 )
270 {
271  Vcluster & v_cl = create_vcluster();
272 
273  if (v_cl.getProcessUnitID() != 0)
274  return;
275 
278 
279  x.add("colum1");
280  x.add("colum2");
281  x.add("colum3");
282  x.add("colum4");
283  x.add("colum5");
284  x.add("colum6");
285 
286  // Each colums can have multiple data-set
287  y.add({2.2,1.3,4.5,0.6});
288  y.add({5.0,6.1,1.3,2.6});
289  y.add({2.1,1.0,6.1,9.3});
290  y.add({1.1,6.1,3.0,2.0});
291  y.add({3.3,0.3,0.0,6.2});
292  y.add({2.0,1.1,4.0,6.1});
293 
294  GoogleChart cg;
295  cg.AddHistGraph(x,y);
296  cg.write("gc_out5.html");
297 
298  bool test = compare("gc_out5.html","gc_out5_test.html");
299  BOOST_REQUIRE_EQUAL(true,test);
300 }
301 
302 BOOST_AUTO_TEST_CASE( google_chart6 )
303 {
304  Vcluster & v_cl = create_vcluster();
305 
306  if (v_cl.getProcessUnitID() != 0)
307  return;
308 
310 
311  // Each colums can have multiple data-set
312  y.add({2.2,1.3,4.5,0.6});
313  y.add({5.0,6.1,1.3,2.6});
314  y.add({2.1,1.0,6.1,9.3});
315  y.add({1.1,6.1,3.0,2.0});
316  y.add({3.3,0.3,0.0,6.2});
317  y.add({2.0,1.1,4.0,6.1});
318 
319  GoogleChart cg;
320  cg.AddHistGraph(y);
321  cg.write("gc_out6.html");
322 
323  bool test = compare("gc_out6.html","gc_out6_test.html");
324  BOOST_REQUIRE_EQUAL(true,test);
325 }
326 
327 BOOST_AUTO_TEST_CASE( google_chart_with_inject_HTML )
328 {
329  Vcluster & v_cl = create_vcluster();
330 
331  if (v_cl.getProcessUnitID() != 0)
332  return;
333 
335 
339 
340  x.add("colum1");
341  x.add("colum2");
342  x.add("colum3");
343  x.add("colum4");
344  x.add("colum5");
345  x.add("colum6");
346 
347  // Each colum can have multiple data set (in this case 4 dataset)
348  // Each dataset can have a name
349  yn.add("dataset1");
350  yn.add("dataset2");
351  yn.add("dataset3");
352  yn.add("dataset4");
353 
354  // Each colums can have multiple data-set
355  y.add({2,3,5,6});
356  y.add({5,6,1,6});
357  y.add({2,1,6,9});
358  y.add({1,6,3,2});
359  y.add({3,3,0,6});
360  y.add({2,1,4,6});
361 
362  // Google charts options
363  GCoptions options;
364 
365  options.title = std::string("Example");
366  options.yAxis = std::string("Y Axis");
367  options.xAxis = std::string("X Axis");
368  options.stype = std::string("bars");
369 
370  // it say that the colum4 must me represented with a line
371  options.stypeext = std::string("{3: {type: 'line'}}");
372 
373  GoogleChart cg;
374  //
375  cg.addHTML("<h2>Before first graph</h2>");
376  cg.AddHistGraph(x,y,yn,options);
377  cg.addHTML("<h2>Before second graph</h2>");
378  cg.AddHistGraph(x,y,yn,options);
379  cg.addHTML("<h2>Before third graph</h2>");
380  cg.AddHistGraph(x,y,yn,options);
381  cg.addHTML("<h2>At the end</h2>");
382  cg.write("gc_out7.html");
383 
385 
386  bool test = compare("gc_out7.html","gc_out7_test.html");
387  BOOST_REQUIRE_EQUAL(true,test);
388 }
389 
390 BOOST_AUTO_TEST_CASE( google_chart_linear_plot )
391 {
392  Vcluster & v_cl = create_vcluster();
393 
394  if (v_cl.getProcessUnitID() != 0)
395  return;
396 
398 
402 
403  x.add("colum1");
404  x.add("colum2");
405  x.add("colum3");
406  x.add("colum4");
407  x.add("colum5");
408  x.add("colum6");
409 
410  // Here we specify how many lines we have
411  // first Line
412  yn.add("line1");
413 
414  // second line + 2 intervals (Error bands)
415  yn.add("line2");
416  yn.add("interval");
417  yn.add("interval");
418  yn.add("interval");
419  yn.add("interval");
420 
421  // third line + 1 interval (Error bands)
422  yn.add("line3");
423  yn.add("interval");
424  yn.add("interval");
425 
426  // Each line can have multiple intervals or error bars
427  // The first number specify the bottom line
428  // The last three numbers specify the top line + error band (min, max)
429  // The middle 5 line specify the middle lines + one external error band + one internal error band
430 
431  y.add({0.10,0.20,0.19,0.22,0.195,0.215,0.35,0.34,0.36});
432  y.add({0.11,0.21,0.18,0.22,0.19,0.215,0.36,0.35,0.37});
433  y.add({0.12,0.22,0.21,0.23,0.215,0.225,0.35,0.34,0.36});
434  y.add({0.15,0.25,0.20,0.26,0.22,0.255,0.36,0.35,0.37});
435  y.add({0.09,0.29,0.25,0.30,0.26,0.295,0.35,0.34,0.36});
436  y.add({0.08,0.28,0.27,0.29,0.275,0.285,0.36,0.35,0.37});
437 
438  // Google charts options
439  GCoptions options;
440 
441  options.title = std::string("Example");
442  options.yAxis = std::string("Y Axis");
443  options.xAxis = std::string("X Axis");
444  options.lineWidth = 1.0;
445  options.intervalext = std::string("{'i2': { 'color': '#4374E0', 'style':'bars', 'lineWidth':4, 'fillOpacity':1 } }");
446 
447  GoogleChart cg;
448  cg.AddLinesGraph(x,y,yn,options);
449  cg.write("gc_plot_out.html");
450 
452 
453  bool test = compare("gc_plot_out.html","gc_plot_out_test.html");
454  BOOST_REQUIRE_EQUAL(true,test);
455 }
456 
457 BOOST_AUTO_TEST_CASE( google_chart_linear_plot2 )
458 {
459  Vcluster & v_cl = create_vcluster();
460 
461  if (v_cl.getProcessUnitID() != 0)
462  return;
463 
465 
468 
469  x.add("colum1");
470  x.add("colum2");
471  x.add("colum3");
472  x.add("colum4");
473  x.add("colum5");
474  x.add("colum6");
475 
476  // Each line can have multiple intervals or error bars
477  // The first number specify the bottom line
478  // The last three numbers specify the top line + error band (min, max)
479  // The middle 5 line specify the middle lines + one external error band + one internal error band
480 
481  y.add({0.10,0.20,0.19,0.22,0.195,0.215,0.35,0.34,0.36});
482  y.add({0.11,0.21,0.18,0.22,0.19,0.215,0.36,0.35,0.37});
483  y.add({0.12,0.22,0.21,0.23,0.215,0.225,0.35,0.34,0.36});
484  y.add({0.15,0.25,0.20,0.26,0.22,0.255,0.36,0.35,0.37});
485  y.add({0.09,0.29,0.25,0.30,0.26,0.295,0.35,0.34,0.36});
486  y.add({0.08,0.28,0.27,0.29,0.275,0.285,0.36,0.35,0.37});
487 
488  // Google charts options
489  GCoptions options;
490 
491  options.title = std::string("Example");
492  options.yAxis = std::string("Y Axis");
493  options.xAxis = std::string("X Axis");
494  options.lineWidth = 1.0;
495 
496  GoogleChart cg;
497  cg.AddLinesGraph(x,y,options);
498  cg.write("gc_plot2_out.html");
499 
501 
502  bool test = compare("gc_plot2_out.html","gc_plot2_out_test.html");
503  BOOST_REQUIRE_EQUAL(true,test);
504 }
505 
507 
508 double f(double x)
509 {
510  return x*x;
511 }
512 
514 
515 BOOST_AUTO_TEST_CASE( plot_util )
516 {
517  Vcluster & v_cl = create_vcluster();
518 
519  if (v_cl.getProcessUnitID() != 0)
520  return;
521 
523 
525 
526  Fill1D(0.0,2.0,5,x);
527 
528  BOOST_REQUIRE_EQUAL(x.get(0),0.0);
529  BOOST_REQUIRE_EQUAL(x.get(1),0.5);
530  BOOST_REQUIRE_EQUAL(x.get(2),1.0);
531  BOOST_REQUIRE_EQUAL(x.get(3),1.5);
532  BOOST_REQUIRE_EQUAL(x.get(4),2.0);
533 
535 
536  x.clear();
537 
539 
540  Fill1D(0.0,2.0,5,x,f);
541 
542  BOOST_REQUIRE_EQUAL(x.get(0),0.0);
543  BOOST_REQUIRE_EQUAL(x.get(1),0.25);
544  BOOST_REQUIRE_EQUAL(x.get(2),1.0);
545  BOOST_REQUIRE_EQUAL(x.get(3),2.25);
546  BOOST_REQUIRE_EQUAL(x.get(4),4.0);
547 
549 }
550 
551 BOOST_AUTO_TEST_SUITE_END()
552 
553 #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.
size_t getProcessUnitID()
Get the process unit id.
std::string title
Title of the chart.
Definition: GoogleChart.hpp:27
std::string stypeext
Definition: GoogleChart.hpp:42
Implementation of VCluster class.
Definition: VCluster.hpp:36
std::string yAxis
Y axis name.
Definition: GoogleChart.hpp:29
Small class to produce graph with Google chart in HTML.
std::string intervalext
Definition: GoogleChart.hpp:63
size_t lineWidth
Width of the line.
Definition: GoogleChart.hpp:55
bool barWD
barWD
Definition: GoogleChart.hpp:72
void write(std::string file)
It write the graphs on file in html format using Google charts.
std::string stype
Definition: GoogleChart.hpp:37
void AddHistGraph(openfpm::vector< Y > &y)
Add an histogram graph.
std::string xAxis
X axis name.
Definition: GoogleChart.hpp:31
Google chart options.
Definition: GoogleChart.hpp:24