sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
DrawSpecs.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/ColumnIndex.hpp>
30 #include <sciplot/Utils.hpp>
31 #include <sciplot/specs/FillSpecsOf.hpp>
32 #include <sciplot/specs/FilledCurvesSpecsOf.hpp>
33 #include <sciplot/specs/LineSpecsOf.hpp>
34 #include <sciplot/specs/PointSpecsOf.hpp>
35 
36 namespace sciplot
37 {
38 
40 class DrawSpecs : public LineSpecsOf<DrawSpecs>, public PointSpecsOf<DrawSpecs>, public FillSpecsOf<DrawSpecs>, public FilledCurvesSpecsOf<DrawSpecs>
41 {
42  public:
47  DrawSpecs(std::string what, std::string use, std::string with);
48 
50  auto label(std::string text) -> DrawSpecs&;
51 
54 
56  auto labelFromColumnHeader(int icolumn) -> DrawSpecs&;
57 
59  auto labelNone() -> DrawSpecs&;
60 
62  auto labelDefault() -> DrawSpecs&;
63 
65  auto xtics(ColumnIndex icol) -> DrawSpecs&;
66 
68  auto ytics(ColumnIndex icol) -> DrawSpecs&;
69 
71  auto repr() const -> std::string;
72 
73  private:
75  std::string m_what;
76 
78  std::string m_using;
79 
81  std::string m_with;
82 
84  std::string m_title;
85 
87  std::string m_xtic;
88 
90  std::string m_ytic;
91 };
92 
93 inline DrawSpecs::DrawSpecs(std::string what, std::string use, std::string with)
94  : m_what(what), m_using(use), m_with(with)
95 {
96  lineWidth(internal::DEFAULT_LINEWIDTH);
97 }
98 
99 inline auto DrawSpecs::label(std::string text) -> DrawSpecs&
100 {
101  m_title = "title '" + text + "'";
102  return *this;
103 }
104 
106 {
107  m_title = "title columnheader";
108  return *this;
109 }
110 
111 inline auto DrawSpecs::labelFromColumnHeader(int icolumn) -> DrawSpecs&
112 {
113  m_title = "title columnheader(" + std::to_string(icolumn) + ")";
114  return *this;
115 }
116 
118 {
119  m_title = "notitle";
120  return *this;
121 }
122 
124 {
125  m_title = "";
126  return *this;
127 }
128 
130 {
131  m_xtic = "xtic(stringcolumn(" + icol.value + "))"; // xtic(stringcolumn(1)) or xtic(stringcolumn('Name'))
132  return *this;
133 }
134 
136 {
137  m_ytic = "ytic(stringcolumn(" + icol.value + "))"; // ytic(stringcolumn(1)) or ytic(stringcolumn('Name'))
138  return *this;
139 }
140 
141 inline auto DrawSpecs::repr() const -> std::string
142 {
143  std::string use = m_using;
144  if (m_xtic.size()) use += ":" + m_xtic;
145  if (m_ytic.size()) use += ":" + m_ytic;
146 
147  std::stringstream ss;
148  ss << m_what << " ";
149  ss << gnuplot::optionValueStr("using", use);
150  ss << m_title << " ";
151  ss << gnuplot::optionValueStr("with", m_with);
152  ss << FilledCurvesSpecsOf<DrawSpecs>::repr() << " ";
153  ss << LineSpecsOf<DrawSpecs>::repr() << " ";
154  ss << PointSpecsOf<DrawSpecs>::repr() << " ";
155  ss << FillSpecsOf<DrawSpecs>::repr() << " ";
156  return internal::removeExtraWhitespaces(ss.str());
157 }
158 
159 } // namespace sciplot
The class used to attach color or pattern fill options to a type.
Definition: FillSpecsOf.hpp:39
DrawSpecs(std::string what, std::string use, std::string with)
Construct a DrawSpecs instance.
Definition: DrawSpecs.hpp:93
auto ytics(ColumnIndex icol) -> DrawSpecs &
Set the column in the data file containing the tic labels for y axis.
Definition: DrawSpecs.hpp:135
auto lineWidth(int value) -> DrawSpecs &
Set the line width of the underlying line object.
Definition: LineSpecsOf.hpp:101
auto repr() const -> std::string
Convert this DrawSpecs object into a gnuplot formatted string.
Definition: DrawSpecs.hpp:141
The class used to set options for the gnuplot filledcurve functionality.
Definition: FilledCurvesSpecsOf.hpp:39
The class used to attach line options to a type.
Definition: LineSpecsOf.hpp:38
An auxiliary type used to represent a data column index.
Definition: ColumnIndex.hpp:35
auto label(std::string text) -> DrawSpecs &
Set the legend label of the plotted element.
Definition: DrawSpecs.hpp:99
auto labelDefault() -> DrawSpecs &
Set the legend label to be determined automatically from the plot expression.
Definition: DrawSpecs.hpp:123
auto labelFromColumnHeader() -> DrawSpecs &
Set the legend label of the plotted element to be retrieved from the header of column.
Definition: DrawSpecs.hpp:105
The class where options for the plotted element can be specified.
Definition: DrawSpecs.hpp:41
auto xtics(ColumnIndex icol) -> DrawSpecs &
Set the column in the data file containing the tic labels for x axis.
Definition: DrawSpecs.hpp:129
The class used to attach point options to a type.
Definition: PointSpecsOf.hpp:38
auto labelNone() -> DrawSpecs &
Set the legend label of the plotted element to be ignored.
Definition: DrawSpecs.hpp:117