Minsky
minsky::PlotWidget Class Reference

a container item for a plot widget More...

#include <plotWidget.h>

Inheritance diagram for minsky::PlotWidget:
Inheritance graph
Collaboration diagram for minsky::PlotWidget:
Collaboration graph

Public Member Functions

void labelPen (size_t pen, const std::string &label)
 
 PlotWidget ()
 
void addPorts ()
 
 PlotWidget (const PlotWidget &x)
 
 PlotWidget (PlotWidget &&x)
 
float width () const
 
float height () const
 
const PlotWidgetplotWidgetCast () const override
 
PlotWidgetplotWidgetCast () override
 
std::string const & xlabel () const
 shadowed label commands to allow latex intepretation More...
 
std::string const & xlabel (const std::string &x)
 
std::string const & ylabel () const
 
std::string const & ylabel (const std::string &x)
 
std::string const & y1label () const
 
std::string const & y1label (const std::string &x)
 
void addPlotPt (double t)
 add another plot point More...
 
void updateIcon (double t) override
 update display after a step() More...
 
void addConstantCurves ()
 add vector/tensor curves to plot More...
 
void connectVar (const std::shared_ptr< VariableValue > &var, unsigned port)
 connect variable var to port port. More...
 
void disconnectAllVars ()
 
void draw (cairo_t *cairo) const override
 draw this item into a cairo context More...
 
void requestRedraw ()
 redraw plot using current data to all open windows More...
 
void redrawWithBounds () override
 
void makeDisplayPlot ()
 add this as a display plot to its group More...
 
void resize (const LassoBox &) override
 resize this item on the canvas More...
 
ClickType::Type clickType (float x, float y) const override
 returns the clicktype given a mouse click at x, y. More...
 
bool contains (float x, float y) const override
 
void autoScale ()
 set autoscaling More...
 
void scalePlot ()
 sets the plot scale and pen labels More...
 
void exportAsCSV (const string &filename)
 export the plotted data as a CSV file More...
 
void destroyFrame () override
 destroy any popup windows associated with this More...
 
std::set< std::string > availableMarkers () const
 return list of variables that could be attached to markers More...
 
virtual float x () const
 
virtual float y () const
 
size_t numLines () const
 number of input ports along a side More...
 
size_t numLines (size_t n)
 number of input ports along a side More...
 
double barWidth () const
 bar chart bar width control More...
 
double barWidth (double w)
 bar chart bar width control More...
 
void mouseDown (float, float) override
 handle mouse events More...
 
void mouseMove (float, float) override
 handle mouse events More...
 
void mouseUp (float x, float y) override
 handle mouse events More...
 
bool onMouseOver (float, float) override
 handle mouse events More...
 
void onMouseLeave () override
 handle mouse events More...
 
- Public Member Functions inherited from minsky::ItemT< PlotWidget >
std::string classType () const override
 
ItemTclone () const override
 
void TCL_obj (classdesc::TCL_obj_t &t, const std::string &d) override
 
void RESTProcess (classdesc::RESTProcess_t &rp, const std::string &d) override
 dummy template definition for use with gui executable More...
 
void RESTProcess (classdesc::RESTProcess_t &rp, const std::string &d) const override
 
void json_pack (classdesc::json_pack_t &j) const override
 
 ItemT ()=default
 
 ItemT (const ItemT &)=default
 
 ItemT (ItemT &&)=delete
 
ItemToperator= (const ItemT &)=default
 
ItemToperator= (ItemT &&)=delete
 
- Public Member Functions inherited from minsky::Item
 Item ()
 
virtual std::weak_ptr< Portports (std::size_t i) const
 callback to be run when item deleted from group More...
 
std::size_t portsSize () const
 number of ports More...
 
float portX (std::size_t i)
 
float portY (std::size_t i)
 
bool contains (const Point &p) const
 
void updateBoundingBox () override
 
void throw_error (const std::string &) const
 mark item on canvas, then throw More...
 
virtual bool ioVar () const
 indicates this is a group I/O variable More...
 
virtual double value () const
 current value of output port More...
 
double rotation () const
 
double rotation (const double &r)
 
std::pair< double, bool > rotationAsRadians () const
 return the rotation as radians, and whether rotation should have additional straight angle added for text that stays upright. More...
 
float iWidth () const
 
float iWidth (const float &w)
 
float iHeight () const
 
float iHeight (const float &h)
 
virtual void flip ()
 rotate icon though 180∘ More...
 
std::string id () const
 return an id uniquely identifying this item More...
 
virtual float x () const
 
virtual float y () const
 
virtual float zoomFactor () const
 
void ensureBBValid () const
 
float width () const
 
float height () const
 
virtual std::vector< Pointcorners () const
 
float left () const
 
float right () const
 
float top () const
 
float bottom () const
 
std::string bookmarkId () const
 Id of bookmark associated with this. More...
 
void adjustBookmark () const override
 adjust bookmark list to reflect current configuration More...
 
