22 Graph::Graph(
const char* title,
const char* x_axis_name,
const char* y_axis_name)
24 set_captions(title, x_axis_name, y_axis_name);
29 void Graph::set_log_x(
bool log)
34 void Graph::set_log_y(
bool log)
39 void Graph::show_legend(
bool show)
44 void Graph::show_grid(
bool show)
49 void Graph::set_captions(
const char* title,
const char* x_axis_name,
const char* y_axis_name)
51 this->title = title ? title :
"";
52 xname = x_axis_name ? x_axis_name :
"";
53 yname = y_axis_name ? y_axis_name :
"";
56 int Graph::add_row(
const char*
name,
const char* color,
const char* line,
const char* marker)
59 if(name == NULL) name =
"";
66 set_row_style(rows.size()-1, color, line, marker);
70 void Graph::set_row_style(
int row,
const char* color,
const char* line,
const char* marker)
72 if(!rows.size()) add_row(NULL);
73 rows[row].color = color;
74 rows[row].line = line;
75 rows[row].marker = marker;
78 void Graph::add_values(
int row,
double x,
double y)
80 if(!rows.size()) add_row(NULL);
81 if(fabs(x) < 1e-12)
return;
83 if(row < 0 || row >= (
int)rows.size())
throw Hermes::Exceptions::Exception(
"Invalid row number.");
85 rows[row].data.push_back(xy);
88 void Graph::add_values(
double x,
double y)
91 if(!rows.size()) add_row(NULL);
92 if(fabs(x) < 1e-12 )
return;
95 rows[row].data.push_back(xy);
98 void Graph::add_values(
int row,
int n,
double* x,
double* y)
100 for (
int i = 0; i < n; i++)
101 add_values(row, x[i], y[i]);
104 void Graph::add_values(
int row,
int n, double2* xy)
106 for (
int i = 0; i < n; i++)
107 add_values(row, xy[i][0], xy[i][1]);
110 void Graph::save_numbered(
const char* filename,
int number)
113 sprintf(buffer, filename, number);
117 void SimpleGraph::save(
const char* filename)
119 if(!rows.size())
throw Hermes::Exceptions::Exception(
"No data rows defined.");
121 FILE* f = fopen(filename,
"w");
122 if(f == NULL)
throw Hermes::Exceptions::Exception(
"Error writing to %s.", filename);
124 for (
unsigned int i = 0; i < rows.size(); i++)
126 int rsize = rows[i].data.size();
127 for (
int j = 0; j < rsize; j++)
128 fprintf(f,
"%.14g %.14g\n", rows[i].data[j].x, rows[i].data[j].y);
133 this->info(
"Graph saved to file '%s'.", filename);
136 void MatlabGraph::save(
const char* filename)
140 if(!rows.size())
throw Hermes::Exceptions::Exception(
"No data rows defined.");
142 FILE* f = fopen(filename,
"w");
143 if(f == NULL)
throw Hermes::Exceptions::Exception(
"Error writing to %s", filename);
147 else if(logx && !logy)
148 fprintf(f,
"semilogx(");
149 else if(!logx && logy)
150 fprintf(f,
"semilogy(");
152 fprintf(f,
"loglog(");
154 for (
unsigned int i = 0; i < rows.size(); i++)
157 int rsize = rows[i].data.size();
158 for (k = 0; k < 2; k++)
160 for (j = 0; j < rsize; j++)
162 fprintf(f,
"%.14g", k ? rows[i].data[j].y : rows[i].data[j].x);
163 if(j < rsize-1) fprintf(f,
", ");
165 fprintf(f, (!k ?
"],[" :
"], '"));
167 fprintf(f,
"%s%s%s'", rows[i].color.c_str(), rows[i].line.c_str(), rows[i].marker.c_str());
168 if(i < rows.size()-1) fprintf(f,
", ");
172 if(title.length()) fprintf(f,
"title('%s');\n", title.c_str());
173 if(xname.length()) fprintf(f,
"xlabel('%s');\n", xname.c_str());
174 if(yname.length()) fprintf(f,
"ylabel('%s');\n", yname.c_str());
176 if(legend && (rows.size() > 1 || rows[0].name.length()))
178 fprintf(f,
"legend(");
179 for (
unsigned int i = 0; i < rows.size(); i++)
181 fprintf(f,
"'%s'", rows[i].name.c_str());
182 if(i < rows.size()-1) fprintf(f,
", ");
187 fprintf(f,
"legend off;\n");
189 fprintf(f,
"grid %s;\n", grid ?
"on" :
"off");
193 this->info(
"Graph saved. Run the file '%s' in Matlab.", filename);
202 else if(line ==
"-.")
204 else if(line ==
"--")
209 if(mark ==
".") pt = 7;
210 else if(mark ==
"o") pt = 6;
211 else if(mark ==
"O") pt = 7;
212 else if(mark ==
"x") pt = 2;
213 else if(mark ==
"+") pt = 1;
214 else if(mark ==
"*") pt = 3;
215 else if(mark ==
"s") pt = 4;
216 else if(mark ==
"S") pt = 5;
217 else if(mark ==
"d") pt = 10;
218 else if(mark ==
"D") pt = 11;
219 else if(mark ==
"v") pt = 12;
220 else if(mark ==
"V") pt = 13;
221 else if(mark ==
"^") pt = 9;
222 else if(mark ==
"<") pt = 12;
223 else if(mark ==
">") pt = 8;
224 else if(mark ==
"p") pt = 14;
225 else if(mark ==
"P") pt = 15;
228 if(col ==
"k") ct = -1;
229 else if(col ==
"b") ct = 3;
230 else if(col ==
"g") ct = 2;
231 else if(col ==
"c") ct = 5;
232 else if(col ==
"m") ct = 4;
233 else if(col ==
"y") ct = 6;
234 else if(col ==
"r") ct = 1;
242 if(
legend_pos.length() && !legend) legend =
true;
245 void GnuplotGraph::save(
const char* filename)
249 if(!rows.size())
throw Hermes::Exceptions::Exception(
"No data rows defined.");
251 FILE* f = fopen(filename,
"w");
252 if(f == NULL)
throw Hermes::Exceptions::Exception(
"Error writing to %s", filename);
256 int len = strlen(filename);
257 char* outname =
new char[len + 10];
258 strcpy(outname, filename);
259 char* slash = strrchr(outname,
'/');
260 if(slash != NULL) strcpy(outname, ++slash);
261 char* dot = strrchr(outname,
'.');
262 if(dot != NULL && dot > outname) *dot = 0;
263 strcat(outname,
".eps");
265 fprintf(f,
"set output '%s'\n", (
char*)outname);
267 fprintf(f,
"set size 0.8, 0.8\n");
270 fprintf(f,
"set logscale x\n");
271 else if(!logx && logy)
272 fprintf(f,
"set logscale y\n");
273 else if(logx && logy)
275 fprintf(f,
"set logscale x\n");
276 fprintf(f,
"set logscale y\n");
279 if(grid) fprintf(f,
"set grid\n");
281 if(title.length()) fprintf(f,
"set title '%s'\n", title.c_str());
282 if(xname.length()) fprintf(f,
"set xlabel '%s'\n", xname.c_str());
283 if(yname.length()) fprintf(f,
"set ylabel '%s'\n", yname.c_str());
287 for (
unsigned int i = 0; i < rows.size(); i++)
290 get_style_types(rows[i].line, rows[i].marker, rows[i].color, lt, pt, ct);
293 fprintf(f,
" '-' w p pointtype %d", pt);
295 fprintf(f,
" '-' w lp linewidth %g linetype %d pointtype %d", this->
lw, lt, pt);
297 fprintf(f,
" '-' w lp linewidth %g linecolor %d linetype %d pointtype %d", this->
lw, ct, lt, pt);
300 fprintf(f,
" title '%s' ", rows[i].name.c_str());
302 fprintf(f,
" notitle ");
304 if(i < rows.size() - 1) fprintf(f,
",\\\n ");
308 for (
unsigned int i = 0; i < rows.size(); i++)
310 int rsize = rows[i].data.size();
311 for (j = 0; j < rsize; j++)
312 fprintf(f,
"%.14g %.14g\n", rows[i].data[j].x, rows[i].data[j].y);
316 fprintf(f,
"set terminal x11\n");
319 this->info(
"Graph saved. Process the file '%s' with gnuplot.", filename);
322 PNGGraph::PNGGraph(
const char* title,
const char* x_axis_name,
const char* y_axis_name,
const double lines_width,
323 double plot_width,
double plot_height )
325 std::stringstream sstm;
326 sstm <<
"set terminal png font arial 12 size " << plot_width <<
"," << plot_height <<
" crop enhanced\n";
327 GnuplotGraph(title, x_axis_name, y_axis_name, lines_width, sstm.str());