sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
GridSpecsBase.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/DepthSpecsOf.hpp>
31 #include <sciplot/specs/LineSpecsOf.hpp>
32 #include <sciplot/specs/ShowSpecsOf.hpp>
33 #include <sciplot/Utils.hpp>
34 
35 namespace sciplot {
36 
38 class GridSpecsBase : public LineSpecsOf<GridSpecsBase>, public DepthSpecsOf<GridSpecsBase>, public ShowSpecsOf<GridSpecsBase>
39 {
40  public:
42  GridSpecsBase(std::string tics = "", bool majortics = true);
43 
45  auto repr() const -> std::string;
46 
47  private:
49  std::string m_tics;
50 
52  bool m_majortics;
53 };
54 
55 inline GridSpecsBase::GridSpecsBase(std::string tics, bool majortics)
56 : m_tics(tics), m_majortics(majortics)
57 {
58  show(true);
59  back();
60  lineColor(internal::DEFAULT_GRID_LINECOLOR);
61  lineWidth(internal::DEFAULT_GRID_LINEWIDTH);
62  lineType(internal::DEFAULT_GRID_LINETYPE);
63  dashType(internal::DEFAULT_GRID_DASHTYPE);
64 }
65 
66 inline auto GridSpecsBase::repr() const -> std::string
67 {
69  const auto visible = show != "no";
70 
71  if(m_tics.empty() && !visible)
72  return "unset grid";
73 
74  if(m_tics.size() && !visible)
75  return "set grid no" + m_tics;
76 
77  std::stringstream ss;
78  ss << "set grid " << m_tics << " ";
79  ss << DepthSpecsOf<GridSpecsBase>::repr() << " ";
80  if(m_majortics)
81  ss << LineSpecsOf<GridSpecsBase>::repr();
82  else
83  ss << ", " + LineSpecsOf<GridSpecsBase>::repr(); // For minor tics, the preceding comma is needed
84  return internal::removeExtraWhitespaces(ss.str());
85 }
86 
87 } // namespace sciplot
auto lineWidth(int value) -> GridSpecsBase &
Set the line width of the underlying line object.
Definition: LineSpecsOf.hpp:101
auto repr() const -> std::string
Convert this LineSpecsOf object into a gnuplot formatted string.
Definition: LineSpecsOf.hpp:122
The class used to specify options for grid lines along axis tics (major or minor).
Definition: GridSpecsBase.hpp:39
auto show(bool value=true) -> GridSpecsBase &
Set the visibility status of the plot element.
Definition: ShowSpecsOf.hpp:68
The class used to attach depth options to a type.
Definition: DepthSpecsOf.hpp:36
The class used to attach visibility options to a type.
Definition: ShowSpecsOf.hpp:36
auto lineColor(std::string value) -> GridSpecsBase &
Set the line color of the underlying line object.
Definition: LineSpecsOf.hpp:108
The class used to attach line options to a type.
Definition: LineSpecsOf.hpp:38
auto lineType(int value) -> GridSpecsBase &
Set the line type of the underlying line object.
Definition: LineSpecsOf.hpp:94
auto repr() const -> std::string
Convert this ShowSpecsOf object into a gnuplot formatted string.
Definition: ShowSpecsOf.hpp:87
auto repr() const -> std::string
Convert this GridSpecsBase object into a gnuplot formatted string.
Definition: GridSpecsBase.hpp:66
auto back() -> GridSpecsBase &
Set the underlying plot element to be displayed on the back of all plot elements.
Definition: DepthSpecsOf.hpp:77
GridSpecsBase(std::string tics="", bool majortics=true)
Construct a default GridSpecsBase instance.
Definition: GridSpecsBase.hpp:55
auto dashType(int value) -> GridSpecsBase &
Set the dash type of the underlying line object.
Definition: LineSpecsOf.hpp:115