INET Framework for OMNeT++/OMNEST
PlotFigure Class Reference

#include <PlotFigure.h>

Inheritance diagram for PlotFigure:
inet::IIndicatorFigure

Classes

struct  Tick
 

Public Member Functions

 PlotFigure (const char *name=nullptr)
 
virtual ~PlotFigure ()
 
virtual void parse (cProperty *property) override
 
const char ** getAllowedPropertyKeys () const override
 
virtual void refreshDisplay () override
 
virtual void setValue (int series, simtime_t timestamp, double value) override
 
const Rectangle & getBounds () const
 
void setBounds (const Rectangle &rect)
 
const Color & getBackgrouncColor () const
 
void setBackgroundColor (const Color &color)
 
double getValueTickSize () const
 
void setValueTickSize (double size)
 
simtime_t getTimeWindow () const
 
void setTimeWindow (simtime_t timeWindow)
 
simtime_t getTimeTickSize () const
 
void setTimeTickSize (simtime_t size)
 
const Color & getLineColor () const
 
void setLineColor (const Color &color)
 
double getMinValue () const
 
void setMinValue (double value)
 
double getMaxValue () const
 
void setMaxValue (double value)
 
const char * getLabel () const
 
void setLabel (const char *text)
 
const int getLabelOffset () const
 
void setLabelOffset (int offset)
 
const Font & getLabelFont () const
 
void setLabelFont (const Font &font)
 
const Color & getLabelColor () const
 
void setLabelColor (const Color &color)
 
- Public Member Functions inherited from inet::IIndicatorFigure
virtual ~IIndicatorFigure ()
 
virtual int getNumSeries () const
 

Protected Member Functions

void redrawValueTicks ()
 
void redrawTimeTicks ()
 
void addChildren ()
 
void layout ()
 
void refresh ()
 

Private Attributes

cPathFigure * plotFigure
 
cTextFigure * labelFigure
 
cRectangleFigure * backgroundFigure
 
std::vector< TicktimeTicks
 
std::vector< TickvalueTicks
 
simtime_t timeWindow = 10
 
double valueTickSize = 2.5
 
simtime_t timeTickSize = 3
 
int labelOffset = 0
 
double numberSizeFactor = 1
 
double min = 0
 
double max = 10
 
std::list< std::pair< simtime_t, double > > values
 

Constructor & Destructor Documentation

PlotFigure::PlotFigure ( const char *  name = nullptr)
52  : cGroupFigure(name)
53 {
54  addChildren();
55 }
void addChildren()
Definition: PlotFigure.cc:256
virtual PlotFigure::~PlotFigure ( )
inlinevirtual
64 {};

Member Function Documentation

void PlotFigure::addChildren ( )
protected

Referenced by PlotFigure().

257 {
258  plotFigure = new cPathFigure("plot");
259  labelFigure = new cTextFigure("label");
260  backgroundFigure = new cRectangleFigure("bounds");
261 
262  backgroundFigure->setFilled(true);
263  backgroundFigure->setFillColor(INIT_BACKGROUND_COLOR);
264  plotFigure->setLineColor(INIT_PLOT_COLOR);
265  labelFigure->setAnchor(ANCHOR_N);
266 
267  addFigure(backgroundFigure);
268  addFigure(plotFigure);
269  addFigure(labelFigure);
270 }
cTextFigure * labelFigure
Definition: PlotFigure.h:41
cRectangleFigure * backgroundFigure
Definition: PlotFigure.h:42
cPathFigure * plotFigure
Definition: PlotFigure.h:40
const char ** PlotFigure::getAllowedPropertyKeys ( ) const
override
241 {
242  static const char *keys[32];
243  if (!keys[0]) {
244  const char *localKeys[] = {
245  PKEY_VALUE_TICK_SIZE, PKEY_TIME_WINDOW, PKEY_TIME_TICK_SIZE,
246  PKEY_LINE_COLOR, PKEY_MIN_VALUE, PKEY_MAX_VALUE, PKEY_BACKGROUND_COLOR,
247  PKEY_LABEL, PKEY_LABEL_OFFSET, PKEY_LABEL_COLOR, PKEY_LABEL_FONT,
248  PKEY_NUMBER_SIZE_FACTOR, PKEY_POS,
249  PKEY_SIZE, PKEY_ANCHOR, PKEY_BOUNDS, nullptr
250  };
251  concatArrays(keys, cGroupFigure::getAllowedPropertyKeys(), localKeys);
252  }
253  return keys;
254 }
std::vector< K > keys(const std::map< K, V > &m)
Definition: stlutils.h:96
const cFigure::Color & PlotFigure::getBackgrouncColor ( ) const
72 {
73  return backgroundFigure->getFillColor();
74 }
cRectangleFigure * backgroundFigure
Definition: PlotFigure.h:42
const cFigure::Rectangle & PlotFigure::getBounds ( ) const

