sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
LayoutSpecs.hpp
1 // sciplot - a modern C++ scientific plotting library powered by gnuplot
2 // https://github.com/sciplot/sciplot
3 //
4 // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
5 //
6 // Copyright (c) 2018-2022 Allan Leal, Bim Overbohm
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 // sciplot includes
29 #include <sciplot/specs/Specs.hpp>
30 
31 namespace sciplot
32 {
33 
35 template <typename DerivedSpecs>
36 class LayoutSpecsOf : virtual public Specs<DerivedSpecs>
37 {
38  public:
40  auto origin(double x, double y) -> DerivedSpecs&;
41 
43  auto size(double sx, double sy) -> DerivedSpecs&;
44 
46  auto marginsAbsolute(double left = -1, double right = -1, double top = -1, double bottom = -1) -> DerivedSpecs&;
47 
51  // auto marginsRelative(double left = -1, double right = -1, double top = -1, double bottom = -1) -> DerivedSpecs&;
52 
54  auto repr() const -> std::string;
55 
56  private:
58  double m_originX = 0.0;
60  double m_originY = 0.0;
62  bool m_originSet = false;
63 
65  double m_sizeX = 0;
67  double m_sizeY = 0;
69  bool m_sizeSet = false;
70 
72  double m_marginsLeft = 0.0;
74  double m_marginsRight = 0.0;
76  double m_marginsTop = 0.0;
78  double m_marginsBottom = 0.0;
80  double m_marginsAbsolute = false;
82  bool m_marginsSet = false;
83 };
84 
87 {
88 };
89 
90 template <typename DerivedSpecs>
91 auto LayoutSpecsOf<DerivedSpecs>::origin(double x, double y) -> DerivedSpecs&
92 {
93  m_originX = x;
94  m_originY = y;
95  m_originSet = true;
96  return static_cast<DerivedSpecs&>(*this);
97 }
98 
99 template <typename DerivedSpecs>
100 auto LayoutSpecsOf<DerivedSpecs>::size(double sx, double sy) -> DerivedSpecs&
101 {
102  m_sizeX = sx;
103  m_sizeY = sy;
104  m_sizeSet = true;
105  return static_cast<DerivedSpecs&>(*this);
106 }
107 
108 template <typename DerivedSpecs>
109 auto LayoutSpecsOf<DerivedSpecs>::marginsAbsolute(double left, double right, double top, double bottom) -> DerivedSpecs&
110 {
111  m_marginsLeft = left;
112  m_marginsRight = right;
113  m_marginsTop = top;
114  m_marginsBottom = bottom;
115  m_marginsAbsolute = true;
116  m_marginsSet = true;
117  return static_cast<DerivedSpecs&>(*this);
118 }
119 
120 /*
121 // This does not seem to work for me with gnuplot 5.4 patchlevel 2
122 template <typename DerivedSpecs>
123 auto LayoutSpecsOf<DerivedSpecs>::marginsRelative(double left, double right, double top, double bottom) -> DerivedSpecs&
124 {
125  m_marginsLeft = left;
126  m_marginsRight = right;
127  m_marginsTop = top;
128  m_marginsBottom = bottom;
129  m_marginsAbsolute = false;
130  m_marginsSet = true;
131  return static_cast<DerivedSpecs&>(*this);
132 }*/
133 
134 template <typename DerivedSpecs>
135 auto LayoutSpecsOf<DerivedSpecs>::repr() const -> std::string
136 {
137  std::stringstream script;
138  if (m_originSet)
139  {
140  script << "set origin " << m_originX << "," << m_originY << std::endl;
141  }
142  if (m_sizeSet)
143  {
144  script << "set size " << m_sizeX << "," << m_sizeY << std::endl;
145  }
146  if (m_marginsSet)
147  {
148  script << "set lmargin " << (m_marginsAbsolute ? "" : "at screen ") << m_marginsLeft << std::endl;
149  script << "set rmargin " << (m_marginsAbsolute ? "" : "at screen ") << m_marginsRight << std::endl;
150  script << "set tmargin " << (m_marginsAbsolute ? "" : "at screen ") << m_marginsTop << std::endl;
151  script << "set bmargin " << (m_marginsAbsolute ? "" : "at screen ") << m_marginsBottom << std::endl;
152  }
153  return script.str();
154 }
155 
156 } // namespace sciplot
auto size(double sx, double sy) -> DerivedSpecs &
Set the size factor of the figure relative to the canvas (a value of 1 will fill the entire canvas,...
Definition: LayoutSpecs.hpp:100
auto marginsAbsolute(double left=-1, double right=-1, double top=-1, double bottom=-1) -> DerivedSpecs &
Set the absolute margins of the figure to the canvas. A value of -1 will let gnuplot autocalculate th...
Definition: LayoutSpecs.hpp:109
auto repr() const -> std::string
Set the relative margins of the figure to the canvas as a fraction of the canvas.
Definition: LayoutSpecs.hpp:135
auto origin(double x, double y) -> DerivedSpecs &
Set the origin of the figure relative to the canvas (0,0 is bottom left and 1,1 is top right).
Definition: LayoutSpecs.hpp:91
The base class for other specs classes (e.g., LineSpecsOf, DrawSpecs, BorderSpecs,...
Definition: Specs.hpp:36
The class used to attach layout options to a type.
Definition: LayoutSpecs.hpp:37
The class used to specify layout options.
Definition: LayoutSpecs.hpp:87