1 //                                               -*- C++ -*-
2 /**
3  *  @brief GraphImplementation implements graphic devices for plotting through R
4  *
5  *  Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6  *
7  *  This library is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 #ifndef OPENTURNS_GRAPHIMPLEMENTATION_HXX
22 #define OPENTURNS_GRAPHIMPLEMENTATION_HXX
23 
24 #include "openturns/PersistentObject.hxx"
25 #include "openturns/PersistentCollection.hxx"
26 #include "openturns/Drawable.hxx"
27 
28 BEGIN_NAMESPACE_OPENTURNS
29 
30 /**
31  * @class GraphImplementation
32  *
33  * GraphImplementation implements graphic devices for plotting through R,
34  * and manages drawables to be plotted on the same window
35  */
36 
37 class OT_API GraphImplementation :
38   public PersistentObject
39 {
40 
41   CLASSNAME
42 
43 public:
44 
45   typedef Collection<Drawable>           DrawableCollection;
46   typedef PersistentCollection<Drawable> DrawablePersistentCollection;
47 
48   enum Format { PNG = 1, EPS = 2, FIG = 4, PDF = 8, ALL = 255 };
49   enum LogScale { NONE = 0, LOGX, LOGY, LOGXY };
50   enum TickLocation { TICKNONE, TICKX, TICKY, TICKXY };
51 
52   /** Default constructor */
53   explicit GraphImplementation(const String & title = "");
54 
55   /** Constructor with parameters */
56   GraphImplementation(const String & title,
57                       const String & xTitle,
58                       const String & yTitle,
59                       const Bool showAxes,
60                       const String & legendPosition = "",
61                       const Scalar legendFontSize = ResourceMap::GetAsScalar("Graph-DefaultLegendFontSize"),
62                       const LogScale logScale = NONE);
63 
64   /** Virtual constructor */
65   GraphImplementation * clone() const override;
66 
67   /** String converter */
68   String __repr__() const override;
69 
70   /** Adds a drawable instance to the collection of drawables contained in GraphImplementation */
71   void add(const Drawable & aDrawable);
72 
73   /** Adds a collection of drawable instances to the collection of drawables contained in GraphImplementation */
74   void add(const DrawableCollection & drawableCollection);
75 
76   /** Adds all the drawables in the GraphImplementation to the collection of drawables contained in GraphImplementation */
77   void add(const GraphImplementation & graphImplementation);
78 
79   /** Erase a drawable instance from the collection of drawables contained in GraphImplementation */
80   void erase(const UnsignedInteger i);
81 
82   /** Drawables accessor */
83   DrawableCollection getDrawables() const;
84   void setDrawables(const DrawableCollection & drawableCollection);
85 
86   /** Individual drawable accessor */
87   Drawable getDrawable(const UnsignedInteger index) const;
88   void setDrawable(const Drawable & drawable,
89                    const UnsignedInteger index);
90 
91   /** Global color accessor */
92   Description getColors() const;
93   void setColors(const Description & colors);
94   void setDefaultColors();
95 
96   /** Global legend accessor */
97   Description getLegends() const;
98   void setLegends(const Description & legends);
99 
100   /** Hide or show x and y axes */
101   virtual void setAxes(const Bool showAxes);
102   Bool getAxes() const;
103 
104   /** Ticks location flag accessor */
105   virtual void setTickLocation(const TickLocation tickLocation);
106   TickLocation getTickLocation() const;
107 
108   /** Set log scale for x, y both or none axes */
109   virtual void setLogScale(const LogScale logScale);
110   LogScale getLogScale() const;
111 
112   /** Hide or show grid */
113   virtual void setGrid(const Bool showGrid);
114   Bool getGrid() const;
115 
116   /** Grid color accessor */
117   virtual void setGridColor(const String & color);
118   String getGridColor() const;
119 
120   /** Accessor for xTitle */
121   String getXTitle() const;
122   void setXTitle(const String & title);
123 
124   /** Accessor for yTitle */
125   String getYTitle() const;
126   void setYTitle(const String & title);
127 
128   /** Accessor for title */
129   virtual String getTitle() const;
130   void setTitle(const String & title);
131 
132   /** The method that generates the graphic files */
133   void draw(const String & file,
134             const Scalar width = ResourceMap::GetAsUnsignedInteger("Graph-DefaultWidth"),
135             const Scalar height = ResourceMap::GetAsUnsignedInteger("Graph-DefaultHeight"),
136             SignedInteger format = ALL);
137 
138   /** Get the R command corresponding to the graph */
139   String getRCommand() const;
140 
141   /** Clean temporary files */
142   void clean();
143 
144   /** Margin accessor */
145   virtual void setXMargin(const Scalar xMargin);
146   virtual void setYMargin(const Scalar yMargin);
147 
148   /** Get the bounding box of the whole plot */
149   virtual Interval getBoundingBox() const;
150   virtual void setBoundingBox(const Interval & boundingBox);
151 
152   /** Automatic bounding box accessor */
153   virtual Bool getAutomaticBoundingBox() const;
154   virtual void setAutomaticBoundingBox(const Bool automaticBoundingBox);
155 
156   /** Legend position accessor */
157   virtual void setLegendPosition(const String & position);
158   String getLegendPosition() const;
159 
160   /** Get the legend font size */
161   Scalar getLegendFontSize() const;
162   void setLegendFontSize(const Scalar legendFontSize);
163 
164   /** Check for legend position validity */
165   static Bool IsValidLegendPosition(const String & position);
166 
167   /** Method save() stores the object through the StorageManager */
168   void save(Advocate & adv) const override;
169 
170   /** Method load() reloads the object from the StorageManager */
171   void load(Advocate & adv) override;
172 
173   /** Gives all the valid legend positions */
174   static Description GetValidLegendPositions();
175 
176 private:
177   static Description ValidLegendPositions;
178   static Bool IsFirstInitialization;
179 
180   /** Initialize valid legend positions **/
181   static void InitializeValidLegendPositions();
182 
183   /** Initialize format enum/extension map */
184   static std::map<SignedInteger, String> GetExtensionMap();
185 
186   /** Compute the best bounding box to enclose all the drawables */
187   void computeBoundingBox() const;
188 
189   /** Make R legend command */
190   String makeRLegendCommand() const;
191 
192   /** Make R header command */
193   String makeRHeaderCommand() const;
194 
195   /** Make R core command */
196   String makeRCoreCommand() const;
197 
198   /** main title */
199   String title_;
200 
201   /** Legend position */
202   String legendPosition_;
203 
204   /** Legend font size */
205   Scalar legendFontSize_;
206 
207   /** x axis title */
208   String xTitle_;
209 
210   /** y axis title */
211   String yTitle_;
212 
213   /** if TRUE, displays the axes on the graphic plot. if FALSE, hides the axes */
214   Bool showAxes_;
215 
216   /** Ticks location flag */
217   TickLocation tickLocation_ = TICKXY;
218 
219   /** Set the log scale for one, both or non of the axes */
220   LogScale logScale_;
221 
222   /** if TRUE, displays a grid on the graphic plot. if FALSE, hides the grid */
223   Bool showGrid_;
224 
225   /** Grid color */
226   String gridColor_;
227 
228   /** Margins (ratio) */
229   Scalar xMargin_;
230   Scalar yMargin_;
231 
232   /** Is the bounding box automatically computed. */
233   Bool automaticBoundingBox_;
234 
235   /** Current bounding box */
236   mutable Interval boundingBox_;
237 
238   /** The drawables to be plotted */
239   DrawablePersistentCollection drawablesCollection_;
240 
241 }; /* class GraphImplementation */
242 
243 END_NAMESPACE_OPENTURNS
244 
245 #endif /* OPENTURNS_GRAPHIMPLEMENTATION_HXX */
246