Referenced by layout(), parse(), redrawTimeTicks(), redrawValueTicks(), and refresh().

58 {
59  return backgroundFigure->getBounds();
60 }
cRectangleFigure * backgroundFigure
Definition: PlotFigure.h:42
const char * PlotFigure::getLabel ( ) const
162 {
163  return labelFigure->getText();
164 }
cTextFigure * labelFigure
Definition: PlotFigure.h:41
const cFigure::Color & PlotFigure::getLabelColor ( ) const
195 {
196  return labelFigure->getColor();
197 }
cTextFigure * labelFigure
Definition: PlotFigure.h:41
const cFigure::Font & PlotFigure::getLabelFont ( ) const
185 {
186  return labelFigure->getFont();
187 }
cTextFigure * labelFigure
Definition: PlotFigure.h:41
const int PlotFigure::getLabelOffset ( ) const
172 {
173  return labelOffset;
174 }
int labelOffset
Definition: PlotFigure.h:49
const cFigure::Color & PlotFigure::getLineColor ( ) const
124 {
125  return plotFigure->getLineColor();
126 }
cPathFigure * plotFigure
Definition: PlotFigure.h:40
double PlotFigure::getMaxValue ( ) const
148 {
149  return max;
150 }
double max
Definition: PlotFigure.h:52
double PlotFigure::getMinValue ( ) const
134 {
135  return min;
136 }
double min
Definition: PlotFigure.h:51
simtime_t PlotFigure::getTimeTickSize ( ) const
110 {
111  return timeTickSize;
112 }
simtime_t timeTickSize
Definition: PlotFigure.h:48
simtime_t PlotFigure::getTimeWindow ( ) const
96 {
97  return timeWindow;
98 }
simtime_t timeWindow
Definition: PlotFigure.h:46
double PlotFigure::getValueTickSize ( ) const
82 {
83  return valueTickSize;
84 }
double valueTickSize
Definition: PlotFigure.h:47
void PlotFigure::layout ( )
protected

Referenced by setBounds(), setLabelOffset(), setMaxValue(), setMinValue(), and setValueTickSize().

