sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
TicsSpecsBaseOf.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-2021 Allan Leal
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/Default.hpp>
30 #include <sciplot/Utils.hpp>
31 #include <sciplot/specs/OffsetSpecsOf.hpp>
32 #include <sciplot/specs/ShowSpecsOf.hpp>
33 #include <sciplot/specs/TextSpecsOf.hpp>
34 
35 namespace sciplot
36 {
37 
39 template <typename DerivedSpecs>
40 class TicsSpecsBaseOf : public TextSpecsOf<DerivedSpecs>, public OffsetSpecsOf<DerivedSpecs>, public ShowSpecsOf<DerivedSpecs>
41 {
42  public:
45 
47  auto alongAxis() -> DerivedSpecs&;
48 
50  auto alongBorder() -> DerivedSpecs&;
51 
53  auto mirror(bool value = true) -> DerivedSpecs&;
54 
56  auto insideGraph() -> DerivedSpecs&;
57 
59  auto outsideGraph() -> DerivedSpecs&;
60 
62  auto rotate(bool value = true) -> DerivedSpecs&;
63 
65  auto rotateBy(double degrees) -> DerivedSpecs&;
66 
68  auto scaleBy(double value) -> DerivedSpecs&;
69 
71  auto scaleMajorBy(double value) -> DerivedSpecs&;
72 
74  auto scaleMinorBy(double value) -> DerivedSpecs&;
75 
77  auto format(std::string fmt) -> DerivedSpecs&;
78 
80  auto logscale(int base = 10) -> DerivedSpecs&;
81 
83  auto repr() const -> std::string;
84 
86  auto repr(std::string axis) const -> std::string;
87 
88  private:
90  std::string m_along;
91 
93  std::string m_mirror;
94 
96  std::string m_rotate;
97 
99  std::string m_inout;
100 
102  std::string m_format;
103 
105  double m_scalemajor = 1.0;
106 
108  double m_scaleminor = 1.0;
109 
111  std::string m_logscaleBase;
112 };
113 
114 template <typename DerivedSpecs>
116 {
117  alongBorder();
118  mirror(internal::DEFAULT_TICS_MIRROR);
119  outsideGraph();
120  rotate(internal::DEFAULT_TICS_ROTATE);
121  scaleMajorBy(internal::DEFAULT_TICS_SCALE_MAJOR_BY);
122  scaleMinorBy(internal::DEFAULT_TICS_SCALE_MINOR_BY);
123 }
124 
125 template <typename DerivedSpecs>
127 {
128  m_along = "axis";
129  return static_cast<DerivedSpecs&>(*this);
130 }
131 
132 template <typename DerivedSpecs>
134 {
135  m_along = "border";
136  return static_cast<DerivedSpecs&>(*this);
137 }
138 
139 template <typename DerivedSpecs>
140 auto TicsSpecsBaseOf<DerivedSpecs>::mirror(bool value) -> DerivedSpecs&
141 {
142  m_mirror = value ? "mirror" : "nomirror";
143  return static_cast<DerivedSpecs&>(*this);
144 }
145 
146 template <typename DerivedSpecs>
147 auto TicsSpecsBaseOf<DerivedSpecs>::rotate(bool value) -> DerivedSpecs&
148 {
149  m_rotate = value ? "rotate" : "norotate";
150  return static_cast<DerivedSpecs&>(*this);
151 }
152 
153 template <typename DerivedSpecs>
154 auto TicsSpecsBaseOf<DerivedSpecs>::rotateBy(double degrees) -> DerivedSpecs&
155 {
156  m_rotate = "rotate by " + internal::str(degrees);
157  return static_cast<DerivedSpecs&>(*this);
158 }
159 
160 template <typename DerivedSpecs>
162 {
163  m_inout = "in";
164  return static_cast<DerivedSpecs&>(*this);
165 }
166 
167 template <typename DerivedSpecs>
169 {
170  m_inout = "out";
171  return static_cast<DerivedSpecs&>(*this);
172 }
173 
174 template <typename DerivedSpecs>
175 auto TicsSpecsBaseOf<DerivedSpecs>::scaleBy(double value) -> DerivedSpecs&
176 {
177  return scaleMajorBy(value);
178 }
179 
180 template <typename DerivedSpecs>
181 auto TicsSpecsBaseOf<DerivedSpecs>::scaleMajorBy(double value) -> DerivedSpecs&
182 {
183  m_scalemajor = value;
184  return static_cast<DerivedSpecs&>(*this);
185 }
186 
187 template <typename DerivedSpecs>
188 auto TicsSpecsBaseOf<DerivedSpecs>::scaleMinorBy(double value) -> DerivedSpecs&
189 {
190  m_scaleminor = value;
191  return static_cast<DerivedSpecs&>(*this);
192 }
193 
194 template <typename DerivedSpecs>
195 auto TicsSpecsBaseOf<DerivedSpecs>::format(std::string fmt) -> DerivedSpecs&
196 {
197  m_format = "'" + fmt + "'";
198  return static_cast<DerivedSpecs&>(*this);
199 }
200 
201 template <typename DerivedSpecs>
202 auto TicsSpecsBaseOf<DerivedSpecs>::logscale(int base) -> DerivedSpecs&
203 {
204  m_logscaleBase = std::to_string(base);
205  return static_cast<DerivedSpecs&>(*this);
206 }
207 
208 template <typename DerivedSpecs>
209 auto TicsSpecsBaseOf<DerivedSpecs>::repr() const -> std::string
210 {
211  return repr("");
212 }
213 
214 template <typename DerivedSpecs>
215 auto TicsSpecsBaseOf<DerivedSpecs>::repr(std::string axis) const -> std::string
216 {
217  const auto show = ShowSpecsOf<DerivedSpecs>::repr();
218  if (show == "no")
219  return "unset " + axis + "tics";
220 
221  std::stringstream ss;
222  if (!m_logscaleBase.empty())
223  {
224  ss << "set logscale " + axis + " " + m_logscaleBase + "\n";
225  }
226  ss << "set " + axis + "tics"
227  << " ";
228  ss << m_along << " ";
229  ss << m_mirror << " ";
230  ss << m_inout << " ";
231  ss << "scale " << m_scalemajor << "," << m_scaleminor << " ";
232  ss << m_rotate << " ";
233  ss << OffsetSpecsOf<DerivedSpecs>::repr() << " ";
234  ss << TextSpecsOf<DerivedSpecs>::repr() << " ";
235  ss << m_format;
236  return internal::removeExtraWhitespaces(ss.str());
237 }
238 
239 } // namespace sciplot
auto scaleMinorBy(double value) -> DerivedSpecs &
Set the scale for the minor tics.
Definition: TicsSpecsBaseOf.hpp:188
auto outsideGraph() -> DerivedSpecs &
Set the tics to be displayed outside the graph.
Definition: TicsSpecsBaseOf.hpp:168
auto scaleMajorBy(double value) -> DerivedSpecs &
Set the scale for the major tics.
Definition: TicsSpecsBaseOf.hpp:181
TicsSpecsBaseOf()
Construct a default TicsSpecsBaseOf instance.
Definition: TicsSpecsBaseOf.hpp:115
auto scaleBy(double value) -> DerivedSpecs &
Set the scale for the major tics (identical to method scaleMajorBy).
Definition: TicsSpecsBaseOf.hpp:175
auto show(bool value=true) -> DerivedSpecs &
Set the visibility status of the plot element.
Definition: ShowSpecsOf.hpp:68
The class used to attach visibility options to a type.
Definition: ShowSpecsOf.hpp:36
The class used to attach common tic options to a type that also specifies tic options.
Definition: TicsSpecsBaseOf.hpp:41
auto repr() const -> std::string
Convert this TicsSpecsBaseOf object into a gnuplot formatted string.
Definition: TicsSpecsBaseOf.hpp:209
auto rotate(bool value=true) -> DerivedSpecs &
Set the tics to be rotated by 90 degrees if true.
Definition: TicsSpecsBaseOf.hpp:147
auto alongBorder() -> DerivedSpecs &
Set the tics to be displayed along the border.
Definition: TicsSpecsBaseOf.hpp:133
auto rotateBy(double degrees) -> DerivedSpecs &
Set the tics to be rotated by given degrees.
Definition: TicsSpecsBaseOf.hpp:154
The class used to attach text options to a type.
Definition: TextSpecsOf.hpp:38
auto mirror(bool value=true) -> DerivedSpecs &
Set the tics to be mirrored on the opposite border if true.
Definition: TicsSpecsBaseOf.hpp:140
auto logscale(int base=10) -> DerivedSpecs &
Set logarithmic scale with base for an axis.
Definition: TicsSpecsBaseOf.hpp:202
auto repr() const -> std::string
Convert this ShowSpecsOf object into a gnuplot formatted string.
Definition: ShowSpecsOf.hpp:87
auto format(std::string fmt) -> DerivedSpecs &
Set the format of the tics using a format expression ("%.2f")
Definition: TicsSpecsBaseOf.hpp:195
The class used to attach offset options to a type.
Definition: OffsetSpecsOf.hpp:38
auto alongAxis() -> DerivedSpecs &
Set the tics to be displayed along the axis.
Definition: TicsSpecsBaseOf.hpp:126
auto insideGraph() -> DerivedSpecs &
Set the tics to be displayed inside the graph.
Definition: TicsSpecsBaseOf.hpp:161