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 ==
nullptr) name =
"";
66 set_row_style(rows.size() - 1, color, line, marker);
67 return rows.size() - 1;
70 void Graph::set_row_style(
int row,
const char* color,
const char* line,
const char* marker)
72 if (!rows.size()) add_row(
nullptr);
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(
nullptr);
82 if (fabs(x) < Hermes::HermesSqrtEpsilon)
return;
84 if (row < 0 || row >= (
int)rows.size())
throw Hermes::Exceptions::Exception(
"Invalid row number.");
86 rows[row].data.push_back(xy);
89 void Graph::add_values(
double x,
double y)
92 if (!rows.size()) add_row(
nullptr);
94 if (fabs(x) < Hermes::HermesSqrtEpsilon)
return;
97 rows[row].data.push_back(xy);
100 void Graph::add_values(
int row,
int n,
double* x,
double* y)
102 for (
int i = 0; i < n; i++)
103 add_values(row, x[i], y[i]);
106 void Graph::add_values(
int row,
int n, double2* xy)
108 for (
int i = 0; i < n; i++)
109 add_values(row, xy[i][0], xy[i][1]);
112 void Graph::save_numbered(
const char* filename,
int number)
115 sprintf(buffer, filename, number);
119 void SimpleGraph::save(
const char* filename)
121 if (!rows.size())
throw Hermes::Exceptions::Exception(
"No data rows defined.");
123 FILE* f = fopen(filename,
"w");
124 if (f ==
nullptr)
throw Hermes::Exceptions::Exception(
"Error writing to %s.", filename);
126 for (
unsigned int i = 0; i < rows.size(); i++)
128 int rsize = rows[i].data.size();
129 for (
int j = 0; j < rsize; j++)
130 fprintf(f,
"%.14g %.14g\n", rows[i].data[j].x, rows[i].data[j].y);
135 this->info(
"Graph saved to file '%s'.", filename);
138 void MatlabGraph::save(
const char* filename)
142 if (!rows.size())
throw Hermes::Exceptions::Exception(
"No data rows defined.");
144 FILE* f = fopen(filename,
"w");
145 if (f ==
nullptr)
throw Hermes::Exceptions::Exception(
"Error writing to %s", filename);
149 else if (logx && !logy)
150 fprintf(f,
"semilogx(");
151 else if (!logx && logy)
152 fprintf(f,
"semilogy(");
154 fprintf(f,
"loglog(");
156 for (
unsigned int i = 0; i < rows.size(); i++)
159 int rsize = rows[i].data.size();
160 for (k = 0; k < 2; k++)
162 for (j = 0; j < rsize; j++)
164 fprintf(f,
"%.14g", k ? rows[i].data[j].y : rows[i].data[j].x);
165 if (j < rsize - 1) fprintf(f,
", ");
167 fprintf(f, (!k ?
"],[" :
"], '"));
169 fprintf(f,
"%s%s%s'", rows[i].color.c_str(), rows[i].line.c_str(), rows[i].marker.c_str());
170 if (i < rows.size() - 1) fprintf(f,
", ");
174 if (title.length()) fprintf(f,
"title('%s');\n", title.c_str());
175 if (xname.length()) fprintf(f,
"xlabel('%s');\n", xname.c_str());
176 if (yname.length()) fprintf(f,
"ylabel('%s');\n", yname.c_str());
178 if (legend && (rows.size() > 1 || rows[0].name.length()))
180 fprintf(f,
"legend(");
181 for (
unsigned int i = 0; i < rows.size(); i++)
183 fprintf(f,
"'%s'", rows[i].name.c_str());
184 if (i < rows.size() - 1) fprintf(f,
", ");
189 fprintf(f,
"legend off;\n");
191 fprintf(f,
"grid %s;\n", grid ?
"on" :
"off");
195 this->info(
"Graph saved. Run the file '%s' in Matlab.", filename);
203 else if (line ==
":")
206 else if (line ==
"-.")
209 else if (line ==
"--")
216 if (mark ==
".") pt = 7;
218 else if (mark ==
"o") pt = 6;
220 else if (mark ==
"O") pt = 7;
222 else if (mark ==
"x") pt = 2;
224 else if (mark ==
"+") pt = 1;
226 else if (mark ==
"*") pt = 3;
228 else if (mark ==
"s") pt = 4;
230 else if (mark ==
"S") pt = 5;
232 else if (mark ==
"d") pt = 10;
234 else if (mark ==
"D") pt = 11;
236 else if (mark ==
"v") pt = 12;
238 else if (mark ==
"V") pt = 13;
240 else if (mark ==
"^") pt = 9;
242 else if (mark ==
"<") pt = 12;
244 else if (mark ==
">") pt = 8;
246 else if (mark ==
"p") pt = 14;
248 else if (mark ==
"P") pt = 15;
252 if (col ==
"k") ct = -1;
254 else if (col ==
"b") ct = 3;
256 else if (col ==
"g") ct = 2;
258 else if (col ==
"c") ct = 5;
260 else if (col ==
"m") ct = 4;
262 else if (col ==
"y") ct = 6;
264 else if (col ==
"r") ct = 1;
272 if (
legend_pos.length() && !legend) legend =
true;
275 void GnuplotGraph::save(
const char* filename)
279 if (!rows.size())
throw Hermes::Exceptions::Exception(
"No data rows defined.");
281 FILE* f = fopen(filename,
"w");
282 if (f ==
nullptr)
throw Hermes::Exceptions::Exception(
"Error writing to %s", filename);
286 int len = strlen(filename);
287 char* outname = malloc_with_check<char>(len + 10);
288 strcpy(outname, filename);
289 char* slash = strrchr(outname,
'/');
290 if (slash !=
nullptr) strcpy(outname, ++slash);
291 char* dot = strrchr(outname,
'.');
292 if (dot !=
nullptr && dot > outname) *dot = 0;
293 strcat(outname,
".eps");
295 fprintf(f,
"set output '%s'\n", (
char*)outname);
296 free_with_check(outname);
298 fprintf(f,
"set size 0.8, 0.8\n");
301 fprintf(f,
"set logscale x\n");
302 else if (!logx && logy)
303 fprintf(f,
"set logscale y\n");
304 else if (logx && logy)
306 fprintf(f,
"set logscale x\n");
307 fprintf(f,
"set logscale y\n");
310 if (grid) fprintf(f,
"set grid\n");
312 if (title.length()) fprintf(f,
"set title '%s'\n", title.c_str());
313 if (xname.length()) fprintf(f,
"set xlabel '%s'\n", xname.c_str());
314 if (yname.length()) fprintf(f,
"set ylabel '%s'\n", yname.c_str());
318 for (
unsigned int i = 0; i < rows.size(); i++)
321 get_style_types(rows[i].line, rows[i].marker, rows[i].color, lt, pt, ct);
324 fprintf(f,
" '-' w p pointtype %d", pt);
326 fprintf(f,
" '-' w lp linewidth %g linetype %d pointtype %d", this->
lw, lt, pt);
328 fprintf(f,
" '-' w lp linewidth %g linecolor %d linetype %d pointtype %d", this->
lw, ct, lt, pt);
331 fprintf(f,
" title '%s' ", rows[i].name.c_str());
333 fprintf(f,
" notitle ");
335 if (i < rows.size() - 1) fprintf(f,
",\\\n ");
339 for (
unsigned int i = 0; i < rows.size(); i++)
341 int rsize = rows[i].data.size();
342 for (j = 0; j < rsize; j++)
343 fprintf(f,
"%.14g %.14g\n", rows[i].data[j].x, rows[i].data[j].y);
347 fprintf(f,
"set terminal x11\n");
350 this->info(
"Graph saved. Process the file '%s' with gnuplot.", filename);
353 PNGGraph::PNGGraph(
const char* title,
const char* x_axis_name,
const char* y_axis_name,
const double lines_width,
354 double plot_width,
double plot_height) : GnuplotGraph(title, x_axis_name, y_axis_name, lines_width)
356 std::stringstream sstm;
357 sstm <<
"set terminal png font arial 12 size " << plot_width <<
"," << plot_height <<
" crop enhanced\n";
Common definitions for Hermes2D.
::xsd::cxx::tree::buffer< char > buffer
Binary buffer type.
::xsd::cxx::tree::name< char, token > name
C++ type corresponding to the Name XML Schema built-in type.
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.
void set_legend_pos(const char *posspec)