281 {
283 
284  Rectangle b = getBounds();
285  double fontSize = timeTicks.size() > 0 && timeTicks[0].number ? timeTicks[0].number->getFont().pointSize : 12;
286  labelFigure->setPosition(Point(b.getCenter().x, b.y + b.height + fontSize * LABEL_Y_DISTANCE_FACTOR + labelOffset));
287 }
cTextFigure * labelFigure
Definition: PlotFigure.h:41
void redrawValueTicks()
Definition: PlotFigure.cc:289
std::vector< Tick > timeTicks
Definition: PlotFigure.h:43
const Rectangle & getBounds() const
Definition: PlotFigure.cc:57
int labelOffset
Definition: PlotFigure.h:49
Definition: Units.h:69
void PlotFigure::parse ( cProperty *  property)
overridevirtual
205 {
206  cGroupFigure::parse(property);
207 
208  const char *s;
209  if ((s = property->getValue(PKEY_NUMBER_SIZE_FACTOR)) != nullptr)
210  numberSizeFactor = atof(s);
211 
212  setBounds(parseBounds(property, getBounds()));
213 
214  if ((s = property->getValue(PKEY_BACKGROUND_COLOR)) != nullptr)
215  setBackgroundColor(parseColor(s));
216  if ((s = property->getValue(PKEY_VALUE_TICK_SIZE)) != nullptr)
217  setValueTickSize(atoi(s));
218  if ((s = property->getValue(PKEY_TIME_WINDOW)) != nullptr)
219  setTimeWindow(atoi(s));
220  if ((s = property->getValue(PKEY_LINE_COLOR)) != nullptr)
221  setLineColor(parseColor(s));
222  if ((s = property->getValue(PKEY_MIN_VALUE)) != nullptr)
223  setMinValue(atof(s));
224  if ((s = property->getValue(PKEY_MAX_VALUE)) != nullptr)
225  setMaxValue(atof(s));
226  if ((s = property->getValue(PKEY_LABEL)) != nullptr)
227  setLabel(s);
228  if ((s = property->getValue(PKEY_LABEL_OFFSET)) != nullptr)
229  setLabelOffset(atoi(s));
230  if ((s = property->getValue(PKEY_LABEL_COLOR)) != nullptr)
231  setLabelColor(parseColor(s));
232  if ((s = property->getValue(PKEY_LABEL_FONT)) != nullptr)
233  setLabelFont(parseFont(s));
234  if ((s = property->getValue(PKEY_TIME_TICK_SIZE)) != nullptr)
235  setTimeTickSize(atoi(s));
236  else
237  refresh();
238 }
void setTimeWindow(simtime_t timeWindow)
Definition: PlotFigure.cc:100
void setValueTickSize(double size)
Definition: PlotFigure.cc:86
void setLabelOffset(int offset)
Definition: PlotFigure.cc:176
void setLabelFont(const Font &font)
Definition: PlotFigure.cc:189
void setLabel(const char *text)
Definition: PlotFigure.cc:166
void setTimeTickSize(simtime_t size)
Definition: PlotFigure.cc:114
void refresh()
Definition: PlotFigure.cc:422
double numberSizeFactor
Definition: PlotFigure.h:50
void setMaxValue(double value)
Definition: PlotFigure.cc:152
void setLineColor(const Color &color)
Definition: PlotFigure.cc:128
const Rectangle & getBounds() const
Definition: PlotFigure.cc:57
void setLabelColor(const Color &color)
Definition: PlotFigure.cc:199
value< double, units::s > s
Definition: Units.h:1049
void setBounds(const Rectangle &rect)
Definition: PlotFigure.cc:62
void setMinValue(double value)
Definition: PlotFigure.cc:138
void setBackgroundColor(const Color &color)
Definition: PlotFigure.cc:76
void PlotFigure::redrawTimeTicks ( )
protected

Referenced by refresh().

359 {
360  Rectangle bounds = getBounds();
361  simtime_t minX = simTime() - timeWindow;
362 
363  double fraction = std::abs(fmod((minX / timeTickSize), 1));
364  simtime_t shifting = timeTickSize * (minX < 0 ? fraction : 1 - fraction);
365  // if fraction == 0 then shifting == timeTickSize therefore don't have to shift the time ticks
366  if (shifting == timeTickSize)
367  shifting = 0;
368 
369  int numTimeTicks = (timeWindow - shifting) / timeTickSize + 1;
370 
371  // Allocate ticks and numbers if needed
372  if (numTimeTicks > timeTicks.size())
373  while (numTimeTicks > timeTicks.size()) {
374  cLineFigure *tick = new cLineFigure("timeTick");
375  cLineFigure *dashLine = new cLineFigure("timeDashLine");
376  cTextFigure *number = new cTextFigure("timeNumber");
377 
378  dashLine->setLineStyle(LINE_DASHED);
379 
380  number->setAnchor(ANCHOR_N);
381  number->setFont(Font("", bounds.height * NUMBER_SIZE_PERCENT * numberSizeFactor));
382  tick->insertBelow(plotFigure);
383  dashLine->insertBelow(plotFigure);
384  number->insertBelow(plotFigure);
385  timeTicks.push_back(Tick(tick, dashLine, number));
386  }
387  else
388  // Add or remove figures from canvas according to previous number of ticks
389  for (int i = timeTicks.size() - 1; i >= numTimeTicks; --i) {
390  delete removeFigure(timeTicks[i].number);
391  delete removeFigure(timeTicks[i].dashLine);
392  delete removeFigure(timeTicks[i].tick);
393  timeTicks.pop_back();
394  }
395 
396  for (int i = 0; i < timeTicks.size(); ++i) {
397  double x = bounds.x + bounds.width * (i * timeTickSize + shifting) / timeWindow;
398  double y = bounds.y + bounds.height;
399  if (x > bounds.x && x < bounds.x + bounds.width) {
400  timeTicks[i].tick->setVisible(true);
401  timeTicks[i].tick->setStart(Point(x, y));
402  timeTicks[i].tick->setEnd(Point(x, y - TICK_LENGTH));
403 
404  timeTicks[i].dashLine->setVisible(true);
405  timeTicks[i].dashLine->setStart(Point(x, y - TICK_LENGTH));
406  timeTicks[i].dashLine->setEnd(Point(x, bounds.y));
407  }
408  else {
409  timeTicks[i].tick->setVisible(false);
410  timeTicks[i].dashLine->setVisible(false);
411  }
412 
413  char buf[32];
414  simtime_t number = minX.dbl() + i * timeTickSize + shifting;
415 
416  sprintf(buf, "%g", number.dbl());
417  timeTicks[i].number->setText(buf);
418  timeTicks[i].number->setPosition(Point(x, y + bounds.height * NUMBER_DISTANCE_FROM_TICK));
419  }
420 }
std::vector< Tick > timeTicks
Definition: PlotFigure.h:43
simtime_t timeTickSize
Definition: PlotFigure.h:48
double numberSizeFactor
Definition: PlotFigure.h:50
cPathFigure * plotFigure
Definition: PlotFigure.h:40
const Rectangle & getBounds() const
Definition: PlotFigure.cc:57
simtime_t timeWindow
Definition: PlotFigure.h:46
void PlotFigure::redrawValueTicks ( )
protected

