8 #ifndef OPENFPM_DATA_SRC_PLOT_GOOGLECHART_HPP_
9 #define OPENFPM_DATA_SRC_PLOT_GOOGLECHART_HPP_
12 #include "Vector/map_vector.hpp"
14 #define GGRAPH_COLUMS 1
15 #define GGRAPH_POINTS 2
96 const std::string begin_data =
"<html>\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\
104 function drawVisualization() {\n";
106 const std::string end_data=
"]);\n\n";
108 const std::string begin_div =
"}</script>\n\
112 const std::string div_end =
"</body>\n\
149 openfpm::vector<GGraph> set_of_graphs;
152 openfpm::vector<std::string> injectHTML;
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)
165 std::stringstream data;
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";
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";
179 data <<
"data" << i <<
".addColumn(" <<
"'number'" <<
"," <<
"'" << opt.
xAxis <<
"');\n";
181 for (
size_t j = 0 ; j < y.last().size() ; j++)
183 if (yn.get(j) == std::string(
"interval"))
185 data <<
"data" << i <<
".addColumn({id:'i" << interval/2 <<
"', type:'number', role:'interval'});\n";
189 data <<
"data" << i <<
".addColumn(" <<
"'number'" <<
"," <<
"'" << yn.get(j) <<
"');\n";
192 data <<
"data" << i <<
".addRows([\n";
193 for (
size_t i = 0 ; i < y.size() ; i++)
196 for (
size_t j = 0 ; j < y.get(i).size()+1 ; j++)
201 if (std::is_same<X,typename std::string>::value ==
true)
202 data <<
"['" << x.get(i) <<
"'";
204 data <<
"[" << x.get(i);
207 data <<
"," << y.get(i).get(j-1);
215 std::string get_colums_bar_option(
const GCoptions & opt)
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";
223 str <<
"series: " << opt.
stypeext <<
"\n";
229 std::string get_points_plot_option(
const GCoptions & opt)
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";
237 str <<
"lineWidth: " << opt.
lineWidth <<
",\n";
241 str <<
"intervals: " <<
"{ 'style':'area' }" <<
",\n";
258 void addData(std::ofstream & of,
size_t i,
const std::string & data)
272 void addOption(std::ofstream & of,
size_t i,
const std::string & opt)
289 of <<
"var chart = new google.visualization.ComboChart(document.getElementById('chart_div";
291 of <<
"'));chart.draw(data";
306 of <<
"<div id=\"chart_div";
308 of <<
"\" style=\"width: ";
310 of <<
"px; height: ";
312 of <<
"px;\"></div>\n";
330 openfpm::vector<std::string> x;
333 AddHistGraph<std::string,Y>(x,y);
344 template<
typename X,
typename Y>
void AddHistGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y)
348 openfpm::vector<std::string> yn;
351 yn.resize(y.get(0).size());
353 AddHistGraph<X,Y,std::string>(x,y,yn,opt);
366 template<
typename X,
typename Y,
typename Yn>
void AddHistGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y, openfpm::vector<Yn> & yn)
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)
394 size_t sz = y.get(0).size();
395 for (
size_t i = 0; i < y.size() ; i++)
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";
402 set_of_graphs.last().type = GGRAPH_COLUMS;
404 set_of_graphs.last().option = get_colums_bar_option(opt);
405 set_of_graphs.last().opt = opt;
419 template<
typename X,
typename Y>
void AddLinesGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y ,
const GCoptions & opt)
421 openfpm::vector<std::string> yn;
425 std::cerr <<
"Error: " << __FILE__ <<
":" << __LINE__ <<
" vector y must be filled";
429 for (
size_t i = 0 ; i < y.last().size() ; i++)
430 yn.add(std::string(
"line") + std::to_string(i));
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)
452 std::cerr <<
"Error: " << __FILE__ <<
":" << __LINE__ <<
" vector y must be filled\n";
463 size_t sz = y.get(0).size();
464 for (
size_t i = 0; i < y.size() ; i++)
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";
471 set_of_graphs.last().type = GGRAPH_POINTS;
473 set_of_graphs.last().option = get_points_plot_option(opt);
474 set_of_graphs.last().opt = opt;
484 injectHTML.last() = html;
496 std::ofstream of(file);
499 if (of.is_open() ==
false)
500 {std::cerr <<
"Error cannot create the HTML file: " + file +
"\n";}
506 for (
size_t i = 0 ; i < set_of_graphs.size() ; i++)
507 addData(of,i,set_of_graphs.get(i).data);
509 for (
size_t i = 0 ; i < set_of_graphs.size() ; i++)
510 addOption(of,i,set_of_graphs.get(i).option);
512 for (
size_t i = 0 ; i < set_of_graphs.size() ; i++)
517 of << injectHTML.get(0);
519 for (
size_t i = 0 ; i < set_of_graphs.size() ; i++)
521 addDiv(of,i,set_of_graphs.get(i).opt);
522 of << injectHTML.get(i+1);
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.
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.
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.
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.
Small class to produce graph with Google chart in HTML.
size_t lineWidth
Width of the line.
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...
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.