A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
gnuplot.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA, 2008 Timo Bingmann
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Original Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 * Enhancements: Timo Bingmann <timo.bingmann@student.kit.edu>
19 */
20#ifndef GNUPLOT_H
21#define GNUPLOT_H
22
23#include <string>
24#include <utility>
25#include <vector>
26
27namespace ns3
28{
29
30/**
31 * \ingroup gnuplot
32 *
33 * \brief Abstract class to store a plot line to be used by ns3::Gnuplot.
34 *
35 * This class contains a reference counted data object in m_data. The data
36 * object contains different structs derived from struct Data by subclasses.
37 */
39{
40 public:
41 /**
42 * Reference-counting copy constructor.
43 * \param original Original GnuPlotDataset
44 */
45 GnuplotDataset(const GnuplotDataset& original);
46
47 /**
48 * Reference-counting destructor.
49 */
51
52 /**
53 * Reference-counting assignment operator.
54 * \param original Right-hand side of assignment operator
55 * \return Copy of original GnuplotDataset
56 */
57 GnuplotDataset& operator=(const GnuplotDataset& original);
58
59 /**
60 * \brief Change line title.
61 * \param title the new title string to use for this dataset.
62 *
63 * \note If you want your title to contain a newline character,
64 * escape it like this: "First line\\nSecond line" so that
65 * it is converted to "First line\nSecond line" in the plot file.
66 */
67 void SetTitle(const std::string& title);
68
69 /**
70 * \brief Change extra formatting style parameters for newly created objects.
71 * \param extra extra formatting
72 */
73 static void SetDefaultExtra(const std::string& extra);
74
75 /**
76 * \brief Add extra formatting parameters to this dataset.
77 * \param extra extra formatting
78 */
79 void SetExtra(const std::string& extra);
80
81 protected:
82 /// Friend because it accesses m_data and it's virtual functions directly in
83 /// GenerateOutput().
84 friend class Gnuplot;
85
86 /**
87 * \brief Extra gnuplot parameters set on every newly created dataset.
88 */
89 static std::string m_defaultExtra;
90
91 /**
92 * \brief Derived classes subclass this struct and add their own data fields.
93 */
94 struct Data;
95
96 /**
97 * Called by constructors of derived classes.
98 * \param data the reference counted data object representing this dataset.
99 */
101
102 /**
103 * Reference counted data object.
104 */
106};
107
108/**
109 * \ingroup gnuplot
110 *
111 * \class Gnuplot2dDataset
112 * \brief Class to represent a 2D points plot. Set the line or points style
113 * using SetStyle() and set points using Add().
114 */
116{
117 public:
118 /**
119 * The plotting style to use for this dataset.
120 */
121 enum Style
122 {
131 };
132
133 /**
134 * Whether errorbars should be used for this dataset.
135 */
137 {
141 XY
142 };
143
144 /**
145 * \param title the title to be associated to this dataset (default "Untitled").
146 *
147 * Create an empty dataset. Usually, the dataset's title is
148 * displayed in the legend box.
149 */
150 Gnuplot2dDataset(const std::string& title = "Untitled");
151
152 /**
153 * Change default style for all newly created objects.
154 * \param style the style of plotting to use for newly created datasets.
155 */
156 static void SetDefaultStyle(Style style);
157
158 /**
159 * \param style the style of plotting to use for this dataset.
160 */
161 void SetStyle(Style style);
162
163 /**
164 * Change default errorbars style for all newly created objects.
165 * \param errorBars the style of errorbars to use for newly created datasets.
166 */
167 static void SetDefaultErrorBars(ErrorBars errorBars);
168
169 /**
170 * \param errorBars the style of errorbars to display.
171 *
172 * If you use any style other than none, you need
173 * to make sure you store the delta information in
174 * this dataset with the right GnuplotDataset::Add
175 * method.
176 */
177 void SetErrorBars(ErrorBars errorBars);
178
179 /**
180 * \param x x coord to new data point
181 * \param y y coord to new data point
182 *
183 * Use this method with error bar style NONE.
184 */
185 void Add(double x, double y);
186
187 /**
188 * \param x x coord to new data point
189 * \param y y coord to new data point
190 * \param errorDelta x and y data point uncertainty
191 *
192 * Use this method with error bar style X or Y.
193 */
194 void Add(double x, double y, double errorDelta);
195
196 /**
197 * \param x x coord to new data point
198 * \param y y coord to new data point
199 * \param xErrorDelta x data point uncertainty
200 * \param yErrorDelta y data point uncertainty
201 *
202 * Use this method with error bar style XY.
203 */
204 void Add(double x, double y, double xErrorDelta, double yErrorDelta);
205
206 /**
207 * Add an empty line in the data output sequence. Empty lines in the plot
208 * data break continuous lines and do other things in the output.
209 */
210 void AddEmptyLine();
211
212 private:
213 /**
214 * A point in a 2D plot
215 */
216 struct Point
217 {
218 bool empty; //!< the point is empty
219 double x; //!< X coordinate
220 double y; //!< Y coordinate
221 double dx; //!< X error delta
222 double dy; //!< Y error delta
223 };
224
225 /// The set of points in the dataset
226 typedef std::vector<Point> PointSet;
227
228 static Style m_defaultStyle; //!< default plot style
229 static ErrorBars m_defaultErrorBars; //!< default error bars type
230
231 /// Forward declaration of the internal data class.
232 struct Data2d;
233};
234
235/**
236 * \ingroup gnuplot
237 *
238 * \brief Class to represent a 2D function expression plot.
239 *
240 * Since the function expression is not escaped, styles and extras could just
241 * as well be included in the expression string.
242 */
244{
245 public:
246 /**
247 * \param title the title to be associated to this dataset.
248 * \param function function to plot
249 *
250 * Create an function dataset. Usually, the dataset's title is displayed in
251 * the legend box.
252 */
253 Gnuplot2dFunction(const std::string& title = "Untitled", const std::string& function = "");
254
255 /**
256 * \param function new function string to set
257 */
258 void SetFunction(const std::string& function);
259
260 private:
261 /// Forward declaration of the internal data class.
262 struct Function2d;
263};
264
265/**
266 * \ingroup gnuplot
267 *
268 * \brief Class to represent a 3D points plot. Set the line or points style
269 * using SetStyle() and set points using Add().
270 */
272{
273 public:
274 /**
275 * \param title the title to be associated to this dataset.
276 *
277 * Create an empty dataset. Usually, the dataset's title is
278 * displayed in the legend box.
279 */
280 Gnuplot3dDataset(const std::string& title = "Untitled");
281
282 /**
283 * Change default style for all newly created objects.
284 * \param style the style of plotting to use for newly created datasets.
285 */
286 static void SetDefaultStyle(const std::string& style);
287
288 /**
289 * \param style the style of plotting to use for this dataset.
290 */
291 void SetStyle(const std::string& style);
292
293 /**
294 * \param x x coord to new data point
295 * \param y y coord to new data point
296 * \param z z coord to new data point
297 *
298 * Use this method to add a new 3D point
299 */
300 void Add(double x, double y, double z);
301
302 /**
303 * Add an empty line in the data output sequence. Empty lines in the plot
304 * data break continuous lines and do other things in the output.
305 */
306 void AddEmptyLine();
307
308 private:
309 /**
310 * A point in a 3D plot
311 */
312 struct Point
313 {
314 bool empty; //!< the point is empty
315 double x; //!< X coordinate
316 double y; //!< Y coordinate
317 double z; //!< Z coordinate
318 };
319
320 /// The set of points in the dataset
321 typedef std::vector<Point> PointSet;
322
323 static std::string m_defaultStyle; //!< default plot style
324
325 /// Forward declaration of the internal data class.
326 struct Data3d;
327};
328
329/**
330 * \ingroup gnuplot
331 *
332 * \brief Class to represent a 3D function expression plot.
333 *
334 * Since the function expression is not escaped, styles and extras could just as
335 * well be included in the expression string. The only difference to
336 * Gnuplot2dFunction is the splot command string.
337 */
339{
340 public:
341 /**
342 * \param title the title to be associated to this dataset.
343 * \param function function to plot
344 *
345 * Create an function dataset. Usually, the dataset's title is displayed in
346 * the legend box.
347 */
348 Gnuplot3dFunction(const std::string& title = "Untitled", const std::string& function = "");
349
350 /**
351 * \param function new function string to set
352 */
353 void SetFunction(const std::string& function);
354
355 private:
356 /// Forward declaration of the internal data class.
357 struct Function3d;
358};
359
360/**
361 * \ingroup gnuplot
362 *
363 * \brief a simple class to generate gnuplot-ready plotting commands
364 * from a set of datasets.
365 *
366 * This class really represents a single graph on which multiple datasets
367 * can be plotted.
368 */
370{
371 public:
372 /**
373 * \param outputFilename the name of the file where the rendering of the
374 * graph will be generated if you feed the command stream output by
375 * Gnuplot::GenerateOutput to the gnuplot program.
376 * \param title title line of the plot page
377 */
378 Gnuplot(const std::string& outputFilename = "", const std::string& title = "");
379
380 /**
381 * \param outputFilename the name of the file where the rendering of the
382 * graph will be generated if you feed the command stream output by
383 * Gnuplot::GenerateOutput to the gnuplot program.
384 */
385 void SetOutputFilename(const std::string& outputFilename);
386
387 /**
388 * Crude attempt to auto-detect the correct terminal setting by inspecting
389 * the filename's extension.
390 * \param filename output filename
391 * \return File extension of the provided filename
392 */
393 static std::string DetectTerminal(const std::string& filename);
394
395 /**
396 * \param terminal terminal setting string for output. The default terminal
397 * string is "png"
398 */
399 void SetTerminal(const std::string& terminal);
400
401 /**
402 * \param title set new plot title string to use for this plot.
403 */
404 void SetTitle(const std::string& title);
405
406 /**
407 * \param xLegend the legend for the x horizontal axis
408 * \param yLegend the legend for the y vertical axis
409 */
410 void SetLegend(const std::string& xLegend, const std::string& yLegend);
411
412 /**
413 * \param extra set extra gnuplot directive for output.
414 */
415 void SetExtra(const std::string& extra);
416
417 /**
418 * \param extra append extra gnuplot directive for output.
419 */
420 void AppendExtra(const std::string& extra);
421
422 /**
423 * \param dataset add a dataset to the graph to be plotted.
424 */
425 void AddDataset(const GnuplotDataset& dataset);
426
427 /**
428 * \param os the output stream on which the relevant gnuplot
429 * commands should be generated. Including output file and terminal
430 * headers.
431 *
432 * \brief Writes gnuplot commands and data values to a single
433 * output stream.
434 */
435 void GenerateOutput(std::ostream& os);
436
437 /**
438 * \param osControl the output stream on which the relevant gnuplot
439 * control commands should be generated. Including output file and
440 * terminal headers.
441 * \param osData the output stream on which the relevant gnuplot
442 * data values should be generated.
443 * \param dataFileName the name for the data file that will be
444 * written.
445 *
446 * \brief Writes gnuplot commands and data values to two
447 * different outputs streams.
448 */
449 void GenerateOutput(std::ostream& osControl, std::ostream& osData, std::string dataFileName);
450
451 /**
452 * \param index the index for the data stream in the data file.
453 *
454 * \brief Sets the current data stream index in the data file.
455 */
456 void SetDataFileDatasetIndex(unsigned int index);
457
458 private:
459 /// Type for Datasets to be used in plots
460 typedef std::vector<GnuplotDataset> Datasets;
461
462 std::string m_outputFilename; //!< Output file name
463 std::string m_terminal; //!< Gnuplot "terminal" to use
464
465 Datasets m_datasets; //!< Data sets
466
467 std::string m_title; //!< Plot title
468 std::string m_xLegend; //!< X axis legend
469 std::string m_yLegend; //!< Y axis legend
470 std::string m_extra; //!< extra parameters for the plot
471
472 bool m_generateOneOutputFile; //!< true if only one plot will be generated
473
474 unsigned int m_dataFileDatasetIndex; //!< Data set index to plot
475};
476
477/**
478 * \ingroup gnuplot
479 *
480 * \brief a simple class to group together multiple gnuplots into one file,
481 * e.g. for PDF multi-page output terminals.
482 */
484{
485 public:
486 /**
487 * \param outputFilename the name of the file where the rendering of the
488 * graph will be generated if you feed the command stream output by
489 * GnuplotCollection::GenerateOutput to the gnuplot program.
490 */
491 GnuplotCollection(const std::string& outputFilename);
492
493 /**
494 * \param terminal terminal setting string for output. The default terminal
495 * string is guessed from the output filename's extension.
496 */
497 void SetTerminal(const std::string& terminal);
498
499 /**
500 * \param plot add a plot to the collection to be plotted.
501 */
502 void AddPlot(const Gnuplot& plot);
503
504 /**
505 * Return a pointer to one of the added plots.
506 * \param id index of plot to return
507 * \return reference to plot, throws std::range_error if it does not exist.
508 */
509 Gnuplot& GetPlot(unsigned int id);
510
511 /**
512 * \param os the output stream on which the relevant gnuplot commands should
513 * be generated.
514 */
515 void GenerateOutput(std::ostream& os);
516
517 /**
518 * \param osControl the output stream on which the relevant gnuplot
519 * control commands should be generated. Including output file and
520 * terminal headers.
521 * \param osData the output stream on which the relevant gnuplot
522 * data values should be generated.
523 * \param dataFileName the name for the data file that will be
524 * written.
525 */
526 void GenerateOutput(std::ostream& osControl, std::ostream& osData, std::string dataFileName);
527
528 private:
529 /// Type of the Gnuplot collection
530 typedef std::vector<Gnuplot> Plots;
531
532 std::string m_outputFilename; //!< Output file name
533 std::string m_terminal; //!< Gnuplot "terminal" to use
534
535 Plots m_plots; //!< Plots in the collection
536};
537
538} // namespace ns3
539
540#endif /* GNUPLOT_H */
Class to represent a 2D points plot.
Definition: gnuplot.h:116
void AddEmptyLine()
Add an empty line in the data output sequence.
Definition: gnuplot.cc:420
static Style m_defaultStyle
default plot style
Definition: gnuplot.h:228
static void SetDefaultStyle(Style style)
Change default style for all newly created objects.
Definition: gnuplot.cc:353
ErrorBars
Whether errorbars should be used for this dataset.
Definition: gnuplot.h:137
static void SetDefaultErrorBars(ErrorBars errorBars)
Change default errorbars style for all newly created objects.
Definition: gnuplot.cc:365
void SetErrorBars(ErrorBars errorBars)
Definition: gnuplot.cc:371
std::vector< Point > PointSet
The set of points in the dataset.
Definition: gnuplot.h:226
void SetStyle(Style style)
Definition: gnuplot.cc:359
void Add(double x, double y)
Definition: gnuplot.cc:377
Style
The plotting style to use for this dataset.
Definition: gnuplot.h:122
static ErrorBars m_defaultErrorBars
default error bars type
Definition: gnuplot.h:229
Class to represent a 2D function expression plot.
Definition: gnuplot.h:244
void SetFunction(const std::string &function)
Definition: gnuplot.cc:507
Class to represent a 3D points plot.
Definition: gnuplot.h:272
void AddEmptyLine()
Add an empty line in the data output sequence.
Definition: gnuplot.cc:633
std::vector< Point > PointSet
The set of points in the dataset.
Definition: gnuplot.h:321
static std::string m_defaultStyle
default plot style
Definition: gnuplot.h:323
void Add(double x, double y, double z)
Definition: gnuplot.cc:622
static void SetDefaultStyle(const std::string &style)
Change default style for all newly created objects.
Definition: gnuplot.cc:610
void SetStyle(const std::string &style)
Definition: gnuplot.cc:616
Class to represent a 3D function expression plot.
Definition: gnuplot.h:339
void SetFunction(const std::string &function)
Definition: gnuplot.cc:720
a simple class to group together multiple gnuplots into one file, e.g.
Definition: gnuplot.h:484
std::string m_outputFilename
Output file name.
Definition: gnuplot.h:532
void AddPlot(const Gnuplot &plot)
Definition: gnuplot.cc:919
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:913
Gnuplot & GetPlot(unsigned int id)
Return a pointer to one of the added plots.
Definition: gnuplot.cc:925
std::string m_terminal
Gnuplot "terminal" to use.
Definition: gnuplot.h:533
Plots m_plots
Plots in the collection.
Definition: gnuplot.h:535
void GenerateOutput(std::ostream &os)
Definition: gnuplot.cc:938
std::vector< Gnuplot > Plots
Type of the Gnuplot collection.
Definition: gnuplot.h:530
Abstract class to store a plot line to be used by ns3::Gnuplot.
Definition: gnuplot.h:39
static void SetDefaultExtra(const std::string &extra)
Change extra formatting style parameters for newly created objects.
Definition: gnuplot.cc:154
GnuplotDataset & operator=(const GnuplotDataset &original)
Reference-counting assignment operator.
Definition: gnuplot.cc:132
void SetExtra(const std::string &extra)
Add extra formatting parameters to this dataset.
Definition: gnuplot.cc:160
Data * m_data
Reference counted data object.
Definition: gnuplot.h:105
static std::string m_defaultExtra
Extra gnuplot parameters set on every newly created dataset.
Definition: gnuplot.h:89
void SetTitle(const std::string &title)
Change line title.
Definition: gnuplot.cc:148
~GnuplotDataset()
Reference-counting destructor.
Definition: gnuplot.cc:123
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:370
std::string m_yLegend
Y axis legend.
Definition: gnuplot.h:469
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:796
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:776
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:764
std::string m_terminal
Gnuplot "terminal" to use.
Definition: gnuplot.h:463
std::string m_extra
extra parameters for the plot
Definition: gnuplot.h:470
unsigned int m_dataFileDatasetIndex
Data set index to plot.
Definition: gnuplot.h:474
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:789
Datasets m_datasets
Data sets.
Definition: gnuplot.h:465
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:802
std::string m_title
Plot title.
Definition: gnuplot.h:467
std::vector< GnuplotDataset > Datasets
Type for Datasets to be used in plots.
Definition: gnuplot.h:460
void SetDataFileDatasetIndex(unsigned int index)
Sets the current data stream index in the data file.
Definition: gnuplot.cc:899
std::string m_xLegend
X axis legend.
Definition: gnuplot.h:468
std::string m_outputFilename
Output file name.
Definition: gnuplot.h:462
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:783
void SetTitle(const std::string &title)
Definition: gnuplot.cc:770
void SetOutputFilename(const std::string &outputFilename)
Definition: gnuplot.cc:737
bool m_generateOneOutputFile
true if only one plot will be generated
Definition: gnuplot.h:472
static std::string DetectTerminal(const std::string &filename)
Crude attempt to auto-detect the correct terminal setting by inspecting the filename's extension.
Definition: gnuplot.cc:743
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]
Structure storing the data to for a 2D plot.
Definition: gnuplot.cc:173
A point in a 2D plot.
Definition: gnuplot.h:217
double y
Y coordinate.
Definition: gnuplot.h:220
double dx
X error delta.
Definition: gnuplot.h:221
bool empty
the point is empty
Definition: gnuplot.h:218
double x
X coordinate.
Definition: gnuplot.h:219
double dy
Y error delta.
Definition: gnuplot.h:222
Structure storing the function to be used for a 2D plot.
Definition: gnuplot.cc:435
Structure storing the data for a 3D plot.
Definition: gnuplot.cc:520
A point in a 3D plot.
Definition: gnuplot.h:313
bool empty
the point is empty
Definition: gnuplot.h:314
double x
X coordinate.
Definition: gnuplot.h:315
double z
Z coordinate.
Definition: gnuplot.h:317
double y
Y coordinate.
Definition: gnuplot.h:316
Structure storing the function to be used for a 3D plot.
Definition: gnuplot.cc:648
Structure storing the data to plot.
Definition: gnuplot.cc:39