Minsky
plotWidget.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2012
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef PLOTWIDGET_H
20 #define PLOTWIDGET_H
21 #include <cairo_base.h>
22 #include "renderNativeWindow.h"
23 #include <TCL_obj_base.h>
24 #include "classdesc_access.h"
25 #include "latexMarkup.h"
26 #include "plot.h"
27 #include "variable.h"
28 #include "variableValue.h"
29 #include "zoom.h"
30 
31 #include <boost/date_time/posix_time/posix_time_types.hpp>
32 #include <boost/date_time/posix_time/ptime.hpp>
33 
34 namespace minsky
35 {
36  // seconds in a year. Gregorian year chosen.
37  const double yearLength = 3600*24*365.2525;
38  inline double yearToPTime(double x) {return (x-1970)*yearLength;}
39 
40  using namespace ecolab;
41 
42  struct PlotWidgetSuper: public ItemT<PlotWidget>,
43  public ecolab::Plot, public RenderNativeWindow {};
44 
47  {
48  static constexpr double portSpace=10; // allow space for ports
49  double clickX, clickY, oldLegendLeft, oldLegendTop, oldLegendFontSz;
52  friend struct SchemaHelper;
53  // timestamp of last time this widget was blitted and also the
54  // accumulated blit time at last add.
55  classdesc::Exclude<boost::posix_time::ptime>
56  lastAdd{boost::posix_time::microsec_clock::local_time()},
57  lastAccumulatedBlitTime{boost::posix_time::microsec_clock::local_time()};
58  // overrides placement of ports etc when just data has
59  // changed. Single shot flag that is reset after the next call to
60  // draw(), which is const, so this attribute needs to be mutable.
61  mutable bool justDataChanged=false;
62  friend struct PlotItem;
63 
64  bool xIsSecsSinceEpoch=false;
65  bool redraw(int, int, int, int) override;
66 
67  // shadow labels, so we can interpret as LaTeX code rather than Pango markup
68  std::string m_xlabel, m_ylabel, m_y1label;
69 
71  mutable double yoffs=0;
72 
73  Formatter formatter=defaultFormatter;
74  size_t m_numLines=1;
75 
77  size_t startPen(size_t port) const;
78 
79  bool clearPensOnLabelling=false;
80 
81  public:
82  using Item::x;
83  using Item::y;
84  using ecolab::CairoSurface::surface;
85 
87  static constexpr unsigned nBoundsPorts=6;
88 
90  std::vector<std::vector<std::shared_ptr<VariableValue>>> yvars;
91  std::vector<std::shared_ptr<VariableValue>> xvars;
92 
93  // cache penLabels to label pen styles dialog
94  std::vector<std::string> penLabels;
95  void labelPen(size_t pen, const std::string& label) {
96  if (clearPensOnLabelling) {penLabels.clear(); clearPensOnLabelling=false;}
97  if (penLabels.size()<=pen+1) penLabels.resize(pen+1);
98  penLabels[pen]=label;
99  Plot::labelPen(pen,latexToPango(label));
100  }
101 
103  std::shared_ptr<VariableValue> xminVar, xmaxVar, yminVar, ymaxVar, y1minVar, y1maxVar;
106  double xmin=nan(""), xmax=xmin, ymin=xmin, ymax=xmin, y1min=xmin, y1max=xmin;
108  unsigned displayNTicks{3};
109  double displayFontSize{3};
111  std::vector<std::string> horizontalMarkers;
112  std::vector<std::string> verticalMarkers;
113 
114  std::string title;
115 
117  PlotType plotType=automatic;
118 
119  PlotWidget();
120  void addPorts();
121  PlotWidget(const PlotWidget& x): PlotWidgetSuper(x) {addPorts();}
122  PlotWidget(PlotWidget&& x): PlotWidgetSuper(x) {addPorts();}
123 
125  size_t numLines() const {return m_numLines;}
126  size_t numLines(size_t n);
128 
129  // pick the Item width method, not ecolab::Plot's
130  float width() const {return Item::width();}
131  float height() const {return Item::height();}
132 
133  const PlotWidget* plotWidgetCast() const override {return this;}
134  PlotWidget* plotWidgetCast() override {return this;}
135 
137  std::string const& xlabel() const {return m_xlabel;}
138  std::string const& xlabel(const std::string& x) {
139  ecolab::Plot::xlabel=latexToPangoNonItalicised(x);
140  return m_xlabel=x;
141  }
142  std::string const& ylabel() const {return m_ylabel;}
143  std::string const& ylabel(const std::string& x) {
144  ecolab::Plot::ylabel=latexToPangoNonItalicised(x);
145  return m_ylabel=x;
146  }
147  std::string const& y1label() const {return m_y1label;}
148  std::string const& y1label(const std::string& x) {
149  ecolab::Plot::y1label=latexToPangoNonItalicised(x);
150  return m_y1label=x;
151  }
152 
154  // for now, set all bar width to the same value
155  double barWidth() const;
156  double barWidth(double w);
158 
159  void addPlotPt(double t);
160  void updateIcon(double t) override {addPlotPt(t);}
162  void addConstantCurves();
164  void connectVar(const std::shared_ptr<VariableValue>& var, unsigned port);
165  void disconnectAllVars();
166  using ecolab::Plot::draw;
167  void draw(cairo_t* cairo) const override;
168  void requestRedraw();
169  void redrawWithBounds() override {redraw(0,0,500,500);}
170 
172  void makeDisplayPlot();
173 
174  void resize(const LassoBox&) override;
175  ClickType::Type clickType(float x, float y) const override;
176  bool contains(float x, float y) const override
177  {return clickType(x,y)!=ClickType::outside;}
178 
180  void autoScale() {xminVar=xmaxVar=yminVar=ymaxVar=y1minVar=y1maxVar=nullptr;}
182  void scalePlot();
183 
185  void mouseDown(float,float) override;
186  void mouseMove(float,float) override;
187  void mouseUp(float x,float y) override {
188  mouseMove(x,y);
190  }
191  bool onMouseOver(float,float) override;
192  void onMouseLeave() override {valueString="";}
194 
196  // implemented as a single argument function here for exposure to TCL
197  void exportAsCSV(const string& filename) {ecolab::Plot::exportAsCSV(filename);}
198 
200 
202  std::set<std::string> availableMarkers() const;
203 
204  };
205 
206 }
207 
208 #ifdef CLASSDESC
209 #pragma omit pack PlotItem
210 #pragma omit unpack PlotItem
211 #endif
212 
213 inline void xml_pack(classdesc::xml_pack_t&,const ecolab::string&,ecolab::Plot&) {}
214 inline void xml_unpack(classdesc::xml_unpack_t&,const ecolab::string&,ecolab::Plot&) {}
215 
216 #include "plotWidget.cd"
217 #include "plotWidget.xcd"
218 #endif
void autoScale()
set autoscaling
Definition: plotWidget.h:180
std::string latexToPango(const char *s)
Definition: latexMarkup.h:30
void xml_unpack(classdesc::xml_unpack_t &, const ecolab::string &, ecolab::Plot &)
Definition: plotWidget.h:214
void exportAsCSV(const string &filename)
export the plotted data as a CSV file
Definition: plotWidget.h:197
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
float height() const
Definition: plotWidget.h:131
represents rectangular region of a lasso operation
Definition: lasso.h:28
std::string const & ylabel() const
Definition: plotWidget.h:142
PlotWidget * plotWidgetCast() override
Definition: plotWidget.h:134
std::string title
Definition: plotWidget.h:114
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
mouseDownid x y X Y
Definition: godley.tcl:137
void updateIcon(double t) override
update display after a step()
Definition: plotWidget.h:160
void xml_pack(classdesc::xml_pack_t &, const ecolab::string &, ecolab::Plot &)
Definition: plotWidget.h:213
std::string const & ylabel(const std::string &x)
Definition: plotWidget.h:143
void mouseUp(float x, float y) override
handle mouse events
Definition: plotWidget.h:187
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
float width() const
Definition: item.h:242
std::string const & y1label(const std::string &x)
Definition: plotWidget.h:148
double yearToPTime(double x)
Definition: plotWidget.h:38
const PlotWidget * plotWidgetCast() const override
Definition: plotWidget.h:133
void destroyFrame() override
destroy any popup windows associated with this
Definition: plotWidget.h:199
#define CLASSDESC_ACCESS(type)
void onMouseLeave() override
handle mouse events
Definition: plotWidget.h:192
string latexToPangoNonItalicised(const char *input)
Definition: latexMarkup.cc:855
std::vector< std::string > horizontalMarkers
markers at a position given by a named variable/parameter
Definition: plotWidget.h:111
bool contains(float x, float y) const override
Definition: plotWidget.h:176
ClickType::Type ct
Definition: plotWidget.h:50
float height() const
Definition: item.h:243
std::string const & y1label() const
Definition: plotWidget.h:147
PlotWidget(PlotWidget &&x)
Definition: plotWidget.h:122
std::shared_ptr< VariableValue > yminVar
Definition: plotWidget.h:103
const double yearLength
Definition: plotWidget.h:37
float width() const
Definition: plotWidget.h:130
std::string m_ylabel
Definition: plotWidget.h:68
std::vector< std::string > penLabels
Definition: plotWidget.h:94
std::vector< std::shared_ptr< VariableValue > > xvars
Definition: plotWidget.h:91
std::string const & xlabel(const std::string &x)
Definition: plotWidget.h:138
a container item for a plot widget
Definition: plotWidget.h:46
std::string const & xlabel() const
shadowed label commands to allow latex intepretation
Definition: plotWidget.h:137
void redrawWithBounds() override
Definition: plotWidget.h:169
PlotWidget(const PlotWidget &x)
Definition: plotWidget.h:121
size_t numLines() const
number of input ports along a side
Definition: plotWidget.h:125