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
17 #define GC_ZOOM std::string("explorer: {actions: ['dragToZoom', 'rightClickToReset'],axis: 'horizontal,vertical',keepInBounds: true, maxZoomIn: 128.0}")
18 #define GC_X_LOG std::string("hAxis: { logScale: true }")
19 #define GC_Y_LOG std::string("vAxis: { logScale: true }")
122 const std::string begin_data =
"<html>\n\
124 <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n\
125 <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js\"></script>\n\
126 <script type=\"text/javascript\">\n\
127 google.charts.load('current', {'packages':['corechart']});\n\
128 google.charts.setOnLoadCallback(drawVisualization);\n\
130 function exportToSVG(i)\n\
132 var e = document.getElementById('chart_div'+i);\n\
133 var svg = e.getElementsByTagName('svg')[0].parentNode.innerHTML;\n\
134 var pos = svg.lastIndexOf(\"</svg>\");\n\
136 svg = svg.substring(0,4) + \" xmlns='http://www.w3.org/2000/svg' xmlns:xlink= 'http://www.w3.org/1999/xlink' \" + svg.substring(4,pos);\n\
137 svgData = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);\n\
138 $(this).attr({'href': svgData,'target': '_blank'});\n\
141 function drawVisualization() {\n";
143 const std::string end_data=
"]);\n\n";
145 const std::string begin_div =
"}</script>\n\
149 const std::string div_end =
"</body>\n\
152 const std::string saving_javascript =
"function save(i)\n\
153 var e = document.getElementById('chart_')\n\
154 e.getElementsByTagName('svg')[0].parentNode.innerHTML";
211 std::stringstream data;
218 std::cerr <<
"Error: " << __FILE__ <<
":" << __LINE__ <<
" vector x and the vector y must have the same number of elements " << x.
size() <<
"!=" << y.
size() <<
"\n";
221 data <<
"var data" << i <<
" = new google.visualization.DataTable();\n";
222 if (std::is_same<X,typename std::string>::value ==
true)
223 data <<
"data" << i <<
".addColumn(" <<
"'string'" <<
"," <<
"'" << opt.
xAxis <<
"');\n";
225 data <<
"data" << i <<
".addColumn(" <<
"'number'" <<
"," <<
"'" << opt.
xAxis <<
"');\n";
227 for (
size_t j = 0 ; j < y.last().
size() ; j++)
229 if (yn.get(j) == std::string(
"interval"))
231 data <<
"data" << i <<
".addColumn({id:'i" << interval/2 <<
"', type:'number', role:'interval'});\n";
235 data <<
"data" << i <<
".addColumn(" <<
"'number'" <<
"," <<
"'" << yn.get(j) <<
"');\n";
238 data <<
"data" << i <<
".addRows([\n";
239 for (
size_t i = 0 ; i < y.
size() && x.
size() ; i++)
242 for (
size_t j = 0 ; j < y.get(i).
size()+1 ; j++)
247 if (std::is_same<X,typename std::string>::value ==
true)
248 data <<
"['" << x.get(i) <<
"'";
250 data <<
"[" << x.get(i);
253 data <<
"," << y.get(i).get(j-1);
270 if (opt.
barWD ==
false)
271 return std::string();
273 std::stringstream str;
275 str <<
"[0" << std::endl;
277 for (
size_t i = 1 ; i < n_col ; i++)
279 str <<
"," << i <<
",{ calc: \"stringify\"," << std::endl;
280 str <<
"sourceColumn: " << i <<
"," << std::endl;
281 str <<
"type: \"string\"," << std::endl;
282 str <<
"role: \"annotation\" }"<< std::endl;
285 str <<
"]" << std::endl;
290 std::string get_colums_bar_option(
const GCoptions & opt)
292 std::stringstream str;
293 str <<
"title : '" << opt.
title <<
"'";
294 str <<
",\nvAxis: {title: '" << opt.
yAxis <<
"'}";
295 str <<
",\nhAxis: {title: '" << opt.
xAxis <<
"'}";
296 str <<
",\nseriesType: '" << opt.
stype <<
"'";
298 str <<
",\nseries: " << opt.
stypeext;
299 if (opt.
more.size() != 0)
300 str <<
",\n" <<opt.
more;
305 std::string get_points_plot_option(
const GCoptions & opt)
307 std::stringstream str;
308 str <<
"title : '" << opt.
title <<
"'";
309 str <<
",\nvAxis: {title: '" << opt.
yAxis <<
"'}";
310 str <<
",\nhAxis: {title: '" << opt.
xAxis <<
"'}";
311 str <<
",\ncurveType: '"<< opt.
curveType <<
"'";
313 str <<
",\nlineWidth: " << opt.
lineWidth;
317 str <<
",\nintervals: " <<
"{ 'style':'area' }";
322 if (opt.
more.size() != 0)
323 str <<
",\n" << opt.
more;
335 void addData(std::ofstream & of,
size_t i,
const std::string & data)
349 void addOption(std::ofstream & of,
size_t i,
const std::string & opt)
365 void addView(std::ofstream & of,
size_t i, std::string view)
367 if (view.size() == 0)
370 of <<
"var view" << i <<
" = new google.visualization.DataView(data" << i <<
");" << std::endl;
371 of <<
"view"<< i <<
".setColumns(";
372 of << view <<
");" << std::endl;
382 void addDrawDiv(std::ofstream & of,
size_t i,
bool draw_view)
384 of <<
"$(\"#export_svg" << i <<
"\").on(\"click\", function (event) {exportToSVG.apply(this,[" << i <<
"]);});\n";
385 of <<
"var chart = new google.visualization.ComboChart(document.getElementById('chart_div";
387 of <<
"'));" << std::endl;
388 if (draw_view ==
true)
390 of <<
"chart.draw(data";
395 of <<
"chart.draw(view";
412 of <<
"<a href=\"#\" download=\"graph1.svg\" id=\"export_svg" << i <<
"\"><button>Export data into svg</button></a>";
413 of <<
"<div id=\"chart_div";
415 of <<
"\" style=\"width: ";
417 of <<
"px; height: ";
419 of <<
"px;\"></div>\n";
440 AddHistGraph<std::string,Y>(x,y);
458 yn.resize(y.get(0).
size());
460 AddHistGraph<X,Y,std::string>(x,y,yn,opt);
501 size_t sz = y.get(0).
size();
502 for (
size_t i = 0; i < y.
size() ; i++)
504 if (y.get(i).
size() != sz)
505 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";
531 std::cerr <<
"Error: " << __FILE__ <<
":" << __LINE__ <<
" vector y must be filled" << std::endl;
535 for (
size_t i = 0 ; i < y.last().
size() ; i++)
536 yn.add(std::string(
"line") + std::to_string(i));
542 size_t np = y.last().
size();
544 for (
size_t j = 0 ; j < y.
size() ; j++)
546 if (y.get(j).
size() != np)
547 std::cerr << __FILE__ <<
":" << __LINE__ <<
" Error all the graph must have the same number of points " << np <<
"!=" << y.get(j).
size() << std::endl;
557 for (
size_t i = 0 ; i < np ; i++)
560 for (
size_t j = 0 ; j < y.
size() ; j++)
562 swap.last().add(y.get(j).get(i));
586 std::cerr <<
"Error: " << __FILE__ <<
":" << __LINE__ <<
" vector y must be filled";
590 for (
size_t i = 0 ; i < y.last().
size() ; i++)
591 yn.add(std::string(
"line") + std::to_string(i));
613 std::cerr <<
"Error: " << __FILE__ <<
":" << __LINE__ <<
" vector y must be filled\n";
624 size_t sz = y.get(0).
size();
625 for (
size_t i = 0; i < y.
size() ; i++)
627 if (y.get(i).
size() != sz)
628 std::cerr << __FILE__ <<
":" << __LINE__ <<
" error all the elements in the y vector must have the same numbers of elements " << y.get(i).
size() <<
" != " << sz <<
"\n";
657 std::ofstream of(file);
660 if (of.is_open() ==
false)
661 {std::cerr <<
"Error cannot create the HTML file: " + file +
"\n";}
size_t heigh
height of the graph in pixels
GCoptions opt
Google chart option.
void addDiv(std::ofstream &of, size_t i, const GCoptions &gc)
Add a div section.
std::string curveType
curve type
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 AddLines(openfpm::vector< X > &x, openfpm::vector< Y > &y, const GCoptions &opt)
Add lines graph.
void AddLinesGraph(openfpm::vector< X > &x, openfpm::vector< Y > &y, const GCoptions &opt)
Add a simple lines graph.
openfpm::vector< std::string > injectHTML
set inject HTML;
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.
std::string view
view in case we need a view
void addData(std::ofstream &of, size_t i, const std::string &data)
Add a graph data variable.
std::string yAxis
Y axis name.
openfpm::vector< GGraph > set_of_graphs
set of graphs
Small class to produce graph with Google chart in HTML.
GCoptions & operator=(const GCoptions &opt)
copy operator
size_t lineWidth
Width of the line.
std::string get_view_bar_option(const GCoptions &opt, size_t n_col)
Construct a view option.
void write(std::string file)
It write the graphs on file in html format using Google charts.
void addDrawDiv(std::ofstream &of, size_t i, bool draw_view)
Add a draw div section.
size_t width
width of the graph in pixels
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.
void addView(std::ofstream &of, size_t i, std::string view)
Add a view data variable.