GoogleChart.hpp 16.3 KB
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * GoogleChart.hpp
 *
 *  Created on: Jan 9, 2016
 *      Author: i-bird
 */

#ifndef OPENFPM_DATA_SRC_PLOT_GOOGLECHART_HPP_
#define OPENFPM_DATA_SRC_PLOT_GOOGLECHART_HPP_

#include <fstream>
Pietro Incardona's avatar
Pietro Incardona committed
12
#include "Vector/map_vector.hpp"
Pietro Incardona's avatar
Pietro Incardona committed
13
14

#define GGRAPH_COLUMS 1
Pietro Incardona's avatar
Pietro Incardona committed
15
#define GGRAPH_POINTS 2
Pietro Incardona's avatar
Pietro Incardona committed
16

Pietro Incardona's avatar
Pietro Incardona committed
17
18
19
20
#define GC_ZOOM std::string("explorer: {actions: ['dragToZoom', 'rightClickToReset'],axis: 'horizontal,vertical',keepInBounds: true, maxZoomIn: 128.0}")
#define GC_X_LOG std::string("hAxis: { logscale: true }")
#define GC_Y_LOG std::string("vAxis: { logscale: true }")

Pietro Incardona's avatar
Pietro Incardona committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*! \brief Google chart options
 *
 */
struct GCoptions
{
	//! Title of the chart
	std::string title;
	//! Y axis name
	std::string yAxis;
	//! X axis name
	std::string xAxis;

	//! Type of chart (list of the option can be founded in Google Chart API for seriesType)
	//! Possible options are:
	//!  'line', 'area', 'bars', 'candlesticks', and 'steppedArea'
	//! default: line
	std::string stype;

	//! Extended series options
	//! Example {5: {type: 'line'}} specify that the series number 5 must be represented
	//! with a line
	std::string stypeext;

incardon's avatar
incardon committed
44
	//! width of the graph in pixels
Pietro Incardona's avatar
Pietro Incardona committed
45
	size_t width=900;
incardon's avatar
incardon committed
46
47

	//! height of the graph in pixels
Pietro Incardona's avatar
Pietro Incardona committed
48
49
	size_t heigh=500;

Pietro Incardona's avatar
Pietro Incardona committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	//! Flag that specify if the colums are stacked
	//! Check in Google Chart for is stacked option
	bool isStacked = false;

	//! Width of the line
	size_t lineWidth = 4;

	//! Style for all the intervals
	//! Check Google Chart API intervals option
	std::string intervalsext;

	//! Style for each interval
	//! Check Google Chart API interval option
	std::string intervalext;

Pietro Incardona's avatar
Pietro Incardona committed
65
66
67
	//! more
	std::string more;

Pietro Incardona's avatar
Pietro Incardona committed
68
69
70
	//! curve type
	std::string curveType = "function";

incardon's avatar
incardon committed
71
	//! copy operator
Pietro Incardona's avatar
Pietro Incardona committed
72
73
	GCoptions & operator=(const GCoptions & opt)
	{
Pietro Incardona's avatar
Pietro Incardona committed
74
75
76
77
78
79
80
81
82
83
		title = opt.title;
		yAxis = opt.yAxis;
		xAxis = opt.xAxis;
		stype = opt.stype;
		stypeext = opt.stypeext;
		width=opt.width;
		heigh=opt.heigh;

		lineWidth = opt.lineWidth;
		intervalsext = opt.intervalsext;
Pietro Incardona's avatar
Pietro Incardona committed
84
		more = opt.more;
Pietro Incardona's avatar
Pietro Incardona committed
85

Pietro Incardona's avatar
Pietro Incardona committed
86
87
88
89
90
91
		return *this;
	}
};

struct GGraph
{
incardon's avatar
incardon committed
92
	//! TypeOfGraph
Pietro Incardona's avatar
Pietro Incardona committed
93
94
	size_t type;

incardon's avatar
incardon committed
95
	//! data
Pietro Incardona's avatar
Pietro Incardona committed
96
97
	std::string data;

incardon's avatar
incardon committed
98
	//! option
Pietro Incardona's avatar
Pietro Incardona committed
99
100
	std::string option;

incardon's avatar
incardon committed
101
	//! Google chart option
Pietro Incardona's avatar
Pietro Incardona committed
102
103
104
105
106
107
108
109
	GCoptions opt;
};

