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
GoogleChart.hpp
1 /*
2  * GoogleChart.hpp
3  *
4  * Created on: Jan 9, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_PLOT_GOOGLECHART_HPP_
9 #define OPENFPM_DATA_SRC_PLOT_GOOGLECHART_HPP_
10 
11 #include <fstream>
12 #include "Vector/map_vector.hpp"
13 
14 #define GGRAPH_COLUMS 1
15 #define GGRAPH_POINTS 2
16 
20 struct GCoptions
21 {
23  std::string title;
25  std::string yAxis;
27  std::string xAxis;
28 
33  std::string stype;
34 
38  std::string stypeext;
39 
40  size_t width=900;
41  size_t heigh=500;
42 
45  bool isStacked = false;
46 
48  size_t lineWidth = 4;
49 
52  std::string intervalsext;
53 
56  std::string intervalext;
57 
59  std::string more;
60 
61  GCoptions & operator=(const GCoptions & opt)
62  {
63  title = opt.title;
64  yAxis = opt.yAxis;
65  xAxis = opt.xAxis;
66  stype = opt.stype;
67  stypeext = opt.stypeext;
68  width=opt.width;
69  heigh=opt.heigh;
70 
71  lineWidth = opt.lineWidth;
73  more = opt.more;
74 
75  return *this;
76  }
77 };
78 
79 struct GGraph
80 {
81  // TypeOfGraph
82  size_t type;
83 
84  // data
85  std::string data;
86 
87  // option
88  std::string option;
89 
90  // Google chart option
91  GCoptions opt;
92 };
93 
95 
96 const std::string begin_data ="<html>\n\
97  <head>\n\
98  <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n\
99  <script type=\"text/javascript\">\n\
100  google.charts.load('current', {'packages':['corechart']});\n\
101  google.charts.setOnLoadCallback(drawVisualization);\n\
102 \n\
103 \n\
104  function drawVisualization() {\n";
105 
106 const std::string end_data="]);\n\n";
107 
108 const std::string begin_div = "}</script>\n\
109 </head>\n\
110 <body>\n";
111 
112 const std::string div_end = "</body>\n\
113 </html>\n";
114 
116 
147 {
148  // set of graphs
149  openfpm::vector<GGraph> set_of_graphs;
150 
151  // set inject HTML;
152  openfpm::vector<std::string> injectHTML;
153 
163  template<typename X, typename Y> std::string get_points_plot_data(const openfpm::vector<X> & x, const openfpm::vector<Y> & y, const openfpm::vector<std::string> & yn, const GCoptions & opt, size_t i)
164  {
165  std::stringstream data;
166 
167  size_t interval = 0;
168 
169  // we require that the number of x elements are the same as y elements
170 
171  if (x.size() != y.size())
172  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " vector x and the vector y must have the same number of elements " << x.size() << "!=" << y.size() << "\n";
173 
174  // Google chart visualization
175  data << "var data" << i << " = new google.visualization.DataTable();\n";
176  if (std::is_same<X,typename std::string>::value == true)
177  data << "data" << i << ".addColumn(" << "'string'" << "," << "'" << opt.xAxis <<"');\n";
178  else
179  data << "data" << i << ".addColumn(" << "'number'" << "," << "'" << opt.xAxis <<"');\n";
180 
181  for (size_t j = 0 ; j < y.last().size() ; j++)
182  {
183  if (yn.get(j) == std::string("interval"))
184  {
185  data << "data" << i << ".addColumn({id:'i" << interval/2 << "', type:'number', role:'interval'});\n";
186  interval++;
187  }
188  else
189  data << "data" << i << ".addColumn(" << "'number'" << "," << "'" << yn.get(j) <<"');\n";
190  }
191 
192  data << "data" << i << ".addRows([\n";
193  for (size_t i = 0 ; i < y.size() ; i++)
194  {
195 
196  for (size_t j = 0 ; j < y.get(i).size()+1 ; j++)
197  {
198  // the first is x
199  if (j == 0)
200  {
201  if (std::is_same<X,typename std::string>::value == true)
202  data << "['" << x.get(i) << "'";
203  else
204  data << "[" << x.get(i);
205  }
206  else
207  data << "," << y.get(i).get(j-1);
208  }
209  data << "],\n";
210  }
211 
212  return data.str();
213  }
214 
215  std::string get_colums_bar_option(const GCoptions & opt)
216  {
217  std::stringstream str;
218  str << "title : '" << opt.title << "',\n";
219  str << "vAxis: {title: '" << opt.yAxis << "'},\n";
220  str << "hAxis: {title: '" << opt.xAxis << "'},\n";
221  str << "seriesType: '" << opt.stype << "',\n";
222  if (opt.stypeext.size() != 0)
223  str << "series: " << opt.stypeext << "\n";
224  str << opt.more;
225 
226  return str.str();
227  }
228 
229  std::string get_points_plot_option(const GCoptions & opt)
230  {
231  std::stringstream str;
232  str << "title : '" << opt.title << "',\n";
233  str << "vAxis: {title: '" << opt.yAxis << "'},\n";
234  str << "hAxis: {title: '" << opt.xAxis << "'},\n";
235  str << "curveType: 'function',\n";
236 
237  str << "lineWidth: " << opt.lineWidth << ",\n";
238  if (opt.intervalsext.size() != 0)
239  str << "intervals: " << opt.intervalsext << ",\n";
240  else
241  str << "intervals: " << "{ 'style':'area' }" << ",\n";
242 
243  if (opt.intervalext.size() != 0)
244  str << "interval: " << opt.intervalext << "\n";
245 
246  str << opt.more;
247 
248  return str.str();
249  }
250 
258  void addData(std::ofstream & of, size_t i, const std::string & data)
259  {
260 
261  of << data;
262  of << "]);\n";
263  }
264 
272  void addOption(std::ofstream & of, size_t i, const std::string & opt)
273  {
274  of << "var options";
275  of << i;
276  of << "= {\n";
277  of << opt;
278  of << "};\n";
279  }
280 
287  void addDrawDiv(std::ofstream & of, size_t i)
288  {
289  of << "var chart = new google.visualization.ComboChart(document.getElementById('chart_div";
290  of << i;
291  of << "'));chart.draw(data";
292  of << i;
293  of << ", options";
294  of << i;
295  of << ");\n";
296  }
297 
304  void addDiv(std::ofstream & of, size_t i, const GCoptions & gc)
305  {
306  of << "<div id=\"chart_div";
307  of << i;
308  of << "\" style=\"width: ";
309  of << gc.width;
310  of << "px; height: ";
311  of << gc.heigh;
312  of << "px;\"></div>\n";
313  }
314 
315 public:
316 
317  GoogleChart()
318  {
319  injectHTML.add();
320  }
321 
328  template<typename Y> void AddHistGraph(openfpm::vector<Y> & y)
329  {
330  openfpm::vector<std::string> x;
331  x.resize(y.size());
332 
333  AddHistGraph<std::string,Y>(x,y);
334  }
335 
344  template<typename X, typename Y> void AddHistGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y)
345  {
346  GCoptions opt;
347 
348  openfpm::vector<std::string> yn;
349 
350  if (y.size() != 0)
351  yn.resize(y.get(0).size());
352 
353  AddHistGraph<X,Y,std::string>(x,y,yn,opt);
354  }
355 
366  template<typename X, typename Y, typename Yn> void AddHistGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y, openfpm::vector<Yn> & yn)
367  {
368  GCoptions opt;
369 
370  AddHistGraph(x,y,yn,opt);
371  }
372 
385  template<typename X, typename Y, typename Yn> void AddHistGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y, openfpm::vector<Yn> & yn , const GCoptions & opt)
386  {
387  set_of_graphs.add();
388  injectHTML.add();
389 
390  // Check that all the internal vector has the same number of elements
391 
392  if (y.size() != 0)
393  {
394  size_t sz = y.get(0).size();
395  for (size_t i = 0; i < y.size() ; i++)
396  {
397  if (y.get(i).size() != sz)
398  std::cerr << __FILE__ << ":" << __LINE__ << " error all the elements in the y vector must have the same numbers, element " << i << ": " << y.get(i).size() << " " << " mismatch the numbers of elements at 0: " << sz << "/n";
399  }
400  }
401 
402  set_of_graphs.last().type = GGRAPH_COLUMS;
403  set_of_graphs.last().data = get_points_plot_data(x,y,yn,opt,set_of_graphs.size()-1);
404  set_of_graphs.last().option = get_colums_bar_option(opt);
405  set_of_graphs.last().opt = opt;
406  }
407 
419  template<typename X, typename Y> void AddLinesGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y , const GCoptions & opt)
420  {
421  openfpm::vector<std::string> yn;
422 
423  if (y.size() == 0)
424  {
425  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " vector y must be filled";
426  return;
427  }
428 
429  for (size_t i = 0 ; i < y.last().size() ; i++)
430  yn.add(std::string("line") + std::to_string(i));
431 
432  AddLinesGraph(x,y,yn,opt);
433  }
434 
448  template<typename X, typename Y> void AddLinesGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y , const openfpm::vector<std::string> & yn, const GCoptions & opt)
449  {
450  if (y.size() == 0)
451  {
452  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " vector y must be filled\n";
453  return;
454  }
455 
456  set_of_graphs.add();
457  injectHTML.add();
458 
459  // Check that all the internal vectors has the same number of elements
460 
461  if (y.size() != 0)
462  {
463  size_t sz = y.get(0).size();
464  for (size_t i = 0; i < y.size() ; i++)
465  {
466  if (y.get(i).size() != sz)
467  std::cerr << __FILE__ << ":" << __LINE__ << " error all the elements in the y vector must have the same numbers, element " << i << ": " << y.get(i).size() << " " << " mismatch the numbers of elements at 0: " << sz << "/n";
468  }
469  }
470 
471  set_of_graphs.last().type = GGRAPH_POINTS;
472  set_of_graphs.last().data = get_points_plot_data(x,y,yn,opt,set_of_graphs.size()-1);
473  set_of_graphs.last().option = get_points_plot_option(opt);
474  set_of_graphs.last().opt = opt;
475  }
476 
482  void addHTML(const std::string & html)
483  {
484  injectHTML.last() = html;
485  }
486 
492  void write(std::string file)
493  {
494  // Open a file
495 
496  std::ofstream of(file);
497 
498  // Check if the file is open
499  if (of.is_open() == false)
500  {std::cerr << "Error cannot create the HTML file: " + file + "\n";}
501 
502  // write the file
503 
504  of << begin_data;
505 
506  for (size_t i = 0 ; i < set_of_graphs.size() ; i++)
507  addData(of,i,set_of_graphs.get(i).data);
508 
509  for (size_t i = 0 ; i < set_of_graphs.size() ; i++)
510  addOption(of,i,set_of_graphs.get(i).option);
511 
512  for (size_t i = 0 ; i < set_of_graphs.size() ; i++)
513  addDrawDiv(of,i);
514 
515  of << begin_div;
516 
517  of << injectHTML.get(0);
518 
519  for (size_t i = 0 ; i < set_of_graphs.size() ; i++)
520  {
521  addDiv(of,i,set_of_graphs.get(i).opt);
522  of << injectHTML.get(i+1);
523  }
524 
525  of << div_end;
526 
527  of.close();
528  }
529 };
530 
531 #endif /* OPENFPM_DATA_SRC_PLOT_GOOGLECHART_HPP_ */
void addDiv(std::ofstream &of, size_t i, const GCoptions &gc)
Add a div section.
void addHTML(const std::string &html)
Add HTML text.
void addOption(std::ofstream &of, size_t i, const std::string &opt)
Add an option data variable.
void AddLinesGraph(openfpm::vector< X > &x, openfpm::vector< Y > &y, const GCoptions &opt)
Add a simple lines graph.
bool isStacked
Definition: GoogleChart.hpp:45
void AddHistGraph(openfpm::vector< X > &x, openfpm::vector< Y > &y, openfpm::vector< Yn > &yn)
Add an histogram graph.
std::string title
Title of the chart.
Definition: GoogleChart.hpp:23
void AddHistGraph(openfpm::vector< X > &x, openfpm::vector< Y > &y, openfpm::vector< Yn > &yn, const GCoptions &opt)
Add an histogram graph.
void AddHistGraph(openfpm::vector< X > &x, openfpm::vector< Y > &y)
Add an histogram graph.
std::string more
more
Definition: GoogleChart.hpp:59
std::string stypeext
Definition: GoogleChart.hpp:38
void addDrawDiv(std::ofstream &of, size_t i)
Add a draw div section.
void addData(std::ofstream &of, size_t i, const std::string &data)
Add a graph data variable.
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 get_points_plot_data(const openfpm::vector< X > &x, const openfpm::vector< Y > &y, const openfpm::vector< std::string > &yn, const GCoptions &opt, size_t i)
Given X and Y vector return the string representing the data section of the Google Chart...
std::string stype
Definition: GoogleChart.hpp:33
std::string intervalsext
Definition: GoogleChart.hpp:52
void AddHistGraph(openfpm::vector< Y > &y)
Add an histogram graph.
void AddLinesGraph(openfpm::vector< X > &x, openfpm::vector< Y > &y, const openfpm::vector< std::string > &yn, const GCoptions &opt)
Add a simple plot graph.
std::string xAxis
X axis name.
Definition: GoogleChart.hpp:27
Google chart options.
Definition: GoogleChart.hpp:20