sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
TicsSpecsMajor.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 // C++ includes
29 #include <vector>
30 
31 // sciplot includes
32 #include <sciplot/Utils.hpp>
33 #include <sciplot/specs/TicsSpecsBaseOf.hpp>
34 
35 namespace sciplot
36 {
37 
39 class TicsSpecsMajor : public TicsSpecsBaseOf<TicsSpecsMajor>
40 {
41  public:
43  TicsSpecsMajor(std::string axis);
44 
46  auto automatic() -> TicsSpecsMajor&;
47 
49  auto start(double value) -> TicsSpecsMajor&;
50 
52  auto increment(double value) -> TicsSpecsMajor&;
53 
55  auto end(double value) -> TicsSpecsMajor&;
56 
58  auto interval(double start, double increment, double end) -> TicsSpecsMajor&;
59 
61  auto at(const std::vector<double>& values) -> TicsSpecsMajor&;
62 
64  auto at(const std::vector<double>& values, const std::vector<std::string>& labels) -> TicsSpecsMajor&;
65 
67  auto add(const std::vector<double>& values) -> TicsSpecsMajor&;
68 
70  auto add(const std::vector<double>& values, const std::vector<std::string>& labels) -> TicsSpecsMajor&;
71 
73  auto repr() const -> std::string;
74 
75  private:
77  const std::string m_axis;
78 
80  std::string m_start;
81 
83  std::string m_increment;
84 
86  std::string m_end;
87 
89  std::string m_at;
90 
92  std::string m_add;
93 };
94 
95 inline TicsSpecsMajor::TicsSpecsMajor(std::string axis)
96  : TicsSpecsBaseOf<TicsSpecsMajor>(), m_axis(axis)
97 {
98  if (axis.empty())
99  throw std::runtime_error(
100  "You have provided an empty string "
101  "in `axis` argument of constructor TicsSpecsMajor(axis).");
102 }
103 
105 {
106  // clear strings related to tics values/labels
107  m_start = "";
108  m_end = "";
109  m_increment = "";
110  m_at = "";
111  return *this;
112 }
113 
114 inline auto TicsSpecsMajor::start(double value) -> TicsSpecsMajor&
115 {
116  m_start = internal::str(value) + ", ";
117  m_at = m_start + m_increment + m_end;
118  return *this;
119 }
120 
121 inline auto TicsSpecsMajor::increment(double value) -> TicsSpecsMajor&
122 {
123  m_increment = internal::str(value);
124  m_at = m_start + m_increment + m_end;
125  return *this;
126 }
127 
128 inline auto TicsSpecsMajor::end(double value) -> TicsSpecsMajor&
129 {
130  m_end = ", " + internal::str(value);
131  m_at = m_start + m_increment + m_end;
132  return *this;
133 }
134 
135 inline auto TicsSpecsMajor::interval(double start, double increment, double end) -> TicsSpecsMajor&
136 {
137  if (increment <= 0.0) throw std::runtime_error("The `increment` argument in method TicsSpecsMajor::interval must be positive.");
138  if (end <= start) throw std::runtime_error("The `end` argument in method TicsSpecsMajor::interval must be greater than `start`.");
139  std::stringstream ss;
140  ss << start << ", " << increment << ", " << end;
141  m_at = ss.str();
142  return *this;
143 }
144 
145 inline auto TicsSpecsMajor::at(const std::vector<double>& values) -> TicsSpecsMajor&
146 {
147  std::stringstream ss;
148  ss << "(";
149  for (auto i = 0; i < values.size(); ++i)
150  ss << (i == 0 ? "" : ", ") << values[i];
151  ss << ")";
152  m_at = ss.str();
153  return *this;
154 }
155 
156 inline auto TicsSpecsMajor::at(const std::vector<double>& values, const std::vector<std::string>& labels) -> TicsSpecsMajor&
157 {
158  std::stringstream ss;
159  ss << "(";
160  for (auto i = 0; i < values.size(); ++i)
161  ss << (i == 0 ? "" : ", ") << "'" << labels[i] << "' " << values[i];
162  ss << ")";
163  m_at = ss.str();
164  return *this;
165 }
166 
167 inline auto TicsSpecsMajor::add(const std::vector<double>& values) -> TicsSpecsMajor&
168 {
169  std::stringstream ss;
170  ss << "add (";
171  for (auto i = 0; i < values.size(); ++i)
172  ss << (i == 0 ? "" : ", ") << values[i];
173  ss << ")";
174  m_add = ss.str();
175  return *this;
176 }
177 
178 inline auto TicsSpecsMajor::add(const std::vector<double>& values, const std::vector<std::string>& labels) -> TicsSpecsMajor&
179 {
180  std::stringstream ss;
181  ss << "add (";
182  for (auto i = 0; i < values.size(); ++i)
183  ss << (i == 0 ? "" : ", ") << "'" << labels[i] << "' " << values[i];
184  ss << ")";
185  m_add = ss.str();
186  return *this;
187 }
188 
189 inline auto TicsSpecsMajor::repr() const -> std::string
190 {
191  const auto baserepr = TicsSpecsBaseOf<TicsSpecsMajor>::repr(m_axis);
192 
193  if (isHidden())
194  return baserepr;
195 
196  if (m_start.size() && m_increment.empty())
197  throw std::runtime_error("You have called method TicsSpecsMajor::start but not TicsSpecsMajor::increment.");
198  if (m_end.size() && m_increment.empty())
199  throw std::runtime_error("You have called method TicsSpecsMajor::end but not TicsSpecsMajor::increment.");
200  if (m_end.size() && m_start.empty())
201  throw std::runtime_error("You have called method TicsSpecsMajor::end but not TicsSpecsMajor::start.");
202 
203  std::stringstream ss;
204  ss << baserepr << " ";
205  ss << m_at << " ";
206  ss << m_add << " ";
207  return internal::removeExtraWhitespaces(ss.str());
208 }
209 
210 } // namespace sciplot
TicsSpecsMajor(std::string axis)
Construct a default TicsSpecsMajor instance.
Definition: TicsSpecsMajor.hpp:95
auto increment(double value) -> TicsSpecsMajor &
Set the increment for the tics (start and end values determined automatically).
Definition: TicsSpecsMajor.hpp:121
auto interval(double start, double increment, double end) -> TicsSpecsMajor &
Set the start, increment and end values of the tics.
Definition: TicsSpecsMajor.hpp:135
auto automatic() -> TicsSpecsMajor &
Set the values of the tics to be identified automatically.
Definition: TicsSpecsMajor.hpp:104
auto start(double value) -> TicsSpecsMajor &
Set the start value for the tics (you must also call method increment).
Definition: TicsSpecsMajor.hpp:114
The class used to specify options for major tics of a specific axis.
Definition: TicsSpecsMajor.hpp:40
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 isHidden() const -> bool
Return true if the underlying plot element is hidden.
Definition: ShowSpecsOf.hpp:81
auto end(double value) -> TicsSpecsMajor &
Set the end value for the tics (you must also call methods start and increment).
Definition: TicsSpecsMajor.hpp:128
auto add(const std::vector< double > &values) -> TicsSpecsMajor &
Add more tics to be displayed with given tic values.
Definition: TicsSpecsMajor.hpp:167
auto at(const std::vector< double > &values) -> TicsSpecsMajor &
Set the values of the tics that should be displayed.
Definition: TicsSpecsMajor.hpp:145
auto repr() const -> std::string
Convert this TicsSpecsMajor object into a gnuplot formatted string.
Definition: TicsSpecsMajor.hpp:189