/////////////////// Constants strings usefull to construct the HTML page //////////

const std::string begin_data ="<html>\n\
  <head>\n\
    <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n\
incardon's avatar
incardon committed
110
	<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js\"></script>\n\
Pietro Incardona's avatar
Pietro Incardona committed
111
112
113
114
    <script type=\"text/javascript\">\n\
      google.charts.load('current', {'packages':['corechart']});\n\
      google.charts.setOnLoadCallback(drawVisualization);\n\
\n\
incardon's avatar
incardon committed
115
116
117
118
119
120
121
122
123
124
function exportToSVG(i)\n\
{\n\
var e = document.getElementById('chart_div'+i);\n\
var svg = e.getElementsByTagName('svg')[0].parentNode.innerHTML;\n\
var pos = svg.lastIndexOf(\"</svg>\");\n\
pos += 6;\n\
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\
svgData = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);\n\
$(this).attr({'href': svgData,'target': '_blank'});\n\
}\n\
Pietro Incardona's avatar
Pietro Incardona committed
125
126
127
128
129
130
131
132
133
134
135
136
\n\
      function drawVisualization() {\n";

const std::string end_data="]);\n\n";

const std::string begin_div = "}</script>\n\
</head>\n\
<body>\n";

const std::string div_end = "</body>\n\
</html>\n";

incardon's avatar
incardon committed
137
138
139
140
const std::string saving_javascript = "function save(i)\n\
										var e = document.getElementById('chart_')\n\
										e.getElementsByTagName('svg')[0].parentNode.innerHTML";

Pietro Incardona's avatar
Pietro Incardona committed
141
142
/////////////////////////////////////////////////////////////////////

Pietro Incardona's avatar
Pietro Incardona committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*! \brief Small class to produce graph with Google chart in HTML
 *
 * This Class can produce several graph using google chart
 *
 * ### Create Histogram graph
 *
 * \image html g_graph_hist.jpg
 *
 * This code produce the graph above
 *
 * \snippet Plot_unit_tests.hpp Producing an Histogram graph
 *
 * ### Create Lines
 *
 * \image html g_graph_plot2.jpg
 *
 * This code produce the graph above
 *
 * \snippet Plot_unit_tests.hpp Producing lines
 *
 * ### Create lines with different styles
 *
 * \image html g_graph_plot.jpg
 *
 * This code produce the graph above
 *
 * \snippet Plot_unit_tests.hpp Producing lines graph with style
Pietro Incardona's avatar
Pietro Incardona committed
170
171
172
173
 *
 */
class GoogleChart
{
incardon's avatar
incardon committed
174
	//! set of graphs
Pietro Incardona's avatar
Pietro Incardona committed
175
176
	openfpm::vector<GGraph> set_of_graphs;

incardon's avatar
incardon committed
177
	//! set inject HTML;
Pietro Incardona's avatar
Pietro Incardona committed
178
179
	openfpm::vector<std::string> injectHTML;

Pietro Incardona's avatar
Pietro Incardona committed
180
181
	/*! \brief Given X and Y vector return the string representing the data section of the Google Chart
	 *
incardon's avatar
incardon committed
182
183
184
185
186
187
188
189
	 * \tparam X type for the X coordinates
	 * \tparam Y type for the Y coordinates
	 *
	 * \param x vector of points on x
	 * \param y vector of points on y
	 * \param yn vector containing the name of each graph
	 * \param opt options to draw the graph
	 * \param i index of the graph we are drawing
Pietro Incardona's avatar
Pietro Incardona committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
	 *
	 * \return string with the data section
	 *
	 */
	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)
	{
		std::stringstream data;

		size_t interval = 0;

		// we require that the number of x elements are the same as y elements

		if (x.size() != y.size())
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " vector x and the vector y must have the same number of elements " << x.size() << "!=" << y.size() << "\n";

		// Google chart visualization
        data << "var data" << i << " = new google.visualization.DataTable();\n";
		if (std::is_same<X,typename std::string>::value == true)
			data << "data" << i << ".addColumn("  << "'string'" << "," << "'" << opt.xAxis <<"');\n";
		else
			data << "data" << i << ".addColumn("  << "'number'" << "," << "'" << opt.xAxis <<"');\n";

        for (size_t j = 0 ; j < y.last().size() ; j++)
        {
        	if (yn.get(j) == std::string("interval"))
        	{
        		data << "data" << i << ".addColumn({id:'i" << interval/2 << "', type:'number', role:'interval'});\n";
        		interval++;
        	}
        	else
        		data << "data" << i << ".addColumn("  << "'number'" << "," << "'" << yn.get(j) <<"');\n";
        }

        data << "data" << i << ".addRows([\n";
Pietro Incardona's avatar
Pietro Incardona committed
224
        for (size_t i = 0 ; i < y.size() && x.size() ; i++)
Pietro Incardona's avatar
Pietro Incardona committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
        {

        	for (size_t j = 0 ; j < y.get(i).size()+1 ; j++)
        	{
        		// the first is x
        		if (j == 0)
        		{
        			if (std::is_same<X,typename std::string>::value == true)
        				data << "['"  << x.get(i) << "'";
        			else
        				data << "["  << x.get(i);
        		}
        		else
        			data << "," << y.get(i).get(j-1);
        	}
        	data << "],\n";
        }

		return data.str();
	}

	std::string get_colums_bar_option(const GCoptions & opt)
Pietro Incardona's avatar
Pietro Incardona committed
247
248
	{
		std::stringstream str;
Pietro Incardona's avatar
Pietro Incardona committed
249
250
251
252
		str << "title : '" << opt.title << "'";
	    str << ",\nvAxis: {title: '" << opt.yAxis <<  "'}";
	    str << ",\nhAxis: {title: '" << opt.xAxis << "'}";
	    str << ",\nseriesType: '" << opt.stype << "'";
Pietro Incardona's avatar
Pietro Incardona committed
253
	    if (opt.stypeext.size() != 0)
Pietro Incardona's avatar
Pietro Incardona committed
254
255
256
	    	str << ",\nseries: " << opt.stypeext;
	    if (opt.more.size() != 0)
	    	str << ",\n" <<opt.more;
Pietro Incardona's avatar
Pietro Incardona committed
257
258
259
260

	    return str.str();
	}

Pietro Incardona's avatar
Pietro Incardona committed
261
262
263
	std::string get_points_plot_option(const GCoptions & opt)
	{
		std::stringstream str;
Pietro Incardona's avatar
Pietro Incardona committed
264
265
266
267
		str << "title : '" << opt.title << "'";
	    str << ",\nvAxis: {title: '" << opt.yAxis <<  "'}";
	    str << ",\nhAxis: {title: '" << opt.xAxis << "'}";
	    str << ",\ncurveType: '"<< opt.curveType << "'";
Pietro Incardona's avatar
Pietro Incardona committed
268

Pietro Incardona's avatar
Pietro Incardona committed
269
        str << ",\nlineWidth: " << opt.lineWidth;
Pietro Incardona's avatar
Pietro Incardona committed
270
        if (opt.intervalsext.size() != 0)
Pietro Incardona's avatar
Pietro Incardona committed
271
        	str << ",\nintervals: " << opt.intervalsext;
Pietro Incardona's avatar
Pietro Incardona committed
272
        else
Pietro Incardona's avatar
Pietro Incardona committed
273
        	str << ",\nintervals: " << "{ 'style':'area' }";
Pietro Incardona's avatar
Pietro Incardona committed
274
275

        if (opt.intervalext.size() != 0)
Pietro Incardona's avatar
Pietro Incardona committed
276
        	str << ",\ninterval: " << opt.intervalext << "\n";
Pietro Incardona's avatar
Pietro Incardona committed
277

Pietro Incardona's avatar
Pietro Incardona committed
278
279
        if (opt.more.size() != 0)
        	str << ",\n" << opt.more;
Pietro Incardona's avatar
Pietro Incardona committed
280

Pietro Incardona's avatar
Pietro Incardona committed
281
282
283
	    return str.str();
	}

Pietro Incardona's avatar
Pietro Incardona committed
284
285
286
287
288
289
290
291
292
	/*! \brief Add a graph data variable
	 *
	 * \param of file out
	 * \param i id
	 * \param data string
	 *
	 */
	void addData(std::ofstream & of, size_t i, const std::string & data)
	{
Pietro Incardona's avatar
Pietro Incardona committed
293

Pietro Incardona's avatar
Pietro Incardona committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
		of << data;
		of << "]);\n";
	}

	/*! \brief Add an option data variable
	 *
	 * \param of file out
	 * \param i id
	 * \param opt string
	 *
	 */
	void addOption(std::ofstream & of, size_t i, const std::string & opt)
	{
		of << "var options";
		of << i;
		of << "= {\n";
		of << opt;
		of << "};\n";
	}

	/*! \brief Add a draw div section
	 *
	 * \param of file out
	 * \param i id
	 *
	 */
	void addDrawDiv(std::ofstream & of, size_t i)
	{
incardon's avatar
incardon committed
322
		of << "$(\"#export_svg" << i << "\").on(\"click\", function (event) {exportToSVG.apply(this,[" << i << "]);});\n";
Pietro Incardona's avatar
Pietro Incardona committed
323
324
325
326
327
328
329
330
331
332
333
		of << "var chart = new google.visualization.ComboChart(document.getElementById('chart_div";
		of << i;
		of << "'));chart.draw(data";
		of << i;
		of << ", options";
		of << i;
		of << ");\n";
	}

	/*! \brief Add a div section
	 *
incardon's avatar
incardon committed
334
335
	 * \param of file ofstream
	 * \param i id of the graph
Pietro Incardona's avatar
Pietro Incardona committed
336
337
338
339
340
	 * \param gc GoogleChart option
	 *
	 */
	void addDiv(std::ofstream & of, size_t i, const GCoptions & gc)
	{
incardon's avatar
incardon committed
341
		of << "<a href=\"#\" download=\"graph1.svg\" id=\"export_svg" << i << "\"><button>Export data into svg</button></a>";
Pietro Incardona's avatar
Pietro Incardona committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
		of << "<div id=\"chart_div";
		of << i;
		of << "\" style=\"width: ";
		of << gc.width;
		of << "px; height: ";
		of << gc.heigh;
		of << "px;\"></div>\n";
	}

public:

	GoogleChart()
	{
		injectHTML.add();
	}

Pietro Incardona's avatar
Pietro Incardona committed
358
	/*! \brief Add an histogram graph
Pietro Incardona's avatar
Pietro Incardona committed
359
	 *
Pietro Incardona's avatar
Pietro Incardona committed
360
361
	 * \param y A vector of vectors the size of y indicate how many values we have on x
	 *          each x value can have multiple values or datasets
Pietro Incardona's avatar
Pietro Incardona committed
362
363
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
364
	template<typename Y> void AddHistGraph(openfpm::vector<Y> & y)
Pietro Incardona's avatar
Pietro Incardona committed
365
366
367
368
	{
		openfpm::vector<std::string> x;
		x.resize(y.size());

Pietro Incardona's avatar
Pietro Incardona committed
369
		AddHistGraph<std::string,Y>(x,y);
Pietro Incardona's avatar
Pietro Incardona committed
370
371
	}

Pietro Incardona's avatar
Pietro Incardona committed
372
	/*! \brief Add an histogram graph
Pietro Incardona's avatar
Pietro Incardona committed
373
	 *
Pietro Incardona's avatar
Pietro Incardona committed
374
375
	 * \param y A vector of vectors the size of y indicate how many values we have on x
	 *          each x value can have multiple values or datasets
Pietro Incardona's avatar
Pietro Incardona committed
376
	 *
Pietro Incardona's avatar
Pietro Incardona committed
377
	 * \param x Give a name or number to each colums. Can be a string or a number
Pietro Incardona's avatar
Pietro Incardona committed
378
379
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
380
	template<typename X, typename Y> void AddHistGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y)
Pietro Incardona's avatar
Pietro Incardona committed
381
382
383
384
385
386
387
388
	{
		GCoptions opt;

		openfpm::vector<std::string> yn;

		if (y.size() != 0)
			yn.resize(y.get(0).size());

Pietro Incardona's avatar
Pietro Incardona committed
389
		AddHistGraph<X,Y,std::string>(x,y,yn,opt);
Pietro Incardona's avatar
Pietro Incardona committed
390
391
	}

Pietro Incardona's avatar
Pietro Incardona committed
392
	/*! \brief Add an histogram graph
Pietro Incardona's avatar
Pietro Incardona committed
393
	 *
Pietro Incardona's avatar
Pietro Incardona committed
394
395
	 * \param y A vector of vectors the size of y indicate how many values we have on x
	 *          each x value can have multiple values or datasets
Pietro Incardona's avatar
Pietro Incardona committed
396
	 *
Pietro Incardona's avatar
Pietro Incardona committed
397
	 * \param x Give a name or number to each colums. Can be a string or a number
Pietro Incardona's avatar
Pietro Incardona committed
398
399
400
401
	 *
	 * \param yn Give a name to each dataset
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
402
	template<typename X, typename Y, typename Yn> void AddHistGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y, openfpm::vector<Yn> & yn)
Pietro Incardona's avatar
Pietro Incardona committed
403
404
405
	{
		GCoptions opt;

Pietro Incardona's avatar
Pietro Incardona committed
406
		AddHistGraph(x,y,yn,opt);
Pietro Incardona's avatar
Pietro Incardona committed
407
408
	}

Pietro Incardona's avatar
Pietro Incardona committed
409
	/*! \brief Add an histogram graph
Pietro Incardona's avatar
Pietro Incardona committed
410
	 *
Pietro Incardona's avatar
Pietro Incardona committed
411
412
	 * \param y A vector of vectors the size of y indicate how many values we have on x
	 *          each x value can have multiple values or datasets
Pietro Incardona's avatar
Pietro Incardona committed
413
	 *
Pietro Incardona's avatar
Pietro Incardona committed
414
	 * \param x Give a name or number to each colums. Can be a string or a number
Pietro Incardona's avatar
Pietro Incardona committed
415
416
417
418
419
420
	 *
	 * \param yn Give a name to each dataset
	 *
	 * \param opt Graph options
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
421
	template<typename X, typename Y, typename Yn> void AddHistGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y, openfpm::vector<Yn> & yn , const GCoptions & opt)
Pietro Incardona's avatar
Pietro Incardona committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
	{
		set_of_graphs.add();
		injectHTML.add();

		// Check that all the internal vector has the same number of elements

		if (y.size() != 0)
		{
			size_t sz = y.get(0).size();
			for (size_t i = 0; i < y.size() ; i++)
			{
				if (y.get(i).size() != sz)
					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";
			}
		}

		set_of_graphs.last().type = GGRAPH_COLUMS;
Pietro Incardona's avatar
Pietro Incardona committed
439
		set_of_graphs.last().data = get_points_plot_data(x,y,yn,opt,set_of_graphs.size()-1);
Pietro Incardona's avatar
Pietro Incardona committed
440
441
442
443
		set_of_graphs.last().option = get_colums_bar_option(opt);
		set_of_graphs.last().opt = opt;
	}

Pietro Incardona's avatar
Pietro Incardona committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
	/*! \brief Add lines graph
	 *
	 * \param y A vector of vectors of values. each vector is a graph of points
	 *
	 * \param x Give a name or number to each x value, so can be a string or a number
	 *
	 * \param opt Graph options
	 *
	 */
	template<typename X, typename Y> void AddLines(openfpm::vector<X> & x, openfpm::vector<Y> & y , const GCoptions & opt)
	{
		openfpm::vector<std::string> yn;

		if (y.size() == 0)
		{
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " vector y must be filled" << std::endl;
			return;
		}

		for (size_t i = 0 ; i < y.last().size() ; i++)
			yn.add(std::string("line") + std::to_string(i));

		if (y.size() == 0)
			return;

		// number of points
		size_t np = y.last().size();

		for (size_t j = 0 ; j < y.size() ; j++)
		{
			if (y.get(j).size() != np)
				std::cerr << __FILE__ << ":" << __LINE__ << " Error all the graph must have the same number of points " << np << "!=" << y.get(j).size() << std::endl;
		}

		openfpm::vector<openfpm::vector<X>> swap;


		// swap the vector
		// Each vector is a graph
		// It is different from the other call where each vector
		// has multiple value for the same point
		for (size_t i = 0 ; i < np ; i++)
		{
			swap.add();
			for (size_t j = 0 ; j < y.size() ; j++)
			{
				swap.last().add(y.get(j).get(i));
			}
		}

		AddLinesGraph(x,swap,yn,opt);
	}

Pietro Incardona's avatar
Pietro Incardona committed
497
	/*! \brief Add a simple lines graph
Pietro Incardona's avatar
Pietro Incardona committed
498
	 *
Pietro Incardona's avatar
Pietro Incardona committed
499
	 * \param y A vector of vectors of values. The size of y indicate how many x values
Pietro Incardona's avatar
Pietro Incardona committed
500
501
	 *          we have, while the internal vector can store multiple value of the same point,
	 *          for example error bar
Pietro Incardona's avatar
Pietro Incardona committed
502
	 *
Pietro Incardona's avatar
Pietro Incardona committed
503
	 * \param x Give a name or number to each x value, so can be a string or a number
Pietro Incardona's avatar
Pietro Incardona committed
504
505
506
507
	 *
	 * \param opt Graph options
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
508
	template<typename X, typename Y> void AddLinesGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y , const GCoptions & opt)
Pietro Incardona's avatar
Pietro Incardona committed
509
510
511
512
513
514
515
516
517
518
519
520
	{
		openfpm::vector<std::string> yn;

		if (y.size() == 0)
		{
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " vector y must be filled";
			return;
		}

		for (size_t i = 0 ; i < y.last().size() ; i++)
			yn.add(std::string("line") + std::to_string(i));

Pietro Incardona's avatar
Pietro Incardona committed
521
		AddLinesGraph(x,y,yn,opt);
Pietro Incardona's avatar
Pietro Incardona committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
	}

	/*! \brief Add a simple plot graph
	 *
	 * \param y A vector of vector of values (numbers) the size of y indicate how many x values
	 *          or colums we have, while the internal vector store multiple lines,
	 *          or error bars
	 *
	 * \param x Give a name or number to each colums, so can be a string or a number
	 *
	 * \param yn Give a name to each line, or specify an error bar
	 *
	 * \param opt Graph options
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
537
	template<typename X, typename Y> void AddLinesGraph(openfpm::vector<X> & x, openfpm::vector<Y> & y , const openfpm::vector<std::string> & yn, const GCoptions & opt)
Pietro Incardona's avatar
Pietro Incardona committed
538
539
540
	{
		if (y.size() == 0)
		{
Pietro Incardona's avatar
Pietro Incardona committed
541
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " vector y must be filled\n";
Pietro Incardona's avatar
Pietro Incardona committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
			return;
		}

		set_of_graphs.add();
		injectHTML.add();

		// Check that all the internal vectors has the same number of elements

		if (y.size() != 0)
		{
			size_t sz = y.get(0).size();
			for (size_t i = 0; i < y.size() ; i++)
			{
				if (y.get(i).size() != sz)
					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";
			}
		}

		set_of_graphs.last().type = GGRAPH_POINTS;
		set_of_graphs.last().data = get_points_plot_data(x,y,yn,opt,set_of_graphs.size()-1);
		set_of_graphs.last().option = get_points_plot_option(opt);
Pietro Incardona's avatar
Pietro Incardona committed
563
564
565
566
567
568
569
570
571
572
573
574
575
		set_of_graphs.last().opt = opt;
	}

	/*! \brief Add HTML text
	 *
	 * \param html add html text in the page
	 *
	 */
	void addHTML(const std::string & html)
	{
		injectHTML.last() = html;
	}

Pietro Incardona's avatar
Pietro Incardona committed
576
	/*! \brief It write the graphs on file in html format using Google charts
Pietro Incardona's avatar
Pietro Incardona committed
577
	 *
Pietro Incardona's avatar
Pietro Incardona committed
578
	 * \param file output file
Pietro Incardona's avatar
Pietro Incardona committed
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
	 *
	 */
	void write(std::string file)
	{
		// Open a file

		std::ofstream of(file);

		// Check if the file is open
		if (of.is_open() == false)
		{std::cerr << "Error cannot create the HTML file: " + file + "\n";}

		// write the file

		of << begin_data;

		for (size_t i = 0 ; i < set_of_graphs.size() ; i++)
			addData(of,i,set_of_graphs.get(i).data);

		for (size_t i = 0 ; i < set_of_graphs.size() ; i++)
			addOption(of,i,set_of_graphs.get(i).option);

		for (size_t i = 0 ; i < set_of_graphs.size() ; i++)
			addDrawDiv(of,i);

		of << begin_div;

		of << injectHTML.get(0);

		for (size_t i = 0 ; i < set_of_graphs.size() ; i++)
		{
			addDiv(of,i,set_of_graphs.get(i).opt);
			of << injectHTML.get(i+1);
		}

		of << div_end;

		of.close();
	}
};

#endif /* OPENFPM_DATA_SRC_PLOT_GOOGLECHART_HPP_ */