Referenced by layout().

290 {
291  Rectangle bounds = getBounds();
292  int numTicks = std::abs(max - min) / valueTickSize + 1;
293 
294  int fontSize = bounds.height * NUMBER_SIZE_PERCENT * numberSizeFactor;
295 
296  double valueTickYposAdjust[2] = { 0, 0 };
297 
298  if(valueTicks.size() == 1)
299  {
300  valueTickYposAdjust[0] = - (fontSize / 2);
301  valueTickYposAdjust[1] = fontSize / 2;
302  }
303 
304  // Allocate ticks and numbers if needed
305  if (numTicks > valueTicks.size())
306  while (numTicks > valueTicks.size()) {
307  cLineFigure *tick = new cLineFigure("valueTick");
308  cLineFigure *dashLine = new cLineFigure("valueDashLine");
309  cTextFigure *number = new cTextFigure("valueNumber");
310 
311  dashLine->setLineStyle(LINE_DASHED);
312 
313  number->setAnchor(ANCHOR_W);
314  number->setFont(Font("", bounds.height * NUMBER_SIZE_PERCENT * numberSizeFactor));
315  tick->insertBelow(plotFigure);
316  dashLine->insertBelow(plotFigure);
317  number->insertBelow(plotFigure);
318  valueTicks.push_back(Tick(tick, dashLine, number));
319  }
320  else
321  // Add or remove figures from canvas according to previous number of ticks
322  for (int i = valueTicks.size() - 1; i >= numTicks; --i) {
323  delete removeFigure(valueTicks[i].number);
324  delete removeFigure(valueTicks[i].dashLine);
325  delete removeFigure(valueTicks[i].tick);
326  valueTicks.pop_back();
327  }
328 
329  for (int i = 0; i < valueTicks.size(); ++i) {
330  double x = bounds.x + bounds.width;
331  double y = bounds.y + bounds.height - bounds.height * (i * valueTickSize) / std::abs(max - min);
332  if (y > bounds.y && y < bounds.y + bounds.height) {
333  valueTicks[i].tick->setVisible(true);
334  valueTicks[i].tick->setStart(Point(x, y));
335  valueTicks[i].tick->setEnd(Point(x - TICK_LENGTH, y));
336 
337  valueTicks[i].dashLine->setVisible(true);
338  valueTicks[i].dashLine->setStart(Point(x - TICK_LENGTH, y));
339  valueTicks[i].dashLine->setEnd(Point(bounds.x, y));
340  }
341  else {
342  valueTicks[i].tick->setVisible(false);
343  valueTicks[i].dashLine->setVisible(false);
344  }
345 
346  char buf[32];
347  sprintf(buf, "%g", min + i * valueTickSize);
348  valueTicks[i].number->setText(buf);
349  valueTicks[i].number->setPosition(Point(x + bounds.height * NUMBER_DISTANCE_FROM_TICK, y + valueTickYposAdjust[i % 2]));
350  }
351 }
double max
Definition: PlotFigure.h:52
double valueTickSize
Definition: PlotFigure.h:47
double min
Definition: PlotFigure.h:51
double numberSizeFactor
Definition: PlotFigure.h:50
std::vector< Tick > valueTicks
Definition: PlotFigure.h:44
cPathFigure * plotFigure
Definition: PlotFigure.h:40
const Rectangle & getBounds() const
Definition: PlotFigure.cc:57
void PlotFigure::refresh ( )
protected

