sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
OffsetSpecsOf.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/specs/TextSpecsOf.hpp>
31 #include <sciplot/Utils.hpp>
32 
33 namespace sciplot {
34 
36 template <typename DerivedSpecs>
37 class OffsetSpecsOf : virtual public Specs<DerivedSpecs>
38 {
39  public:
42 
44  auto shiftAlongX(double chars) -> DerivedSpecs&;
45 
47  auto shiftAlongY(double chars) -> DerivedSpecs&;
48 
50  auto shiftAlongGraphX(double val) -> DerivedSpecs&;
51 
53  auto shiftAlongGraphY(double val) -> DerivedSpecs&;
54 
56  auto shiftAlongScreenX(double val) -> DerivedSpecs&;
57 
59  auto shiftAlongScreenY(double val) -> DerivedSpecs&;
60 
62  auto repr() const -> std::string;
63 
64  private:
66  std::string xoffset = "0";
67 
69  std::string yoffset = "0";
70 };
71 
74 
75 template <typename DerivedSpecs>
77 {
78 }
79 
80 template <typename DerivedSpecs>
81 auto OffsetSpecsOf<DerivedSpecs>::shiftAlongX(double chars) -> DerivedSpecs&
82 {
83  xoffset = internal::str(chars);
84  return static_cast<DerivedSpecs&>(*this);
85 }
86 
87 template <typename DerivedSpecs>
88 auto OffsetSpecsOf<DerivedSpecs>::shiftAlongY(double chars) -> DerivedSpecs&
89 {
90  yoffset = internal::str(chars);
91  return static_cast<DerivedSpecs&>(*this);
92 }
93 
94 template <typename DerivedSpecs>
95 auto OffsetSpecsOf<DerivedSpecs>::shiftAlongGraphX(double val) -> DerivedSpecs&
96 {
97  xoffset = "graph " + internal::str(val);
98  return static_cast<DerivedSpecs&>(*this);
99 }
100 
101 template <typename DerivedSpecs>
102 auto OffsetSpecsOf<DerivedSpecs>::shiftAlongGraphY(double val) -> DerivedSpecs&
103 {
104  yoffset = "graph " + internal::str(val);
105  return static_cast<DerivedSpecs&>(*this);
106 }
107 
108 template <typename DerivedSpecs>
109 auto OffsetSpecsOf<DerivedSpecs>::shiftAlongScreenX(double val) -> DerivedSpecs&
110 {
111  xoffset = "screen " + internal::str(val);
112  return static_cast<DerivedSpecs&>(*this);
113 }
114 
115 template <typename DerivedSpecs>
116 auto OffsetSpecsOf<DerivedSpecs>::shiftAlongScreenY(double val) -> DerivedSpecs&
117 {
118  yoffset = "screen " + internal::str(val);
119  return static_cast<DerivedSpecs&>(*this);
120 }
121 
122 template <typename DerivedSpecs>
123 auto OffsetSpecsOf<DerivedSpecs>::repr() const -> std::string
124 {
125  std::string offset;
126 
127  if(xoffset != "0" || yoffset != "0")
128  offset = "offset " + xoffset + ", " + yoffset;
129 
130  std::stringstream ss;
131  ss << gnuplot::optionStr(offset);
132  return internal::removeExtraWhitespaces(ss.str());
133 }
134 
135 } // namespace sciplot
The class used to specify offset options.
Definition: OffsetSpecsOf.hpp:73
auto shiftAlongY(double chars) -> DerivedSpecs &
Shift the underlying plot element along the y direction by given number of characters (can be fractio...
Definition: OffsetSpecsOf.hpp:88
auto shiftAlongX(double chars) -> DerivedSpecs &
Shift the underlying plot element along the x direction by given number of characters (can be fractio...
Definition: OffsetSpecsOf.hpp:81
auto shiftAlongScreenY(double val) -> DerivedSpecs &
Shift the underlying plot element along the y direction within the screen coordinate system.
Definition: OffsetSpecsOf.hpp:116
auto repr() const -> std::string
Convert this OffsetSpecsOf object into a gnuplot formatted string.
Definition: OffsetSpecsOf.hpp:123
OffsetSpecsOf()
Construct a default OffsetSpecsOf instance.
Definition: OffsetSpecsOf.hpp:76
The class used to attach offset options to a type.
Definition: OffsetSpecsOf.hpp:38
The base class for other specs classes (e.g., LineSpecsOf, DrawSpecs, BorderSpecs,...
Definition: Specs.hpp:36
auto shiftAlongScreenX(double val) -> DerivedSpecs &
Shift the underlying plot element along the x direction within the screen coordinate system.
Definition: OffsetSpecsOf.hpp:109
auto shiftAlongGraphX(double val) -> DerivedSpecs &
Shift the underlying plot element along the x direction within the graph coordinate system.
Definition: OffsetSpecsOf.hpp:95
auto shiftAlongGraphY(double val) -> DerivedSpecs &
Shift the underlying plot element along the y direction within the graph coordinate system.
Definition: OffsetSpecsOf.hpp:102