Minsky: 3.17.0
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 "classdesc_access.h"
24 #include "latexMarkup.h"
25 #include "plot.h"
26 #include "variable.h"
27 #include "variableValue.h"
28 #include "zoom.h"
29 
30 #include <boost/date_time/posix_time/posix_time_types.hpp>
31 #include <boost/date_time/posix_time/ptime.hpp>
32 
33 namespace minsky
34 {
35  // seconds in a year. Gregorian year chosen.
36  const double yearLength = 3600*24*365.2525;
37  inline double yearToPTime(double x) {return (x-1970)*yearLength;}
38 
39  using namespace ecolab;
40 
41  struct PlotWidgetSuper: public ItemT<PlotWidget>,
42  public ecolab::Plot, public RenderNativeWindow {};
43 
46  {
47  static constexpr double portSpace=10; // allow space for ports
48  double clickX, clickY, oldLegendLeft, oldLegendTop, oldLegendFontSz;
51  friend struct SchemaHelper;
52  // timestamp of last time this widget was blitted and also the
53  // accumulated blit time at last add.
54  classdesc::Exclude<boost::posix_time::ptime>
55  lastAdd{boost::posix_time::microsec_clock::local_time()},
56  lastAccumulatedBlitTime{boost::posix_time::microsec_clock::local_time()};
57  // overrides placement of ports etc when just data has
58  // changed. Single shot flag that is reset after the next call to
59  // draw(), which is const, so this attribute needs to be mutable.
60  mutable bool justDataChanged=false;
61  friend struct PlotItem;
62 
63  bool xIsSecsSinceEpoch=false;
64  bool redraw(int, int, int, int) override;
65  using Plot::redraw;
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 RenderNativeWindow::surface;
86  using RenderNativeWindow::renderToSVG;
87 
89  static constexpr unsigned nBoundsPorts=6;
90 
92  std::vector<std::vector<std::shared_ptr<VariableValue>>> yvars;
93  std::vector<std::shared_ptr<VariableValue>> xvars;
94 
95  // cache penLabels to label pen styles dialog
96  std::vector<std::string> penLabels;
97  void labelPen(size_t pen, const std::string& label) {
98  if (clearPensOnLabelling) {penLabels.clear(); clearPensOnLabelling=false;}
99  if (penLabels.size()<=pen+1) penLabels.resize(pen+1);
100  penLabels[pen]=label;
101  Plot::labelPen(pen,latexToPango(label));
102  }
103 
105  std::shared_ptr<VariableValue> xminVar, xmaxVar, yminVar, ymaxVar, y1minVar, y1maxVar;
108  double xmin=nan(""), xmax=xmin, ymin=xmin, ymax=xmin, y1min=xmin, y1max=xmin;
110  unsigned displayNTicks{3};
111  double displayFontSize{3};
113  std::vector<std::string> horizontalMarkers;
114  std::vector<std::string> verticalMarkers;
115 
116  std::string title;
117 
119  PlotType plotType=automatic;
120 
121  PlotWidget();
122  void addPorts();
123  PlotWidget(const PlotWidget& x): PlotWidgetSuper(x) {addPorts();}
124  PlotWidget(PlotWidget&& x): PlotWidgetSuper(x) {addPorts();}
125 
127  size_t numLines() const {return m_numLines;}
128  size_t numLines(size_t n);
130 
131  // pick the Item width method, not ecolab::Plot's
132  float width() const {return Item::width();}
133  float height() const {return Item::height();}
134 
135  const PlotWidget* plotWidgetCast() const override {return this;}
136  PlotWidget* plotWidgetCast() override {return this;}
137 
139  std::string const& xlabel() const {return m_xlabel;}
140  std::string const& xlabel(const std::string& x) {
141  ecolab::Plot::xlabel=latexToPangoNonItalicised(x);
142  return m_xlabel=x;
143  }
144  std::string const& ylabel() const {return m_ylabel;}
145  std::string const& ylabel(const std::string& x) {
146  ecolab::Plot::ylabel=latexToPangoNonItalicised(x);
147  return m_ylabel=x;
148  }
149  std::string const& y1label() const {return m_y1label;}
150  std::string const& y1label(const std::string& x) {
151  ecolab::Plot::y1label=latexToPangoNonItalicised(x);
152  return m_y1label=x;
153  }
154 
156  // for now, set all bar width to the same value
157  double barWidth() const;
158  double barWidth(double w);
160 
161  void addPlotPt(double t);
162  void updateIcon(double t) override {addPlotPt(t);}
164  void addConstantCurves();
166  void connectVar(const std::shared_ptr<VariableValue>& var, unsigned port);
167  void disconnectAllVars();
168  using ecolab::Plot::draw;
169  void draw(cairo_t* cairo) const override;
170  void requestRedraw();
171  void redrawWithBounds() override {redraw(0,0,500,500);}
172 
174  void makeDisplayPlot();
175 
176  void resize(const LassoBox&) override;
177  ClickType::Type clickType(float x, float y) const override;
178  bool contains(float x, float y) const override
179  {return clickType(x,y)!=ClickType::outside;}
180 
182  void autoScale() {xminVar=xmaxVar=yminVar=ymaxVar=y1minVar=y1maxVar=nullptr;}
184  void scalePlot();
185 
187  void mouseDown(float,float) override;
188  void mouseMove(float,float) override;
189  void mouseUp(float x,float y) override {
190  mouseMove(x,y);
192  }
193  bool onMouseOver(float,float) override;
194  void onMouseLeave() override {valueString="";}
196 
198  // implemented as a single argument function here for exposure to TCL
199  void exportAsCSV(const string& filename) {ecolab::Plot::exportAsCSV(filename);}
200 
202 
204  std::set<std::string> availableMarkers() const;
205 
206  };
207 
208 }
209 
210 #ifdef CLASSDESC
211 #pragma omit pack PlotItem
212 #pragma omit unpack PlotItem
213 #endif
214 
215 inline void xml_pack(classdesc::xml_pack_t&,const ecolab::string&,ecolab::Plot&) {}
216 inline void xml_unpack(classdesc::xml_unpack_t&,const ecolab::string&,ecolab::Plot&) {}
217 
218 #include "plotWidget.cd"
219 #include "plotWidget.xcd"
220 #endif
void autoScale()
set autoscaling
Definition: plotWidget.h:182
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:216
void exportAsCSV(const string &filename)
export the plotted data as a CSV file
Definition: plotWidget.h:199
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
float height() const
Definition: plotWidget.h:133
represents rectangular region of a lasso operation
Definition: lasso.h:28
std::string const & ylabel() const
Definition: plotWidget.h:144
PlotWidget * plotWidgetCast() override
Definition: plotWidget.h:136
std::string title
Definition: plotWidget.h:116
std::vector< std::vector< std::shared_ptr< VariableValue > > > yvars
variable port attached to (if any)
Definition: plotWidget.h:92
std::vector< std::string > verticalMarkers
Definition: plotWidget.h:114
void labelPen(size_t pen, const std::string &label)
Definition: plotWidget.h:97
void updateIcon(double t) override
update display after a step()
Definition: plotWidget.h:162
void xml_pack(classdesc::xml_pack_t &, const ecolab::string &, ecolab::Plot &)
Definition: plotWidget.h:215
std::string const & ylabel(const std::string &x)
Definition: plotWidget.h:145
void mouseUp(float x, float y) override
handle mouse events
Definition: plotWidget.h:189
float width() const
Definition: item.h:239
std::string const & y1label(const std::string &x)
Definition: plotWidget.h:150
double yearToPTime(double x)
Definition: plotWidget.h:37
const PlotWidget * plotWidgetCast() const override
Definition: plotWidget.h:135
void destroyFrame() override
destroy any popup windows associated with this
Definition: plotWidget.h:201
#define CLASSDESC_ACCESS(type)
void onMouseLeave() override
handle mouse events
Definition: plotWidget.h:194
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:113
bool contains(float x, float y) const override
Definition: plotWidget.h:178
ClickType::Type ct
Definition: plotWidget.h:49
float height() const
Definition: item.h:240
std::string const & y1label() const
Definition: plotWidget.h:149
PlotWidget(PlotWidget &&x)
Definition: plotWidget.h:124
std::shared_ptr< VariableValue > yminVar
Definition: plotWidget.h:105
const double yearLength
Definition: plotWidget.h:36
float width() const
Definition: plotWidget.h:132
std::string m_ylabel
Definition: plotWidget.h:68
std::vector< std::string > penLabels
Definition: plotWidget.h:96
std::vector< std::shared_ptr< VariableValue > > xvars
Definition: plotWidget.h:93
std::string const & xlabel(const std::string &x)
Definition: plotWidget.h:140
a container item for a plot widget
Definition: plotWidget.h:45
std::string const & xlabel() const
shadowed label commands to allow latex intepretation
Definition: plotWidget.h:139
void redrawWithBounds() override
Definition: plotWidget.h:171
PlotWidget(const PlotWidget &x)
Definition: plotWidget.h:123
size_t numLines() const
number of input ports along a side
Definition: plotWidget.h:127