Referenced by parse(), refreshDisplay(), setTimeTickSize(), setTimeWindow(), and setValue().

423 {
424  // timeTicks + timeNumbers
425  redrawTimeTicks();
426 
427  // plot
428  simtime_t minX = simTime() - timeWindow;
429  simtime_t maxX = simTime();
430 
431  plotFigure->clearPath();
432 
433  if (values.size() < 2)
434  return;
435  if (minX > values.front().first) {
436  values.clear();
437  return;
438  }
439 
440  auto r = getBounds();
441  auto it = values.begin();
442  double startX = r.x + r.width - (maxX - it->first).dbl() / timeWindow * r.width;
443  double startY = r.y + (max - it->second) / std::abs(max - min) * r.height;
444  plotFigure->addMoveTo(startX, startY);
445 
446  ++it;
447  do {
448  double endX = r.x + r.width - (maxX - it->first).dbl() / timeWindow * r.width;
449  double endY = r.y + (max - it->second) / std::abs(max - min) * r.height;
450 
451  double originalStartX = startX;
452  double originalStartY = startY;
453  double originalEndX = endX;
454  double originalEndY = endY;
455  if (InstrumentUtil::CohenSutherlandLineClip(startX, startY, endX, endY, r.x, r.x + r.width, r.y, r.y + r.height)) {
456  if (originalStartX != startX || originalStartY != startY)
457  plotFigure->addMoveTo(startX, startY);
458 
459  plotFigure->addLineTo(endX, endY);
460  }
461 
462  if (minX > it->first)
463  break;
464 
465  startX = originalEndX;
466  startY = originalEndY;
467  ++it;
468  } while (it != values.end());
469 
470  // Delete old elements
471  if (it != values.end())
472  values.erase(++it, values.end());
473 }
double max
Definition: PlotFigure.h:52
static bool CohenSutherlandLineClip(double &x0, double &y0, double &x1, double &y1, double xmin, double xmax, double ymin, double ymax)
Cohen–Sutherland clipping algorithm clips a line from P0 = (x0, y0) to P1 = (x1, y1) against a recta...
Definition: InstrumentUtil.cc:38
void redrawTimeTicks()
Definition: PlotFigure.cc:358
double min
Definition: PlotFigure.h:51
cPathFigure * plotFigure
Definition: PlotFigure.h:40
const Rectangle & getBounds() const
Definition: PlotFigure.cc:57
std::list< std::pair< simtime_t, double > > values
Definition: PlotFigure.h:53
simtime_t timeWindow
Definition: PlotFigure.h:46
void PlotFigure::refreshDisplay ( )
overridevirtual

Reimplemented from inet::IIndicatorFigure.

354 {
355  refresh();
356 }
void refresh()
Definition: PlotFigure.cc:422
void PlotFigure::setBackgroundColor ( const Color &  color)

Referenced by parse().

77 {
78  backgroundFigure->setFillColor(color);
79 }
cRectangleFigure * backgroundFigure
Definition: PlotFigure.h:42
void PlotFigure::setBounds ( const Rectangle &  rect)

Referenced by parse().

