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