float resizeHandleSize () const
 resize handles should be at least a percentage if the icon size (#1025) More...
 
virtual bool onResizeHandle (float x, float y) const
 
virtual bool inItem (float x, float y) const
 
virtual bool onItem (float x, float y) const
 returns true if (x,y) is on the icon More...
 
virtual void onMouseDown (float x, float y)
 respond to mouse down events More...
 
virtual void onMouseUp (float x, float y)
 respond to mouse up events More...
 
virtual bool onMouseMotion (float x, float y)
 respond to mouse motion events with button pressed More...
 
virtual bool onKeyPress (int keySym, const std::string &utf8, int state)
 respond to key press events More...
 
virtual void deleteAttachedWires ()
 delete all attached wires More...
 
virtual bool visible () const
 whether this item is visible on the canvas. More...
 
void moveTo (float x, float y)
 
virtual float scaleFactor () const
 factor by which item has been resized More...
 
virtual float scaleFactor (const float &sf)
 
void dummyDraw () const
 draw into a dummy cairo context, for purposes of calculating port positions More...
 
virtual void displayTooltip (cairo_t *, const std::string &) const
 display tooltip text, eg on mouseover More...
 
 Item (const Item &)=default
 
Itemoperator= (const Item &)=default
 
virtual ~Item ()
 
void drawPorts (cairo_t *cairo) const
 
virtual void drawResizeHandles (cairo_t *cairo) const
 
virtual std::shared_ptr< PortclosestOutPort (float x, float y) const
 returns closest output port to x,y More...
 
virtual std::shared_ptr< PortclosestInPort (float x, float y) const
 
virtual std::shared_ptr< Itemselect (float x, float y) const
 returns the variable if point (x,y) is within a visible variable icon, null otherwise. More...
 
void RESTProcess (classdesc::RESTProcess_t &rp, const std::string &d) override
 runs the RESTProcess descriptor suitable for this type More...
 
void RESTProcess (classdesc::RESTProcess_t &rp, const std::string &d) const override
 
virtual void displayDelayedTooltip (float x, float y)
 enable extended tooltip help message appropriate for mouse at (x,y) More...
 
virtual void disableDelayedTooltip ()
 
virtual bool editorMode () const
 some items have an editor mode attribute More...
 
virtual void toggleEditorMode ()
 
virtual Units units (bool check=false) const
 compute the dimensional units More...
 
Units checkUnits () const
 perform units consistency checks More...
 
ItemPtr itemPtrFromThis () const
 return a shared_ptr to this More...
 
- Public Member Functions inherited from minsky::NoteBase
virtual std::string const & detailedText () const
 
virtual std::string const & detailedText (const std::string &x)
 
virtual std::string const & tooltip () const
 
virtual std::string const & tooltip (const std::string &x)
 
virtual ~NoteBase ()=default
 
- Public Member Functions inherited from minsky::RenderNativeWindow
const RenderFrameArgsframeArgs () const
 
 ~RenderNativeWindow () override
 
virtual void init ()
 perform any initialisation of any subclasses of this More...
 
void renderFrame (const RenderFrameArgs &args)
 
void destroyFrame ()
 
void draw ()
 
void requestRedraw ()
 
void macOSXRedraw ()
 
RenderNativeWindowoperator= (const RenderNativeWindow &x)
 
 RenderNativeWindow ()=default
 
 RenderNativeWindow (const RenderNativeWindow &)=default
 
virtual bool hasScrollBars () const
 return whether this window has scrollbars (needed for MacOSX). More...
 
- Public Member Functions inherited from minsky::EventInterface
virtual void controlMouseDown (float x, float y)
 
virtual void zoom (double x, double y, double z)
 
virtual void moveTo (float x, float y)
 move this so that (x,y) is centred More...
 
virtual std::vector< float > position () const
 current centre coordinates More...
 
virtual double zoomFactor () const
 
virtual bool keyPress (const KeyPressArgs &)
 handle key press over current itemFocus, More...
 
virtual bool getItemAt (float x, float y)
 item or wire obtained by get*At() calls More...
 
virtual bool getWireAt (float x, float y)
 item or wire obtained by get*At() calls More...
 

Public Attributes

std::vector< std::vector< std::shared_ptr< VariableValue > > > yvars
 variable port attached to (if any) More...
 
std::vector< std::shared_ptr< VariableValue > > xvars
 
std::vector< std::string > penLabels
 
std::shared_ptr< VariableValuexminVar
 variable ports specifying plot size More...
 
std::shared_ptr< VariableValuexmaxVar
 
std::shared_ptr< VariableValueyminVar
 
std::shared_ptr< VariableValueymaxVar
 
std::shared_ptr< VariableValuey1minVar
 
std::shared_ptr< VariableValuey1maxVar
 
double xmin =nan("")
 Settable bounds values if above variables not connected NaN means autocalculate from data. More...
 
double xmax =xmin
 
double ymin =xmin
 
double ymax =xmin
 
double y1min =xmin
 
double y1max =xmin
 
unsigned displayNTicks {3}
 number of ticks to show in canvas item More...
 
double displayFontSize {3}
 
std::vector< std::string > horizontalMarkers
 markers at a position given by a named variable/parameter More...
 
std::vector< std::string > verticalMarkers
 
std::string title
 
PlotType plotType =automatic
 automatic means choose line or bar depending on the x-vector type. More...
 
- Public Attributes inherited from minsky::Item
float m_x =0
 
float m_y =0
 position in canvas, or within group More...
 
float m_sf =1
 scale factor of item on canvas, or within group More...
 
bool onResizeHandles =false
 set to true to indicate mouse is ovcaler resize handles More...
 
bool onBorder =false
 true to indicate mouse hovering over border More...
 
std::string deleteCallback
 
BoundingBox bb
 canvas bounding box. More...
 
- Public Attributes inherited from minsky::NoteBase
bool mouseFocus =false
 true if target of a mouseover More...
 
bool selected =false
 true if selected for cut, copy or group operation More...
 
bool bookmark =false
 Is this item also a bookmark? More...
 
- Public Attributes inherited from minsky::EventInterface
ItemPtr item
 item or wire obtained by get*At() calls More...
 
WirePtr wire
 item or wire obtained by get*At() calls More...
 

Static Public Attributes

static constexpr unsigned nBoundsPorts =6
 number of bounds (xmin/xmax, ymin/ymax, y1min/y1max) ports More...
 
- Static Public Attributes inherited from minsky::RenderNativeWindow
static ecolab::cairo::Colour backgroundColour {0.8,0.8,0.8,1}
 

Private Member Functions

 CLASSDESC_ACCESS (PlotWidget)
 
bool redraw (int, int, int, int) override
 
size_t startPen (size_t port) const
 returns to starting pen number for input port More...
 

Private Attributes

double clickX
 
double clickY
 
double oldLegendLeft
 
double oldLegendTop
 
double oldLegendFontSz
 
ClickType::Type ct
 
classdesc::Exclude< boost::posix_time::ptime > lastAdd {boost::posix_time::microsec_clock::local_time()}
 
classdesc::Exclude< boost::posix_time::ptime > lastAccumulatedBlitTime {boost::posix_time::microsec_clock::local_time()}
 
bool justDataChanged =false
 
bool xIsSecsSinceEpoch =false
 
std::string m_xlabel
 
std::string m_ylabel
 
std::string m_y1label
 
double yoffs =0
 extra offset for titles More...
 
Formatter formatter =defaultFormatter
 
size_t m_numLines =1
 number of ports on the side More...
 
bool clearPensOnLabelling =false
 

Static Private Attributes

static constexpr double portSpace =10
 

Friends

struct SchemaHelper
 
struct PlotItem
 

Additional Inherited Members

- Static Public Member Functions inherited from minsky::Item
static void drawSelected (cairo_t *cairo)
 
- Static Public Member Functions inherited from minsky::RenderNativeWindow
static double scaleFactor ()
 
- Static Protected Member Functions inherited from minsky::Item
static void drawResizeHandle (cairo_t *cairo, double x, double y, double sf, double angle)
 
- Protected Attributes inherited from minsky::Item
double m_width =10
 
double m_height =10
 
ItemPortVector m_ports
 
minsky::Item::MemoisedRotator memoisedRotator
 

Detailed Description

a container item for a plot widget

Definition at line 46 of file plotWidget.h.

Constructor & Destructor Documentation

◆ PlotWidget() [1/3]

minsky::PlotWidget::PlotWidget ( )

Definition at line 66 of file plotWidget.cc.

67  {
68  m_width=m_height=150;
69  nxTicks=nyTicks=10;
70  fontScale=2;
71  leadingMarker=true;
72  grid=true;
73  legendLeft=0.1; // override ecolab's default value
74  legendSide=boundingBox;
75  addPorts();
76 
77  yvars.resize(2*m_numLines);
78  xvars.resize(m_numLines);
79  }
double m_height
Definition: item.h:155
std::vector< std::vector< std::shared_ptr< VariableValue > > > yvars
variable port attached to (if any)
Definition: plotWidget.h:90
double m_width
Definition: item.h:155
std::vector< std::shared_ptr< VariableValue > > xvars
Definition: plotWidget.h:91
size_t m_numLines
number of ports on the side
Definition: plotWidget.h:74

◆ PlotWidget() [2/3]

minsky::PlotWidget::PlotWidget ( const PlotWidget x)
inline

Definition at line 121 of file plotWidget.h.

121 : PlotWidgetSuper(x) {addPorts();}
virtual float x() const
Definition: item.cc:107

◆ PlotWidget() [3/3]

minsky::PlotWidget::PlotWidget ( PlotWidget &&  x)
inline

Definition at line 122 of file plotWidget.h.

122 : PlotWidgetSuper(x) {addPorts();}
virtual float x() const
Definition: item.cc:107

Member Function Documentation

◆ addConstantCurves()

void minsky::PlotWidget::addConstantCurves ( )

add vector/tensor curves to plot

Definition at line 555 of file plotWidget.cc.

References minsky::cminsky(), minsky::defang(), minsky::anonymous_namespace{userFunction.cc}::isfinite(), minsky::maxNumTensorElementsToPlot, minsky::str(), minsky::valueId(), and minsky::yearToPTime().

556  {
557  std::vector<std::vector<std::pair<double,std::string>>> newXticks;
558 
559  // determine if any of the incoming vectors has a ptime-based xVector
560  xIsSecsSinceEpoch=false;
561  for (auto& yv: yvars)
562  for (auto& i: yv)
563  {
564  if (i && !i->hypercube().xvectors.empty())
565  {
566  const auto& xv=i->hypercube().xvectors[0];
567  if (xv.dimension.type==Dimension::time)
568  {
569  xIsSecsSinceEpoch=true;
570  break;
571  }
572  }
573  }
574 
575  formatter=defaultFormatter;
576 
577  if (plotType!=automatic)
578  Plot::plotType=plotType;
579 
580  size_t pen=0;
581  bool noLhsPens=true; // track whether any left had side ports are connected
582  clearPensOnLabelling=true; // arrange for penLabels to be cleared first time an entry is added
583  const OnStackExit setClearPensOnLabellingFalse([this]{clearPensOnLabelling=false;});
584 
585  for (size_t port=0; port<yvars.size(); ++port)
586  for (size_t i=0; i<yvars[port].size(); ++i)
587  if (yvars[port][i])
588  {
589  if (port<m_numLines) noLhsPens=false;
590  auto& yv=yvars[port][i];
591  if (yv->size()>0) (*yv)[0]; // ensure cachedTensors are up to date
592  auto d=yv->hypercube().dims();
593  if (d.empty())
594  {
595  if (Plot::plotType==Plot::bar)
596  addPlotPt(0);
597  pen++;
598  continue;
599  }
600  // work out a reference to the x data
601  vector<double> xdefault;
602  double* x;
603  if (port<xvars.size() && xvars[port])
604  {
605  if (xvars[port]->hypercube().xvectors[0].size()!=d[0])
606  throw error("x vector not same length as y vectors");
607  if (xvars[port]->index().empty())
608  x=xvars[port]->begin();
609  else
610  {
611  xdefault.reserve(d[0]);
612  for (size_t i=0; i<d[0]; ++i)
613  xdefault.push_back(xvars[port]->atHCIndex(i));
614  x=xdefault.data();
615  }
616  }
617  else
618  {
619  xdefault.reserve(d[0]);
620  newXticks.emplace_back();
621  if (yv->hypercube().rank()) // yv carries its own x-vector
622  {
623  const auto& xv=yv->hypercube().xvectors[0];
624  assert(xv.size()==d[0]);
625  switch (xv.dimension.type)
626  {
627  case Dimension::string:
628  for (size_t i=0; i<xv.size(); ++i)
629  {
630  newXticks.back().emplace_back(i, str(xv[i]));
631  xdefault.push_back(i);
632  }
633  if (plotType==automatic)
634  Plot::plotType=Plot::bar;
635  break;
636  case Dimension::value:
637  if (xIsSecsSinceEpoch && xv.dimension.units=="year")
638  // interpret "year" as Gregorian year date
639  for (const auto& i: xv)
640  {
641  xdefault.push_back(yearToPTime(i.value));
642  if (abs(i.value-int(i.value))<0.05) // only label years
643  newXticks.back().emplace_back(xdefault.back(), str(int(i.value)));
644  }
645  else
646  for (const auto& i: xv)
647  xdefault.push_back(i.value);
648  if (plotType==automatic)
649  Plot::plotType=Plot::line;
650  break;
651  case Dimension::time:
652  {
653  const string format=xv.timeFormat();
654  formatter=TimeFormatter(xv.dimension.units);
655  for (const auto& i: xv)
656  {
657  const double tv=(i.time-ptime(date(1970,Jan,1))).total_microseconds()*1E-6;
658  newXticks.back().emplace_back(tv,str(i,format));
659  xdefault.push_back(tv);
660  }
661  }
662  if (plotType==automatic)
663  Plot::plotType=Plot::line;
664  break;
665  }
666  }
667  else // by default, set x to 0..d[0]-1
668  for (size_t i=0; i<d[0]; ++i)
669  xdefault.push_back(i);
670  x=xdefault.data();
671  }
672 
673  const auto& idx=yv->index();
674  if (yv->rank()==1)
675  {
676  // 1D data's pen attributes should match the input port
677  if (idx.empty())
678  setPen(pen, x, yv->begin(), d[0]);
679  else
680  for (size_t j=0; j<idx.size(); ++j)
681  addPt(pen,x[idx[j]], (*yv)[j]);
682  pen++;
683  }
684  else
685  {
686  // higher rank y objects treated as multiple y vectors to plot
687  size_t startPen=pen;
688  if (idx.empty())
689  for (size_t j=0 /*d[0]*/; j<std::min(maxNumTensorElementsToPlot*d[0], yv->size()); j+=d[0])
690  {
691  setPen(pen++, x, yv->begin()+j, d[0]);
692  }
693  else // data is sparse
694  for (size_t j=0; j<idx.size(); ++j)
695  {
696  auto div=lldiv(idx[j], d[0]);
697  if (size_t(div.quot)<maxNumTensorElementsToPlot)
698  {
699  addPt(startPen+div.quot, x[div.rem], (*yv)[j]);
700  if (pen<=startPen+div.quot) pen=startPen+div.quot+1; // track highest pen used
701  }
702  }
703  // compute the pen labels
704  for (int j=0; startPen<pen; ++startPen, ++j)
705  {
706  string label;
707  size_t stride=1;
708  for (size_t i=1; i<yv->hypercube().rank(); ++i)
709  {
710  label+=str(yv->hypercube().xvectors[i][(j/stride)%d[i]])+" ";
711  stride*=d[i];
712  }
713  assignSide(startPen,port<m_numLines? Side::left: Side::right);
714  labelPen(startPen,defang(label));
715  }
716  }
717  }
718  justDataChanged=true;
719  scalePlot();
720 
721  if (noLhsPens)
722  {
723  // set scale to RHS
724  miny=miny1;
725  maxy=maxy1;
726  }
727 
728  // add markers
729  if (isfinite(miny) && isfinite(maxy))
730  {
731  for (auto& m: horizontalMarkers)
732  if (auto v=cminsky().variableValues[valueId(group.lock(), ':'+m)])
733  {
734  auto eps=1e-4*(maxx-minx);
735  addPt(pen,minx+eps,v->value());
736  addPt(pen,maxx-eps,v->value());
737  assignSide(pen,marker);
738  if (!v->tooltip.empty())
739  labelPen(pen++,v->tooltip);
740  else
741  labelPen(pen++,m);
742  }
743  for (auto& m: verticalMarkers)
744  if (auto v=cminsky().variableValues[valueId(group.lock(), ':'+m)])
745  {
746  auto eps=1e-4*(maxy-miny);
747  auto value=v->value();
748  if (xIsSecsSinceEpoch && (v->units.empty() || v->units==Units("year")))
750  addPt(pen,value,miny+eps);
751  addPt(pen,value,maxy-eps);
752  assignSide(pen,marker);
753  if (!v->tooltip.empty())
754  labelPen(pen++,v->tooltip);
755  else
756  labelPen(pen++,m);
757  }
758  }
759 
760  if (newXticks.size()==1) // nothing to disambiguate
761  xticks=std::move(newXticks.front());
762  else
763  {
764  xticks.clear();
765  // now work out which xticks we'll use See Ravel #173
766  for (auto& i: newXticks)
767  if (i.empty())
768  {
769  xticks.clear();
770  break; // value axes trump all
771  }
772  else if (xticks.empty())
773  xticks=std::move(i);
774  else
775  {// expand range of tick labels by each pen's tick labels in turn
776  auto j=i.begin();
777  for (; j!=i.end(); ++j)
778  if (j->first>=xticks.front().first)
779  break;
780  xticks.insert(xticks.begin(), i.begin(), j);
781  j=i.end();
782  for (; j!=i.begin(); --j)
783  if ((j-1)->first<=xticks.back().first)
784  break;
785  xticks.insert(xticks.end(), j, i.end());
786  }
787  }
788 
789  }
string defang(char c)
Definition: latexMarkup.cc:842
PlotType plotType
automatic means choose line or bar depending on the x-vector type.
Definition: plotWidget.h:117
virtual double value() const
current value of output port
Definition: item.h:209
Formatter formatter
Definition: plotWidget.h:73
virtual float x() const
Definition: item.cc:107
static const size_t maxNumTensorElementsToPlot
Definition: plotWidget.cc:433
void addPlotPt(double t)
add another plot point
Definition: plotWidget.cc:458
size_t startPen(size_t port) const
returns to starting pen number for input port
Definition: plotWidget.cc:792
string valueId(const string &name)
construct a valueId from fully qualified name @ name should not be canonicalised
Definition: valueId.cc:75
std::vector< std::vector< std::shared_ptr< VariableValue > > > yvars
variable port attached to (if any)
Definition: plotWidget.h:90
std::vector< std::string > verticalMarkers
Definition: plotWidget.h:112
void labelPen(size_t pen, const std::string &label)
Definition: plotWidget.h:95
double yearToPTime(double x)
Definition: plotWidget.h:38
std::string str(T x)
utility function to create a string representation of a numeric type
Definition: str.h:33
const Minsky & cminsky()
const version to help in const correctness
Definition: minsky.h:549
std::vector< std::string > horizontalMarkers
markers at a position given by a named variable/parameter
Definition: plotWidget.h:111
void scalePlot()
sets the plot scale and pen labels
Definition: plotWidget.cc:218
bool clearPensOnLabelling
Definition: plotWidget.h:79
std::vector< std::shared_ptr< VariableValue > > xvars
Definition: plotWidget.h:91
Definition: group.tcl:84
size_t m_numLines
number of ports on the side
Definition: plotWidget.h:74
Here is the call graph for this function:

◆ addPlotPt()

void minsky::PlotWidget::addPlotPt ( double  t)

add another plot point

Definition at line 458 of file plotWidget.cc.

References minsky::accumulatedBlitTime, minsky::cminsky(), minsky::anonymous_namespace{userFunction.cc}::isfinite(), minsky::maxNumTensorElementsToPlot, minsky::Simulation::timeUnit, minsky::timeUnit, minsky::to_string(), minsky::valueId(), and minsky::yearToPTime().

459  {
460  size_t pen=0;
461  for (size_t port=0; port<yvars.size(); ++port)
462  for (auto& yvar: yvars[port])
463  for (size_t i=0; i<min(maxNumTensorElementsToPlot,yvar->size()); ++i)
464  {
465  double x,y;
466  switch (xvars.size())
467  {
468  case 0: // use t, when x variable not attached
469  if (xIsSecsSinceEpoch && (cminsky().timeUnit.empty()||cminsky().timeUnit=="year"))
470  x=yearToPTime(t);
471  else
472  x=t;
473  y=(*yvar)[i];
474  break;
475  case 1: // use the value of attached variable
476  assert(xvars[0] && xvars[0]->idx()>=0); // xvars also vector of shared pointers and null derefencing error can likewise cause crash. for ticket 1248
477  if (xvars[0]->size()>1)
478  throw_error("Tensor valued x inputs not supported");
479  x=(*xvars[0])[0];
480  y=(*yvar)[i];
481  break;
482  default:
483  if (port < xvars.size() && xvars[port] && xvars[port]->idx()>=0) // xvars also vector of shared pointers and null derefencing error can likewise cause crash. for ticket 1248
484  {
485  if (xvars[port]->size()>1)
486  throw_error("Tensor valued x inputs not supported");
487  x=(*xvars[port])[0];
488  y=(*yvar)[i];
489  }
490  else
491  throw_error("x input not wired for port "+to_string(port+1));
492  break;
493  }
494  addPt(pen++, x, y);
495  }
496 
497  // add markers
498  if (isfinite(miny) && isfinite(maxy))
499  {
500  for (auto& m: horizontalMarkers)
501  if (auto v=cminsky().variableValues[valueId(group.lock(), ':'+m)])
502  {
503  auto eps=1e-4*(maxx-minx);
504  double x[]{minx+eps,miny-eps};
505  double y[]{v->value(),v->value()};
506  setPen(pen,x,y,2);
507  assignSide(pen,marker);
508  labelPen(pen++,v->tooltip);
509  }
510  for (auto& m: verticalMarkers)
511  if (auto v=cminsky().variableValues[valueId(group.lock(), ':'+m)])
512  {
513  auto eps=1e-4*(maxy-miny);
514  auto value=v->value();
515  if (xIsSecsSinceEpoch && (v->units.empty() || v->units==Units("year")))
517  double x[]{value,value};
518  double y[]{miny+eps,maxy-eps};
519  setPen(pen,x,y,2);
520  assignSide(pen,marker);
521  labelPen(pen++,v->tooltip);
522  }
523  }
524 
525 
526  // throttle plot redraws
527  static const time_duration maxWait=milliseconds(1000);
528  if ((microsec_clock::local_time()-(ptime&)lastAdd) >
529  min((accumulatedBlitTime-(ptime&)lastAccumulatedBlitTime) * 2, maxWait))
530  {
531  const ptime timerStart=microsec_clock::local_time();
532  requestRedraw();
534  lastAdd=microsec_clock::local_time();
535  accumulatedBlitTime += lastAdd - timerStart;
536  }
537  }
virtual double value() const
current value of output port
Definition: item.h:209
classdesc::Exclude< boost::posix_time::ptime > lastAdd
Definition: plotWidget.h:56
virtual float x() const
Definition: item.cc:107
static const size_t maxNumTensorElementsToPlot
Definition: plotWidget.cc:433
virtual float y() const
Definition: item.cc:114
string valueId(const string &name)
construct a valueId from fully qualified name @ name should not be canonicalised
Definition: valueId.cc:75
std::vector< std::vector< std::shared_ptr< VariableValue > > > yvars
variable port attached to (if any)
Definition: plotWidget.h:90
std::vector< std::string > verticalMarkers
Definition: plotWidget.h:112
void labelPen(size_t pen, const std::string &label)
Definition: plotWidget.h:95
std::string timeUnit
Definition: simulation.h:35
double yearToPTime(double x)
Definition: plotWidget.h:38
const Minsky & cminsky()
const version to help in const correctness
Definition: minsky.h:549
std::vector< std::string > horizontalMarkers
markers at a position given by a named variable/parameter
Definition: plotWidget.h:111
void throw_error(const std::string &) const
mark item on canvas, then throw
Definition: item.cc:86
static ptime accumulatedBlitTime
Definition: plotWidget.cc:431
void requestRedraw()
redraw plot using current data to all open windows
Definition: plotWidget.cc:363
std::vector< std::shared_ptr< VariableValue > > xvars
Definition: plotWidget.h:91
UnitsExpressionWalker timeUnit
Definition: group.tcl:84
string to_string(CONST84 char *x)
Definition: minskyTCLObj.h:33
classdesc::Exclude< boost::posix_time::ptime > lastAccumulatedBlitTime
Definition: plotWidget.h:57
Here is the call graph for this function:

◆ addPorts()

void minsky::PlotWidget::addPorts ( )

Definition at line 81 of file plotWidget.cc.

82  {
83  m_ports.clear();
84  unsigned i=0;
85  for (; i<nBoundsPorts; ++i) // bounds ports
86  m_ports.emplace_back(make_shared<InputPort>(*this));
87  for (; i<2*m_numLines+nBoundsPorts; ++i) // y data input ports
88  m_ports.emplace_back(make_shared<MultiWireInputPort>(*this));
89  for (; i<4*m_numLines+nBoundsPorts; ++i) // x data input ports
90  m_ports.emplace_back(make_shared<InputPort>(*this));
91  }
static constexpr unsigned nBoundsPorts
number of bounds (xmin/xmax, ymin/ymax, y1min/y1max) ports
Definition: plotWidget.h:87
ItemPortVector m_ports
Definition: item.h:156
size_t m_numLines
number of ports on the side
Definition: plotWidget.h:74

◆ autoScale()

void minsky::PlotWidget::autoScale ( )
inline

set autoscaling

Definition at line 180 of file plotWidget.h.

std::shared_ptr< VariableValue > xminVar
variable ports specifying plot size
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > xmaxVar
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > y1maxVar
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > ymaxVar
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > yminVar
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > y1minVar
Definition: plotWidget.h:103

◆ availableMarkers()

set< string > minsky::PlotWidget::availableMarkers ( ) const

return list of variables that could be attached to markers

Definition at line 837 of file plotWidget.cc.

References minsky::uqName().

838  {
839  // search upwards through group heirarchy, looking for variable to add
840  set<string> r;
841  for (auto g=group.lock(); g; g=g->group.lock())
842  for (auto& i: g->items)
843  if (auto v=i->variableCast())
844  r.insert(uqName(v->rawName()));
845  return r;
846  }
std::string uqName(const std::string &name)
extract unqualified portion of name
Definition: valueId.cc:135
Definition: group.tcl:84
Here is the call graph for this function:

◆ barWidth() [1/2]

double minsky::PlotWidget::barWidth ( ) const

bar chart bar width control

Definition at line 446 of file plotWidget.cc.

447  {
448  return accumulate(palette.begin(), palette.end(), 1.0,
449  [](double acc, const LineStyle& ls) {return std::min(acc, ls.barWidth);});
450  }

◆ barWidth() [2/2]

double minsky::PlotWidget::barWidth ( double  w)

bar chart bar width control

Definition at line 452 of file plotWidget.cc.

453  {
454  for (auto& ls: palette) ls.barWidth=w;
455  return w;
456  }

◆ CLASSDESC_ACCESS()

minsky::PlotWidget::CLASSDESC_ACCESS ( PlotWidget  )
private

◆ clickType()

ClickType::Type minsky::PlotWidget::clickType ( float  x,
float  y 
) const
overridevirtual

returns the clicktype given a mouse click at x, y.

Reimplemented from minsky::Item.

Definition at line 399 of file plotWidget.cc.

References minsky::portRadius.

400  {
401  // firstly, check whether a port has been selected
402  const double z=Item::zoomFactor();
403  for (auto& p: m_ports)
404  {
405  if (hypot(x-p->x(), y-p->y()) < portRadius*z)
406  return ClickType::onPort;
407  }
408 
409  double legendWidth, legendHeight, lx, ly;
410  legendSize(lx, ly, legendWidth, legendHeight, z*(iWidth()-2*portSpace), z*(iHeight()-portSpace-yoffs));
411  // xx & yy are in plot user coordinates
412  const double xx= x-this->x() + z*(0.5*iWidth()-portSpace) - lx;
413  const double yy= z*(0.5*iHeight()-portSpace)-y+this->y() - ly+legendHeight;
414 
415  if (legend && xx>0 && xx<legendWidth)
416  {
417  if (yy>0.2*legendHeight && yy<legendHeight)
418  return ClickType::legendMove;
419  if (yy>-6*z && yy<=0.2*legendHeight) // allow a bit of extra height for resize arrow
421  }
422 
423  if (onResizeHandle(x,y)) return ClickType::onResize;
424 
425  const double dx=x-this->x(), dy=y-this->y();
426  const double w=0.5*iWidth()*z, h=0.5*iHeight()*z;
427  return (abs(dx)<w && abs(dy)<h)?
429  }
double yoffs
extra offset for titles
Definition: plotWidget.h:71
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
float iHeight() const
Definition: item.h:224
virtual float zoomFactor() const
Definition: item.cc:121
virtual bool onResizeHandle(float x, float y) const
Definition: item.cc:228
float iWidth() const
Definition: item.h:217
ItemPortVector m_ports
Definition: item.h:156
static constexpr double portSpace
Definition: plotWidget.h:48
constexpr float portRadius
radius of circle marking ports at zoom=1
Definition: item.h:69

◆ connectVar()

void minsky::PlotWidget::connectVar ( const std::shared_ptr< VariableValue > &  var,
unsigned  port 
)

connect variable var to port port.

Definition at line 801 of file plotWidget.cc.

References minsky::var.

802  {
803  assert(var);
804  if (port<nBoundsPorts)
805  switch (port)
806  {
807  case 0: xminVar=var; return;
808  case 1: xmaxVar=var; return;
809  case 2: yminVar=var; return;
810  case 3: ymaxVar=var; return;
811  case 4: y1minVar=var; return;
812  case 5: y1maxVar=var; return;
813  }
814  if (port-nBoundsPorts<2*m_numLines)
815  {
816  yvars.resize(port-nBoundsPorts+1);
817  yvars[port-nBoundsPorts].push_back(var);
818  // assign Side::right to pens belonging to the RHS
819  assignSide(startPen(port-nBoundsPorts+1)-1,port-nBoundsPorts<m_numLines? Side::left: Side::right);
820  }
821  else if (port-nBoundsPorts<4*m_numLines)
822  {
823  xvars.resize(port-nBoundsPorts-2*m_numLines+1);
825  }
826  justDataChanged=false;
827  scalePlot();
828  }
size_t startPen(size_t port) const
returns to starting pen number for input port
Definition: plotWidget.cc:792
std::shared_ptr< VariableValue > xminVar
variable ports specifying plot size
Definition: plotWidget.h:103
static constexpr unsigned nBoundsPorts
number of bounds (xmin/xmax, ymin/ymax, y1min/y1max) ports
Definition: plotWidget.h:87
std::vector< std::vector< std::shared_ptr< VariableValue > > > yvars
variable port attached to (if any)
Definition: plotWidget.h:90
std::shared_ptr< VariableValue > xmaxVar
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > y1maxVar
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > ymaxVar
Definition: plotWidget.h:103
void scalePlot()
sets the plot scale and pen labels
Definition: plotWidget.cc:218
std::shared_ptr< VariableValue > yminVar
Definition: plotWidget.h:103
std::vector< std::shared_ptr< VariableValue > > xvars
Definition: plotWidget.h:91
size_t m_numLines
number of ports on the side
Definition: plotWidget.h:74
std::shared_ptr< VariableValue > y1minVar
Definition: plotWidget.h:103

◆ contains()

bool minsky::PlotWidget::contains ( float  x,
float  y 
) const
inlineoverridevirtual

Reimplemented from minsky::Item.

Definition at line 176 of file plotWidget.h.

References minsky::ClickType::outside.

177  {return clickType(x,y)!=ClickType::outside;}
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
ClickType::Type clickType(float x, float y) const override
returns the clicktype given a mouse click at x, y.
Definition: plotWidget.cc:399

◆ destroyFrame()

void minsky::PlotWidget::destroyFrame ( )
inlineoverridevirtual

destroy any popup windows associated with this

Reimplemented from minsky::Item.

Definition at line 199 of file plotWidget.h.

References minsky::RenderNativeWindow::destroyFrame().

Here is the call graph for this function:

◆ disconnectAllVars()

void minsky::PlotWidget::disconnectAllVars ( )

Definition at line 830 of file plotWidget.cc.

831  {
832  xvars.clear();
833  yvars.clear();
835  }
std::shared_ptr< VariableValue > xminVar
variable ports specifying plot size
Definition: plotWidget.h:103
std::vector< std::vector< std::shared_ptr< VariableValue > > > yvars
variable port attached to (if any)
Definition: plotWidget.h:90
std::shared_ptr< VariableValue > xmaxVar
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > y1maxVar
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > ymaxVar
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > yminVar
Definition: plotWidget.h:103
std::vector< std::shared_ptr< VariableValue > > xvars
Definition: plotWidget.h:91
std::shared_ptr< VariableValue > y1minVar
Definition: plotWidget.h:103

◆ draw()

void minsky::PlotWidget::draw ( cairo_t *  cairo) const
overridevirtual

draw this item into a cairo context

Reimplemented from minsky::Item.

Definition at line 93 of file plotWidget.cc.

References minsky::anonymous_namespace{plotWidget.cc}::boundX, minsky::anonymous_namespace{plotWidget.cc}::boundY, minsky::drawTriangle(), minsky::latexToPango(), M_PI, minsky::anonymous_namespace{plotWidget.cc}::orient, and minsky::anonymous_namespace{plotWidget.cc}::titleHeight.

94  {
95  CairoSave cs(cairo);
96  const double z=Item::zoomFactor();
97  cairo_scale(cairo,z,z);
98  const double w=iWidth();
99  double h=iHeight();
100 
101  // if any titling, draw an extra bounding box (ticket #285)
102  if (!title.empty()||!xlabel().empty()||!ylabel().empty()||!y1label().empty())
103  {
104  cairo_rectangle(cairo,-0.5*w+10,-0.5*h,w-20,h-10);
105  cairo_set_line_width(cairo,1);
106  cairo_stroke(cairo);
107  }
108 
109  cairo_translate(cairo,-0.5*w,-0.5*h);
110 
111  yoffs=0;
112  if (!title.empty())
113  {
114  const CairoSave cs(cairo);
115  const double fy=titleHeight*iHeight();
116 
117  Pango pango(cairo);
118  pango.setFontSize(fabs(fy));
119  pango.setMarkup(latexToPango(title));
120  cairo_set_source_rgb(cairo,0,0,0);
121  cairo_move_to(cairo,0.5*(w-pango.width()), 0);
122  pango.show();
123 
124  // allow some room for the title
125  yoffs=pango.height();
126  h-=yoffs;
127  }
128 
129  // draw bounding box ports
130 
131  size_t i=0;
132  // draw bounds input ports
133  for (; i<nBoundsPorts; ++i)
134  {
135  const float x=boundX[i]*w, y=boundY[i]*h;
136  if (!justDataChanged)
137  m_ports[i]->moveTo(x*z + this->x(), y*z + this->y()+0.5*yoffs);
138  drawTriangle(cairo, x+0.5*w, y+0.5*h+yoffs, palette[(i/2)%palette.size()].colour, orient[i]);
139 
140  }
141 
142  const float xLeft = -0.5*w, dx=w/(2*m_numLines+1); // x location of ports
143  const float dy = h/m_numLines;
144  // draw y data ports
145  for (; i<m_numLines+nBoundsPorts; ++i)
146  {
147  const float y=0.5*(dy-h) + (i-nBoundsPorts)*dy;
148  if (!justDataChanged)
149  m_ports[i]->moveTo(xLeft*z + this->x(), y*z + this->y()+0.5*yoffs);
150  drawTriangle(cairo, xLeft+0.5*w, y+0.5*h+yoffs, palette[(i-nBoundsPorts)%palette.size()].colour, 0);
151  }
152 
153  // draw RHS y data ports
154  for (; i<2*m_numLines+nBoundsPorts; ++i)
155  {
156  const float y=0.5*(dy-h) + (i-m_numLines-nBoundsPorts)*dy, x=0.5*w;
157  if (!justDataChanged)
158  m_ports[i]->moveTo(x*z + this->x(), y*z + this->y()+0.5*yoffs);
159  drawTriangle(cairo, x+0.5*w, y+0.5*h+yoffs, palette[(i-nBoundsPorts)%palette.size()].colour, M_PI);
160  }
161 
162  // draw x data ports
163  const float yBottom=0.5*h;
164  for (; i<4*m_numLines+nBoundsPorts; ++i)
165  {
166  const float x=dx-0.5*w + (i-2*m_numLines-nBoundsPorts)*dx;
167  if (!justDataChanged)
168  m_ports[i]->moveTo(x*z + this->x(), yBottom*z + this->y()+0.5*yoffs);
169  drawTriangle(cairo, x+0.5*w, yBottom+0.5*h+yoffs, palette[(i-2*m_numLines-nBoundsPorts)%palette.size()].colour, -0.5*M_PI);
170  }
171 
172  cairo_translate(cairo, portSpace, yoffs);
173  cairo_set_line_width(cairo,1);
174  const double gw=w-2*portSpace, gh=h-portSpace;
175 
176  Plot::draw(cairo,gw,gh);
177  if (mouseFocus && legend)
178  {
179  double width,height,x,y;
180  legendSize(x,y,width,height,gw,gh);
181  // following code puts x,y at centre point of legend
182  x+=0.5*width;
183  const double arrowLength=6;
184  y=(h-portSpace)-y+0.5*height;
185  cairo_move_to(cairo,x-arrowLength,y);
186  cairo_rel_line_to(cairo,2*arrowLength,0);
187  cairo_move_to(cairo,x,y-arrowLength);
188  cairo_rel_line_to(cairo,0,2*arrowLength);
189 
190  cairo_move_to(cairo,x,y+0.5*height);
191  cairo_rel_line_to(cairo,0,arrowLength);
192  cairo_stroke(cairo);
193  drawTriangle(cairo,x-arrowLength,y,{0,0,0,1},M_PI);
194  drawTriangle(cairo,x+arrowLength,y,{0,0,0,1},0);
195  drawTriangle(cairo,x,y-arrowLength,{0,0,0,1},3*M_PI/2);
196  drawTriangle(cairo,x,y+arrowLength,{0,0,0,1},M_PI/2);
197  drawTriangle(cairo,x,y+0.5*height+arrowLength,{0,0,0,1},M_PI/2);
198 
199  cairo_rectangle(cairo,x-0.5*width,y-0.5*height,width,height);
200  }
201  cs.restore();
202  if (mouseFocus)
203  {
204  drawPorts(cairo);
205  displayTooltip(cairo,tooltip());
206  // Resize handles always visible on mousefocus. For ticket 92.
207  drawResizeHandles(cairo);
208  }
209  justDataChanged=false;
210 
211  cairo_new_path(cairo);
212  cairo_rectangle(cairo,-0.5*w,-0.5*h,w,h);
213  cairo_clip(cairo);
214  if (selected) drawSelected(cairo);
215 
216  }
#define M_PI
some useful geometry types, defined from boost::geometry
Definition: geometry.h:29
std::string latexToPango(const char *s)
Definition: latexMarkup.h:30
void drawPorts(cairo_t *cairo) const
Definition: item.cc:294
virtual void displayTooltip(cairo_t *, const std::string &) const
display tooltip text, eg on mouseover
Definition: item.cc:398
const float boundX[PlotWidget::nBoundsPorts]
Definition: plotWidget.cc:57
double yoffs
extra offset for titles
Definition: plotWidget.h:71
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
float height() const
Definition: plotWidget.h:131
float iHeight() const
Definition: item.h:224
const float boundY[PlotWidget::nBoundsPorts]
Definition: plotWidget.cc:59
std::string const & ylabel() const
Definition: plotWidget.h:142
static constexpr unsigned nBoundsPorts
number of bounds (xmin/xmax, ymin/ymax, y1min/y1max) ports
Definition: plotWidget.h:87
std::string title
Definition: plotWidget.h:114
virtual std::string const & tooltip() const
Definition: noteBase.h:36
virtual float zoomFactor() const
Definition: item.cc:121
const double orient[PlotWidget::nBoundsPorts]
Definition: plotWidget.cc:55
static void drawSelected(cairo_t *cairo)
Definition: item.cc:308
float iWidth() const
Definition: item.h:217
virtual void drawResizeHandles(cairo_t *cairo) const
Definition: item.cc:343
ItemPortVector m_ports
Definition: item.h:156
bool selected
true if selected for cut, copy or group operation
Definition: noteBase.h:32
std::string const & y1label() const
Definition: plotWidget.h:147
bool mouseFocus
true if target of a mouseover
Definition: noteBase.h:31
float width() const
Definition: plotWidget.h:130
void drawTriangle(cairo_t *cairo, double x, double y, const ecolab::cairo::Colour &col, double angle=0)
static constexpr double portSpace
Definition: plotWidget.h:48
std::string const & xlabel() const
shadowed label commands to allow latex intepretation
Definition: plotWidget.h:137
size_t m_numLines
number of ports on the side
Definition: plotWidget.h:74
Here is the call graph for this function:

◆ exportAsCSV()

void minsky::PlotWidget::exportAsCSV ( const string &  filename)
inline

export the plotted data as a CSV file

Definition at line 197 of file plotWidget.h.

197 {ecolab::Plot::exportAsCSV(filename);}

◆ height()

float minsky::PlotWidget::height ( ) const
inline

Definition at line 131 of file plotWidget.h.

References minsky::Item::height().

131 {return Item::height();}
float height() const
Definition: item.h:243
Here is the call graph for this function:

◆ labelPen()

void minsky::PlotWidget::labelPen ( size_t  pen,
const std::string &  label 
)
inline

Definition at line 95 of file plotWidget.h.

References minsky::latexToPango().

95  {
97  if (penLabels.size()<=pen+1) penLabels.resize(pen+1);
98  penLabels[pen]=label;
99  Plot::labelPen(pen,latexToPango(label));
100  }
std::string latexToPango(const char *s)
Definition: latexMarkup.h:30
bool clearPensOnLabelling
Definition: plotWidget.h:79
std::vector< std::string > penLabels
Definition: plotWidget.h:94
Here is the call graph for this function:

◆ makeDisplayPlot()

void minsky::PlotWidget::makeDisplayPlot ( )

add this as a display plot to its group

Definition at line 384 of file plotWidget.cc.

384  {
385  if (auto g=group.lock())
386  g->displayPlot=dynamic_pointer_cast<PlotWidget>(g->findItem(*this));
387  }
Definition: group.tcl:84

◆ mouseDown()

void minsky::PlotWidget::mouseDown ( float  x,
float  y 
)
overridevirtual

handle mouse events

Reimplemented from minsky::EventInterface.

Definition at line 305 of file plotWidget.cc.

References minsky::anonymous_namespace{plotWidget.cc}::titleHeight.

306  {
307  clickX=x;
308  clickY=y;
309  ct=clickType(x,y);
310  const double z=Item::zoomFactor();
311  const double gw=iWidth()*z-2*portSpace;
312  double gh=iHeight()*z-portSpace;
313  if (!title.empty()) gh=iHeight()*z-portSpace-titleHeight;
314  oldLegendLeft=legendLeft*gw+portSpace;
315  oldLegendTop=legendTop*gh;
316  oldLegendFontSz=legendFontSz;
317  }
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
float iHeight() const
Definition: item.h:224
ClickType::Type clickType(float x, float y) const override
returns the clicktype given a mouse click at x, y.
Definition: plotWidget.cc:399
std::string title
Definition: plotWidget.h:114
virtual float zoomFactor() const
Definition: item.cc:121
float iWidth() const
Definition: item.h:217
double oldLegendLeft
Definition: plotWidget.h:49
ClickType::Type ct
Definition: plotWidget.h:50
static constexpr double portSpace
Definition: plotWidget.h:48
double oldLegendFontSz
Definition: plotWidget.h:49

◆ mouseMove()

void minsky::PlotWidget::mouseMove ( float  x,
float  y 
)
overridevirtual

handle mouse events

Reimplemented from minsky::EventInterface.

Definition at line 319 of file plotWidget.cc.

References f, and minsky::anonymous_namespace{plotWidget.cc}::titleHeight.

320  {
321  const double z=Item::zoomFactor();
322  //const double w=0.5*iWidth()*z, h=0.5*iHeight()*z;
323  //const double dx=x-this->x(), dy=y-this->y();
324  const double gw=iWidth()*z-2*portSpace;
325  double gh=iHeight()*z-portSpace;
326  if (!title.empty()) gh=iHeight()*z-portSpace-titleHeight;
327  const double yoffs=this->y()-(legendTop-0.5)*iHeight()*z;
328  switch (ct)
329  {
331  legendLeft = (oldLegendLeft + x - clickX-portSpace)/gw;
332  legendTop = (oldLegendTop + clickY - y)/gh;
333  if (!title.empty()) legendTop = (oldLegendTop + clickY - y + titleHeight)/gh;
334  break;
336  legendFontSz = oldLegendFontSz * (y-yoffs)/(clickY-yoffs);
337  if (!title.empty()) legendFontSz = oldLegendFontSz * (y-yoffs+titleHeight)/(clickY-yoffs);
338  break;
339  default:
340  {
341  auto& f=frameArgs();
342  if (Plot::mouseMove((x-f.offsetLeft)/f.childWidth, (f.childHeight-f.offsetTop-y)/f.childHeight,
343  10.0/std::max(f.childWidth,f.childHeight)))
344  requestRedraw();
345  }
346  break;
347  }
348  }
function f
Definition: canvas.m:1
double yoffs
extra offset for titles
Definition: plotWidget.h:71
const RenderFrameArgs & frameArgs() const
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
float iHeight() const
Definition: item.h:224
std::string title
Definition: plotWidget.h:114
virtual float zoomFactor() const
Definition: item.cc:121
float iWidth() const
Definition: item.h:217
double oldLegendLeft
Definition: plotWidget.h:49
ClickType::Type ct
Definition: plotWidget.h:50
void requestRedraw()
redraw plot using current data to all open windows
Definition: plotWidget.cc:363
static constexpr double portSpace
Definition: plotWidget.h:48
double oldLegendFontSz
Definition: plotWidget.h:49

◆ mouseUp()

void minsky::PlotWidget::mouseUp ( float  x,
float  y 
)
inlineoverridevirtual

handle mouse events

Reimplemented from minsky::EventInterface.

Definition at line 187 of file plotWidget.h.

References minsky::ClickType::outside.

187  {
188  mouseMove(x,y);
190  }
void mouseMove(float, float) override
handle mouse events
Definition: plotWidget.cc:319
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
ClickType::Type ct
Definition: plotWidget.h:50

◆ numLines() [1/2]

size_t minsky::PlotWidget::numLines ( ) const
inline

number of input ports along a side

Definition at line 125 of file plotWidget.h.

125 {return m_numLines;}
size_t m_numLines
number of ports on the side
Definition: plotWidget.h:74

◆ numLines() [2/2]

size_t minsky::PlotWidget::numLines ( size_t  n)

number of input ports along a side

Definition at line 435 of file plotWidget.cc.

436  {
437  if (m_numLines!=n)
438  {
439  m_numLines=n;
440  addPorts();
441  }
442  return n;
443  }
size_t m_numLines
number of ports on the side
Definition: plotWidget.h:74

◆ onMouseLeave()

void minsky::PlotWidget::onMouseLeave ( )
inlineoverridevirtual

handle mouse events

Reimplemented from minsky::Item.

Definition at line 192 of file plotWidget.h.

192 {valueString="";}

◆ onMouseOver()

bool minsky::PlotWidget::onMouseOver ( float  x,
float  y 
)
overridevirtual

handle mouse events

Reimplemented from minsky::Item.

Definition at line 350 of file plotWidget.cc.

351  {
352  const double z=Item::zoomFactor();
353  // coordinate system runs from bottom left to top right. Vertical coordinates must be flipped
354  const double dx=x-this->x()+0.5*iWidth()*z-portSpace;
355  const double dy=this->y()-y+0.5*iHeight()*z-portSpace;
356  const double gw=iWidth()*z-2*portSpace;
357  const double gh=iHeight()*z-portSpace-yoffs*z;
358  const double loffx=lh(gw,gh)*!Plot::ylabel.empty(), loffy=lh(gw,gh)*!Plot::xlabel.empty();
359  return Plot::mouseMove((dx-loffx)/gw, (dy-loffy)/gh, 10.0/std::max(gw,gh),formatter);
360  }
double yoffs
extra offset for titles
Definition: plotWidget.h:71
Formatter formatter
Definition: plotWidget.h:73
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
float iHeight() const
Definition: item.h:224
virtual float zoomFactor() const
Definition: item.cc:121
float iWidth() const
Definition: item.h:217
static constexpr double portSpace
Definition: plotWidget.h:48

◆ plotWidgetCast() [1/2]

const PlotWidget* minsky::PlotWidget::plotWidgetCast ( ) const
inlineoverride

Definition at line 133 of file plotWidget.h.

133 {return this;}

◆ plotWidgetCast() [2/2]

PlotWidget* minsky::PlotWidget::plotWidgetCast ( )
inlineoverride

Definition at line 134 of file plotWidget.h.

134 {return this;}

◆ redraw()

bool minsky::PlotWidget::redraw ( int  x0,
int  y0,
int  width,
int  height 
)
overrideprivate

Definition at line 372 of file plotWidget.cc.

373  {
374  if (surface.get())
375  {
377  Plot::draw(surface->cairo(),width/sf,height/sf);
378  surface->blit();
379  }
380  return surface.get();
381  }
float height() const
Definition: plotWidget.h:131
float width() const
Definition: plotWidget.h:130

◆ redrawWithBounds()

void minsky::PlotWidget::redrawWithBounds ( )
inlineoverride

Definition at line 169 of file plotWidget.h.

169 {redraw(0,0,500,500);}
bool redraw(int, int, int, int) override
Definition: plotWidget.cc:372

◆ requestRedraw()

void minsky::PlotWidget::requestRedraw ( )

redraw plot using current data to all open windows

Definition at line 363 of file plotWidget.cc.

364  {
365  justDataChanged=true; // assume plot same size, don't do unnecessary stuff
366  // store previous min/max values to determine if plot scale changes
367  scalePlot();
368  if (surface.get())
369  surface->requestRedraw();
370  }
void scalePlot()
sets the plot scale and pen labels
Definition: plotWidget.cc:218

◆ resize()

void minsky::PlotWidget::resize ( const LassoBox b)
overridevirtual

resize this item on the canvas

Reimplemented from minsky::Item.

Definition at line 389 of file plotWidget.cc.

References minsky::LassoBox::x0, minsky::LassoBox::x1, minsky::LassoBox::y0, and minsky::LassoBox::y1.

390  {
391  const float invZ=1/Item::zoomFactor();
392  iWidth(abs(x.x1-x.x0)*invZ);
393  iHeight(abs(x.y1-x.y0)*invZ);
394  Item::moveTo(0.5*(x.x0+x.x1), 0.5*(x.y0+x.y1));
395  bb.update(*this);
396  }
virtual float x() const
Definition: item.cc:107
float iHeight() const
Definition: item.h:224
BoundingBox bb
canvas bounding box.
Definition: item.h:195
virtual float zoomFactor() const
Definition: item.cc:121
void update(const Item &x)
Definition: item.cc:46
float iWidth() const
Definition: item.h:217
void moveTo(float x, float y)
Definition: item.cc:256

◆ scalePlot()

void minsky::PlotWidget::scalePlot ( )

sets the plot scale and pen labels

Definition at line 218 of file plotWidget.cc.

References minsky::cminsky(), minsky::anonymous_namespace{userFunction.cc}::isfinite(), minsky::Simulation::timeUnit, minsky::timeUnit, minsky::uqName(), minsky::wire, and minsky::yearToPTime().

219  {
220  // set any scale overrides
221  setMinMax();
222  if (xminVar && xminVar->idx()>-1)
223  {
224  if (xIsSecsSinceEpoch && (xminVar->units.empty() || xminVar->units==Units("year")))
225  minx=yearToPTime(xminVar->value());
226  else
227  minx=xminVar->value();
228  }
229  else if (isfinite(xmin))
230  {
231  if (xIsSecsSinceEpoch && (cminsky().timeUnit.empty() || cminsky().timeUnit=="year"))
232  minx=yearToPTime(xmin);
233  else
234  minx=xmin;
235  }
236 
237  if (xmaxVar && xmaxVar->idx()>-1)
238  {
239  if (xIsSecsSinceEpoch && (xmaxVar->units.empty() || xmaxVar->units==Units("year")))
240  maxx=yearToPTime(xmaxVar->value());
241  else
242  maxx=xmaxVar->value();
243  }
244  else if (isfinite(xmax))
245  {
246  if (xIsSecsSinceEpoch && (cminsky().timeUnit.empty() || cminsky().timeUnit=="year"))
247  maxx=yearToPTime(xmax);
248  else
249  maxx=xmax;
250  }
251 
252  if (yminVar && yminVar->idx()>-1)
253  miny=yminVar->value();
254  else if (isfinite(ymin))
255  miny=ymin;
256  if (ymaxVar && ymaxVar->idx()>-1)
257  maxy=ymaxVar->value();
258  else if (isfinite(ymax))
259  maxy=ymax;
260 
261  if (y1minVar && y1minVar->idx()>-1)
262  miny1=y1minVar->value();
263  else if (isfinite(y1min))
264  miny1=ymin;
265  if (y1maxVar && y1maxVar->idx()>-1)
266  maxy1=y1maxVar->value();
267  else if (isfinite(y1max))
268  maxy1=y1max;
269  autoscale=false;
270 
271  if (!justDataChanged)
272  {
273  // label pens. In order or priority:
274  // 1. wire tooltip
275  // 2. from item tooltip
276  // 3. attached variable tooltip
277  // 4. attached variable name
278  size_t pen=0;
279  penLabels.clear();
280  assert(m_ports.size()>=2*m_numLines+nBoundsPorts);
281  for (auto portNo=nBoundsPorts; portNo<2*m_numLines+nBoundsPorts; ++portNo)
282  {
283  if (portNo<m_ports.size())
284  for (size_t i=0; i<m_ports[portNo]->wires().size(); ++i, ++pen)
285  {
286  auto wire=m_ports[portNo]->wires()[i];
287  if (!wire->tooltip().empty())
288  {
289  labelPen(pen, wire->tooltip());
290  continue;
291  }
292  if (auto from=wire->from(); !from->item().tooltip().empty())
293  {
294  labelPen(pen, from->item().tooltip());
295  continue;
296  }
297  if (portNo-nBoundsPorts<yvars.size() && i<yvars[portNo-nBoundsPorts].size())
298  if (auto v=yvars[portNo-nBoundsPorts][i]; !v->name.empty())
299  labelPen(pen, uqName(v->name));
300  }
301  }
302  }
303  }
std::string uqName(const std::string &name)
extract unqualified portion of name
Definition: valueId.cc:135
std::shared_ptr< VariableValue > xminVar
variable ports specifying plot size
Definition: plotWidget.h:103
double xmin
Settable bounds values if above variables not connected NaN means autocalculate from data...
Definition: plotWidget.h:106
static constexpr unsigned nBoundsPorts
number of bounds (xmin/xmax, ymin/ymax, y1min/y1max) ports
Definition: plotWidget.h:87
std::vector< std::vector< std::shared_ptr< VariableValue > > > yvars
variable port attached to (if any)
Definition: plotWidget.h:90
void labelPen(size_t pen, const std::string &label)
Definition: plotWidget.h:95
std::string timeUnit
Definition: simulation.h:35
double yearToPTime(double x)
Definition: plotWidget.h:38
std::shared_ptr< VariableValue > xmaxVar
Definition: plotWidget.h:103
const Minsky & cminsky()
const version to help in const correctness
Definition: minsky.h:549
std::shared_ptr< VariableValue > y1maxVar
Definition: plotWidget.h:103
ItemPortVector m_ports
Definition: item.h:156
std::shared_ptr< VariableValue > ymaxVar
Definition: plotWidget.h:103
std::shared_ptr< VariableValue > yminVar
Definition: plotWidget.h:103
std::vector< std::string > penLabels
Definition: plotWidget.h:94
UnitsExpressionWalker timeUnit
WirePtr wire
item or wire obtained by get*At() calls
size_t m_numLines
number of ports on the side
Definition: plotWidget.h:74
std::shared_ptr< VariableValue > y1minVar
Definition: plotWidget.h:103
Here is the call graph for this function:

◆ startPen()

size_t minsky::PlotWidget::startPen ( size_t  port) const
private

returns to starting pen number for input port

Definition at line 792 of file plotWidget.cc.

793  {
794  size_t pen=0;
795  for (size_t p=0; p<std::min(port, yvars.size()); ++p)
796  pen+=yvars[p].size();
797  return pen;
798  }
std::vector< std::vector< std::shared_ptr< VariableValue > > > yvars
variable port attached to (if any)
Definition: plotWidget.h:90

◆ updateIcon()

void minsky::PlotWidget::updateIcon ( double  t)
inlineoverridevirtual

update display after a step()

Reimplemented from minsky::Item.

Definition at line 160 of file plotWidget.h.

160 {addPlotPt(t);}
void addPlotPt(double t)
add another plot point
Definition: plotWidget.cc:458

◆ width()

float minsky::PlotWidget::width ( ) const
inline

Definition at line 130 of file plotWidget.h.

References minsky::Item::width().

130 {return Item::width();}
float width() const
Definition: item.h:242
Here is the call graph for this function:

◆ x()

float minsky::Item::x

Definition at line 107 of file item.cc.

108  {
109  if (auto g=group.lock())
110  return zoomFactor()*m_x+g->x();
111  return m_x;
112  }
virtual double zoomFactor() const
Definition: group.tcl:84
float m_x
Definition: item.h:173

◆ xlabel() [1/2]

std::string const& minsky::PlotWidget::xlabel ( ) const
inline

shadowed label commands to allow latex intepretation

Definition at line 137 of file plotWidget.h.

137 {return m_xlabel;}
std::string m_xlabel
Definition: plotWidget.h:68

◆ xlabel() [2/2]

std::string const& minsky::PlotWidget::xlabel ( const std::string &  x)
inline

Definition at line 138 of file plotWidget.h.

References minsky::latexToPangoNonItalicised().

138  {
139  ecolab::Plot::xlabel=latexToPangoNonItalicised(x);
140  return m_xlabel=x;
141  }
virtual float x() const
Definition: item.cc:107
string latexToPangoNonItalicised(const char *input)
Definition: latexMarkup.cc:855
std::string m_xlabel
Definition: plotWidget.h:68
Here is the call graph for this function:

◆ y()

float minsky::Item::y

Definition at line 114 of file item.cc.

115  {
116  if (auto g=group.lock())
117  return zoomFactor()*m_y+g->y();
118  return m_y;
119  }
virtual double zoomFactor() const
Definition: group.tcl:84
float m_y
position in canvas, or within group
Definition: item.h:173

◆ y1label() [1/2]

std::string const& minsky::PlotWidget::y1label ( ) const
inline

Definition at line 147 of file plotWidget.h.

147 {return m_y1label;}
std::string m_y1label
Definition: plotWidget.h:68

◆ y1label() [2/2]

std::string const& minsky::PlotWidget::y1label ( const std::string &  x)
inline

Definition at line 148 of file plotWidget.h.

References minsky::latexToPangoNonItalicised().

148  {
149  ecolab::Plot::y1label=latexToPangoNonItalicised(x);
150  return m_y1label=x;
151  }
virtual float x() const
Definition: item.cc:107
string latexToPangoNonItalicised(const char *input)
Definition: latexMarkup.cc:855
std::string m_y1label
Definition: plotWidget.h:68
Here is the call graph for this function:

◆ ylabel() [1/2]

std::string const& minsky::PlotWidget::ylabel ( ) const
inline

Definition at line 142 of file plotWidget.h.

142 {return m_ylabel;}
std::string m_ylabel
Definition: plotWidget.h:68

◆ ylabel() [2/2]

std::string const& minsky::PlotWidget::ylabel ( const std::string &  x)
inline

Definition at line 143 of file plotWidget.h.

References minsky::latexToPangoNonItalicised().

143  {
144  ecolab::Plot::ylabel=latexToPangoNonItalicised(x);
145  return m_ylabel=x;
146  }
virtual float x() const
Definition: item.cc:107
string latexToPangoNonItalicised(const char *input)
Definition: latexMarkup.cc:855
std::string m_ylabel
Definition: plotWidget.h:68
Here is the call graph for this function:

Friends And Related Function Documentation

◆ PlotItem

friend struct PlotItem
friend

Definition at line 62 of file plotWidget.h.

◆ SchemaHelper

friend struct SchemaHelper
friend

Definition at line 52 of file plotWidget.h.

Member Data Documentation

◆ clearPensOnLabelling

bool minsky::PlotWidget::clearPensOnLabelling =false
private

Definition at line 79 of file plotWidget.h.

◆ clickX

double minsky::PlotWidget::clickX
private

Definition at line 49 of file plotWidget.h.

◆ clickY

double minsky::PlotWidget::clickY
private

Definition at line 49 of file plotWidget.h.

◆ ct

ClickType::Type minsky::PlotWidget::ct
private

Definition at line 50 of file plotWidget.h.

◆ displayFontSize

double minsky::PlotWidget::displayFontSize {3}

Definition at line 109 of file plotWidget.h.

◆ displayNTicks

unsigned minsky::PlotWidget::displayNTicks {3}

number of ticks to show in canvas item

Definition at line 108 of file plotWidget.h.

◆ formatter

Formatter minsky::PlotWidget::formatter =defaultFormatter
private

Definition at line 73 of file plotWidget.h.

◆ horizontalMarkers

std::vector<std::string> minsky::PlotWidget::horizontalMarkers

markers at a position given by a named variable/parameter

Definition at line 111 of file plotWidget.h.

◆ justDataChanged

bool minsky::PlotWidget::justDataChanged =false
mutableprivate

Definition at line 61 of file plotWidget.h.

◆ lastAccumulatedBlitTime

classdesc::Exclude<boost::posix_time::ptime> minsky::PlotWidget::lastAccumulatedBlitTime {boost::posix_time::microsec_clock::local_time()}
private

Definition at line 57 of file plotWidget.h.

◆ lastAdd

classdesc::Exclude<boost::posix_time::ptime> minsky::PlotWidget::lastAdd {boost::posix_time::microsec_clock::local_time()}
private

Definition at line 56 of file plotWidget.h.

◆ m_numLines

size_t minsky::PlotWidget::m_numLines =1
private

number of ports on the side

Definition at line 74 of file plotWidget.h.

◆ m_xlabel

std::string minsky::PlotWidget::m_xlabel
private

Definition at line 68 of file plotWidget.h.

◆ m_y1label

std::string minsky::PlotWidget::m_y1label
private

Definition at line 68 of file plotWidget.h.

◆ m_ylabel

std::string minsky::PlotWidget::m_ylabel
private

Definition at line 68 of file plotWidget.h.

◆ nBoundsPorts

constexpr unsigned minsky::PlotWidget::nBoundsPorts =6
static

number of bounds (xmin/xmax, ymin/ymax, y1min/y1max) ports

Definition at line 87 of file plotWidget.h.

◆ oldLegendFontSz

double minsky::PlotWidget::oldLegendFontSz
private

Definition at line 49 of file plotWidget.h.

◆ oldLegendLeft

double minsky::PlotWidget::oldLegendLeft
private

Definition at line 49 of file plotWidget.h.

◆ oldLegendTop

double minsky::PlotWidget::oldLegendTop
private

Definition at line 49 of file plotWidget.h.

◆ penLabels

std::vector<std::string> minsky::PlotWidget::penLabels

Definition at line 94 of file plotWidget.h.

◆ plotType

PlotType minsky::PlotWidget::plotType =automatic

automatic means choose line or bar depending on the x-vector type.

Definition at line 117 of file plotWidget.h.

◆ portSpace

constexpr double minsky::PlotWidget::portSpace =10
staticprivate

Definition at line 48 of file plotWidget.h.

◆ title

std::string minsky::PlotWidget::title

Definition at line 114 of file plotWidget.h.

◆ verticalMarkers

std::vector<std::string> minsky::PlotWidget::verticalMarkers

Definition at line 112 of file plotWidget.h.

◆ xIsSecsSinceEpoch

bool minsky::PlotWidget::xIsSecsSinceEpoch =false
private

Definition at line 64 of file plotWidget.h.

◆ xmax

double minsky::PlotWidget::xmax =xmin

Definition at line 106 of file plotWidget.h.

◆ xmaxVar

std::shared_ptr<VariableValue> minsky::PlotWidget::xmaxVar

Definition at line 103 of file plotWidget.h.

◆ xmin

double minsky::PlotWidget::xmin =nan("")

Settable bounds values if above variables not connected NaN means autocalculate from data.

Definition at line 106 of file plotWidget.h.

◆ xminVar

std::shared_ptr<VariableValue> minsky::PlotWidget::xminVar

variable ports specifying plot size

Definition at line 103 of file plotWidget.h.

◆ xvars

std::vector<std::shared_ptr<VariableValue> > minsky::PlotWidget::xvars

Definition at line 91 of file plotWidget.h.

◆ y1max

double minsky::PlotWidget::y1max =xmin

Definition at line 106 of file plotWidget.h.

◆ y1maxVar

std::shared_ptr<VariableValue> minsky::PlotWidget::y1maxVar

Definition at line 103 of file plotWidget.h.

◆ y1min

double minsky::PlotWidget::y1min =xmin

Definition at line 106 of file plotWidget.h.

◆ y1minVar

std::shared_ptr<VariableValue> minsky::PlotWidget::y1minVar

Definition at line 103 of file plotWidget.h.

◆ ymax

double minsky::PlotWidget::ymax =xmin

Definition at line 106 of file plotWidget.h.

◆ ymaxVar

std::shared_ptr<VariableValue> minsky::PlotWidget::ymaxVar

Definition at line 103 of file plotWidget.h.

◆ ymin

double minsky::PlotWidget::ymin =xmin

Definition at line 106 of file plotWidget.h.

◆ yminVar

std::shared_ptr<VariableValue> minsky::PlotWidget::yminVar

Definition at line 103 of file plotWidget.h.

◆ yoffs

double minsky::PlotWidget::yoffs =0
mutableprivate

extra offset for titles

Definition at line 71 of file plotWidget.h.

◆ yvars

std::vector<std::vector<std::shared_ptr<VariableValue> > > minsky::PlotWidget::yvars

variable port attached to (if any)

Definition at line 90 of file plotWidget.h.


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