63 {
64  if (backgroundFigure->getBounds() == rect)
65  return;
66 
67  backgroundFigure->setBounds(rect);
68  layout();
69 }
cRectangleFigure * backgroundFigure
Definition: PlotFigure.h:42
void layout()
Definition: PlotFigure.cc:280
void PlotFigure::setLabel ( const char *  text)

Referenced by parse().

167 {
168  labelFigure->setText(text);
169 }
cTextFigure * labelFigure
Definition: PlotFigure.h:41
void PlotFigure::setLabelColor ( const Color &  color)

Referenced by parse().

200 {
201  labelFigure->setColor(color);
202 }
cTextFigure * labelFigure
Definition: PlotFigure.h:41
void PlotFigure::setLabelFont ( const Font &  font)

Referenced by parse().

190 {
191  labelFigure->setFont(font);
192 }
cTextFigure * labelFigure
Definition: PlotFigure.h:41
void PlotFigure::setLabelOffset ( int  offset)

Referenced by parse().

177 {
178  if(labelOffset != offset) {
179  labelOffset = offset;
180  layout();
181  }
182 }
int labelOffset
Definition: PlotFigure.h:49
void layout()
Definition: PlotFigure.cc:280
void PlotFigure::setLineColor ( const Color &  color)

Referenced by parse().

129 {
130  plotFigure->setLineColor(color);
131 }
cPathFigure * plotFigure
Definition: PlotFigure.h:40
void PlotFigure::setMaxValue ( double  value)

Referenced by parse().

153 {
154  if (max == value)
155  return;
156 
157  max = value;
158  layout();
159 }
double max
Definition: PlotFigure.h:52
void layout()
Definition: PlotFigure.cc:280
void PlotFigure::setMinValue ( double  value)

Referenced by parse().

139 {
140  if (min == value)
141  return;
142 
143  min = value;
144  layout();
145 }
double min
Definition: PlotFigure.h:51
void layout()
Definition: PlotFigure.cc:280
void PlotFigure::setTimeTickSize ( simtime_t  size)

Referenced by parse().

115 {
116  if (timeTickSize == size)
117  return;
118 
119  timeTickSize = size;
120  refresh();
121 }
simtime_t timeTickSize
Definition: PlotFigure.h:48
void refresh()
Definition: PlotFigure.cc:422
void PlotFigure::setTimeWindow ( simtime_t  timeWindow)

Referenced by parse().

101 {
102  if (this->timeWindow == timeWindow)
103  return;
104 
105  this->timeWindow = timeWindow;
106  refresh();
107 }
void refresh()
Definition: PlotFigure.cc:422
simtime_t timeWindow
Definition: PlotFigure.h:46
void PlotFigure::setValue ( int  series,
simtime_t  timestamp,
double  value 
)
overridevirtual

Implements inet::IIndicatorFigure.

273 {
274  ASSERT(series == 0);
275 
276  values.push_front(std::pair<simtime_t, double>(timestamp, value));
277  refresh();
278 }
void refresh()
Definition: PlotFigure.cc:422
std::list< std::pair< simtime_t, double > > values
Definition: PlotFigure.h:53
void PlotFigure::setValueTickSize ( double  size)

Referenced by parse().

87 {
88  if (valueTickSize == size)
89  return;
90 
91  valueTickSize = size;
92  layout();
93 }
double valueTickSize
Definition: PlotFigure.h:47
void layout()
Definition: PlotFigure.cc:280

Member Data Documentation

cRectangleFigure* PlotFigure::backgroundFigure
private
cTextFigure* PlotFigure::labelFigure
private
int PlotFigure::labelOffset = 0
private
double PlotFigure::max = 10
private
double PlotFigure::min = 0
private
double PlotFigure::numberSizeFactor = 1
private
cPathFigure* PlotFigure::plotFigure
private
std::vector<Tick> PlotFigure::timeTicks
private

Referenced by layout(), and redrawTimeTicks().

simtime_t PlotFigure::timeTickSize = 3
private
simtime_t PlotFigure::timeWindow = 10
private
std::list<std::pair<simtime_t, double> > PlotFigure::values
private

Referenced by refresh(), and setValue().

std::vector<Tick> PlotFigure::valueTicks
private

Referenced by redrawValueTicks().

double PlotFigure::valueTickSize = 2.5
private

The documentation for this class was generated from the following files: