sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
LegendSpecs.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/FrameSpecsOf.hpp>
31 #include <sciplot/specs/ShowSpecsOf.hpp>
32 #include <sciplot/specs/TextSpecsOf.hpp>
33 #include <sciplot/specs/TitleSpecsOf.hpp>
34 #include <sciplot/Utils.hpp>
35 
36 namespace sciplot {
37 
39 class LegendSpecs : public TextSpecsOf<LegendSpecs>, public ShowSpecsOf<LegendSpecs>, public TitleSpecsOf<LegendSpecs>, public FrameSpecsOf<LegendSpecs>
40 {
41  public:
43  LegendSpecs();
44 
46  auto opaque() -> LegendSpecs&;
47 
49  auto transparent() -> LegendSpecs&;
50 
52  auto atLeft() -> LegendSpecs&;
53 
55  auto atRight() -> LegendSpecs&;
56 
58  auto atCenter() -> LegendSpecs&;
59 
61  auto atTop() -> LegendSpecs&;
62 
64  auto atTopLeft() -> LegendSpecs&;
65 
67  auto atTopRight() -> LegendSpecs&;
68 
70  auto atBottom() -> LegendSpecs&;
71 
73  auto atBottomLeft() -> LegendSpecs&;
74 
76  auto atBottomRight() -> LegendSpecs&;
77 
79  auto atOutsideLeft() -> LegendSpecs&;
80 
82  auto atOutsideLeftTop() -> LegendSpecs&;
83 
86 
88  auto atOutsideRight() -> LegendSpecs&;
89 
91  auto atOutsideRightTop() -> LegendSpecs&;
92 
95 
97  auto atOutsideBottom() -> LegendSpecs&;
98 
100  auto atOutsideBottomLeft() -> LegendSpecs&;
101 
104 
106  auto atOutsideTop() -> LegendSpecs&;
107 
109  auto atOutsideTopLeft() -> LegendSpecs&;
110 
112  auto atOutsideTopRight() -> LegendSpecs&;
113 
115  auto titleLeft() -> LegendSpecs&;
116 
118  auto titleCenter() -> LegendSpecs&;
119 
121  auto titleRight() -> LegendSpecs&;
122 
124  auto displayVertical() -> LegendSpecs&;
125 
127  auto displayVerticalMaxRows(int value) -> LegendSpecs&;
128 
130  auto displayHorizontal() -> LegendSpecs&;
131 
133  auto displayHorizontalMaxCols(int value) -> LegendSpecs&;
134 
137 
140 
142  auto displayJustifyLeft() -> LegendSpecs&;
143 
145  auto displayJustifyRight() -> LegendSpecs&;
146 
149 
152 
154  auto displaySpacing(int value) -> LegendSpecs&;
155 
157  auto displayExpandWidthBy(int value) -> LegendSpecs&;
158 
160  auto displayExpandHeightBy(int value) -> LegendSpecs&;
161 
163  auto displaySymbolLength(int value) -> LegendSpecs&;
164 
166  auto repr() const -> std::string;
167 
168  private:
170  std::string m_placement;
171 
173  std::string m_opaque;
174 
176  std::string m_alignment;
177 
179  std::string m_reverse;
180 
182  std::string m_invert;
183 
185  std::string m_justification;
186 
188  std::string m_title_loc = "left";
189 
191  int m_width_increment;
192 
194  int m_height_increment;
195 
197  int m_samplen;
198 
200  int m_spacing;
201 
203  std::string m_maxrows = "auto";
204 
206  std::string m_maxcols = "auto";
207 };
208 
210 {
211  atTopRight();
212  title("");
213  displayExpandWidthBy(internal::DEFAULT_LEGEND_FRAME_EXTRA_WIDTH);
214  displayExpandHeightBy(internal::DEFAULT_LEGEND_FRAME_EXTRA_HEIGHT);
215  displaySymbolLength(internal::DEFAULT_LEGEND_SAMPLE_LENGTH);
216  displaySpacing(internal::DEFAULT_LEGEND_SPACING);
217  displayVertical();
221  opaque();
222 }
223 
225 {
226  m_opaque = "opaque";
227  return *this;
228 }
229 
231 {
232  m_opaque = "noopaque";
233  return *this;
234 }
235 
237 {
238  m_placement = "inside left";
239  return *this;
240 }
241 
243 {
244  m_placement = "inside right";
245  return *this;
246 }
247 
249 {
250  m_placement = "inside center";
251  return *this;
252 }
253 
255 {
256  m_placement = "inside center top";
257  return *this;
258 }
259 
261 {
262  m_placement = "inside left top";
263  return *this;
264 }
265 
267 {
268  m_placement = "inside right top";
269  return *this;
270 }
271 
273 {
274  m_placement = "inside center bottom";
275  return *this;
276 }
277 
279 {
280  m_placement = "inside left bottom";
281  return *this;
282 }
283 
285 {
286  m_placement = "inside right bottom";
287  return *this;
288 }
289 
291 {
292  m_placement = "lmargin center";
293  return *this;
294 }
295 
297 {
298  m_placement = "lmargin top";
299  return *this;
300 }
301 
303 {
304  m_placement = "lmargin bottom";
305  return *this;
306 }
307 
309 {
310  m_placement = "rmargin center";
311  return *this;
312 }
313 
315 {
316  m_placement = "rmargin top";
317  return *this;
318 }
319 
321 {
322  m_placement = "rmargin bottom";
323  return *this;
324 }
325 
327 {
328  m_placement = "bmargin center";
329  return *this;
330 }
331 
333 {
334  m_placement = "bmargin left";
335  return *this;
336 }
337 
339 {
340  m_placement = "bmargin right";
341  return *this;
342 }
343 
345 {
346  m_placement = "tmargin center";
347  return *this;
348 }
349 
351 {
352  m_placement = "tmargin left";
353  return *this;
354 }
355 
357 {
358  m_placement = "tmargin right";
359  return *this;
360 }
361 
363 {
364  m_title_loc = "left";
365  return *this;
366 }
367 
369 {
370  m_title_loc = "center";
371  return *this;
372 }
373 
375 {
376  m_title_loc = "right";
377  return *this;
378 }
379 
381 {
382  m_alignment = "vertical";
383  return *this;
384 }
385 
387 {
388  m_maxrows = internal::str(value);
389  return *this;
390 }
391 
393 {
394  m_alignment = "horizontal";
395  return *this;
396 }
397 
399 {
400  m_maxcols = internal::str(value);
401  return *this;
402 }
403 
405 {
406  m_reverse = "noreverse";
407  return *this;
408 }
409 
411 {
412  m_reverse = "reverse";
413  return *this;
414 }
415 
417 {
418  m_justification = "Left";
419  return *this;
420 }
421 
423 {
424  m_justification = "Right";
425  return *this;
426 }
427 
429 {
430  m_invert = "noinvert";
431  return *this;
432 }
433 
435 {
436  m_invert = "invert";
437  return *this;
438 }
439 
440 inline auto LegendSpecs::displaySpacing(int value) -> LegendSpecs&
441 {
442  m_spacing = value;
443  return *this;
444 }
445 
447 {
448  m_width_increment = value;
449  return *this;
450 }
451 
453 {
454  m_height_increment = value;
455  return *this;
456 }
457 
459 {
460  m_samplen = value;
461  return *this;
462 }
463 
464 inline auto LegendSpecs::repr() const -> std::string
465 {
466  const auto show = ShowSpecsOf<LegendSpecs>::repr();
467  if(show == "no")
468  return "unset key";
469 
470  auto titlespecs = TitleSpecsOf<LegendSpecs>::repr();
471  if(titlespecs.size())
472  titlespecs += " " + m_title_loc; // attach left|center|right to title (e.g. title 'Legend' right)
473 
474  std::stringstream ss;
475  ss << "set key ";
476  ss << m_placement << " " << m_opaque << " ";
477  ss << m_alignment << " ";
478  ss << m_justification << " ";
479  ss << m_invert << " ";
480  ss << m_reverse << " ";
481  ss << "width " << m_width_increment << " ";
482  ss << "height " << m_height_increment << " ";
483  ss << "samplen " << m_samplen << " ";
484  ss << "spacing " << m_spacing << " ";
485  ss << TextSpecsOf<LegendSpecs>::repr() << " ";
486  ss << titlespecs << " ";
487  ss << FrameSpecsOf<LegendSpecs>::repr() << " ";
488  ss << "maxrows " << m_maxrows << " ";
489  ss << "maxcols " << m_maxcols << " ";
490  return internal::removeExtraWhitespaces(ss.str());
491 }
492 
493 } // namespace sciplot
auto atBottomLeft() -> LegendSpecs &
Place the legend on the inside of the plot at its bottom-left corner.
Definition: LegendSpecs.hpp:278
auto displayJustifyRight() -> LegendSpecs &
Set the legend labels to be right justified.
Definition: LegendSpecs.hpp:422
auto repr() const -> std::string
Convert this LegendSpecs object into a gnuplot formatted string.
Definition: LegendSpecs.hpp:464
auto displaySymbolLength(int value) -> LegendSpecs &
Set the length of the samples used to generate the symbols in the legend entries.
Definition: LegendSpecs.hpp:458
auto displayLabelsAfterSymbols() -> LegendSpecs &
Set the labels in the legend entries to appear after their corresponding symbols (on the right).
Definition: LegendSpecs.hpp:410
auto displayStartFromLast() -> LegendSpecs &
Set the legend entries to be displayed in the order from last to first.
Definition: LegendSpecs.hpp:434
auto atOutsideLeftBottom() -> LegendSpecs &
Place the legend on the outside of the plot at its left-bottom corner.
Definition: LegendSpecs.hpp:302
auto displayStartFromFirst() -> LegendSpecs &
Set the legend entries to be displayed in the order from first to last.
Definition: LegendSpecs.hpp:428
auto atOutsideTopRight() -> LegendSpecs &
Place the legend on the outside of the plot at its top-right corner.
Definition: LegendSpecs.hpp:356
auto displaySpacing(int value) -> LegendSpecs &
Set the spacing between the titles in the legend.
Definition: LegendSpecs.hpp:440
The class used to attach title options to a type.
Definition: TitleSpecsOf.hpp:39
auto atOutsideBottomLeft() -> LegendSpecs &
Place the legend on the outside of the plot at its bottom-left corner.
Definition: LegendSpecs.hpp:332
auto title(std::string title) -> LegendSpecs &
Set the text of the title.
Definition: TitleSpecsOf.hpp:98
auto atLeft() -> LegendSpecs &
Place the legend on the inside of the plot at its left side.
Definition: LegendSpecs.hpp:236
auto show(bool value=true) -> LegendSpecs &
Set the visibility status of the plot element.
Definition: ShowSpecsOf.hpp:68
auto displayJustifyLeft() -> LegendSpecs &
Set the legend labels to be left justified.
Definition: LegendSpecs.hpp:416
The class used to attach visibility options to a type.
Definition: ShowSpecsOf.hpp:36
auto atOutsideTop() -> LegendSpecs &
Place the legend on the outside of the plot at its top side.
Definition: LegendSpecs.hpp:344
The class used to attach text options to a type.
Definition: TextSpecsOf.hpp:38
auto atRight() -> LegendSpecs &
Place the legend on the inside of the plot at its right side.
Definition: LegendSpecs.hpp:242
auto atCenter() -> LegendSpecs &
Place the legend on the inside of the plot at its center.
Definition: LegendSpecs.hpp:248
auto atTopLeft() -> LegendSpecs &
Place the legend on the inside of the plot at its top-left corner.
Definition: LegendSpecs.hpp:260
auto atOutsideLeftTop() -> LegendSpecs &
Place the legend on the outside of the plot at its left-top corner.
Definition: LegendSpecs.hpp:296
auto atBottomRight() -> LegendSpecs &
Place the legend on the inside of the plot at its bottom-right corner.
Definition: LegendSpecs.hpp:284
The class used to specify options for legend.
Definition: LegendSpecs.hpp:40
auto repr() const -> std::string
Convert this ShowSpecsOf object into a gnuplot formatted string.
Definition: ShowSpecsOf.hpp:87
auto atTop() -> LegendSpecs &
Place the legend on the inside of the plot at its top side.
Definition: LegendSpecs.hpp:254
auto titleLeft() -> LegendSpecs &
Place the legend title on the left.
Definition: LegendSpecs.hpp:362
auto transparent() -> LegendSpecs &
Set the background of the legend box to be transparent.
Definition: LegendSpecs.hpp:230
auto displayExpandWidthBy(int value) -> LegendSpecs &
Set the width increment/decrement of the legend frame to either enlarge or reduce its width.
Definition: LegendSpecs.hpp:446
auto displayHorizontal() -> LegendSpecs &
Set the legend entries to be displayed along the horizontal (in rows).
Definition: LegendSpecs.hpp:392
auto repr() const -> std::string
Convert this TitleSpecsOf object into a gnuplot formatted string.
Definition: TitleSpecsOf.hpp:168
auto displayLabelsBeforeSymbols() -> LegendSpecs &
Set the labels in the legend entries to appear before their corresponding symbols (on the left).
Definition: LegendSpecs.hpp:404
auto atOutsideRightTop() -> LegendSpecs &
Place the legend on the outside of the plot at its right-top corner.
Definition: LegendSpecs.hpp:314
auto displayExpandHeightBy(int value) -> LegendSpecs &
Set the height increment/decrement of the legend frame to either enlarge or reduce its height.
Definition: LegendSpecs.hpp:452
auto displayHorizontalMaxCols(int value) -> LegendSpecs &
Set the number of columns that trigger a new row to be created in the legend (when using horizontal d...
Definition: LegendSpecs.hpp:398
auto atOutsideTopLeft() -> LegendSpecs &
Place the legend on the outside of the plot at its top-left corner.
Definition: LegendSpecs.hpp:350
auto atTopRight() -> LegendSpecs &
Place the legend on the inside of the plot at its top-right corner.
Definition: LegendSpecs.hpp:266
auto atOutsideRight() -> LegendSpecs &
Place the legend on the outside of the plot at its right side.
Definition: LegendSpecs.hpp:308
auto titleCenter() -> LegendSpecs &
Place the legend title on the center.
Definition: LegendSpecs.hpp:368
auto atOutsideLeft() -> LegendSpecs &
Place the legend on the outside of the plot at its left side.
Definition: LegendSpecs.hpp:290
auto opaque() -> LegendSpecs &
Set the background of the legend box to be opaque.
Definition: LegendSpecs.hpp:224
auto titleRight() -> LegendSpecs &
Place the legend title on the right.
Definition: LegendSpecs.hpp:374
auto atOutsideBottom() -> LegendSpecs &
Place the legend on the outside of the plot at its bottom side.
Definition: LegendSpecs.hpp:326
auto displayVerticalMaxRows(int value) -> LegendSpecs &
Set the number of rows that trigger a new column to be created in the legend (when using vertical dis...
Definition: LegendSpecs.hpp:386
auto atOutsideRightBottom() -> LegendSpecs &
Place the legend on the outside of the plot at its right-bottom corner.
Definition: LegendSpecs.hpp:320
auto displayVertical() -> LegendSpecs &
Set the legend entries to be displayed along the vertical (in columns).
Definition: LegendSpecs.hpp:380
auto atBottom() -> LegendSpecs &
Place the legend on the inside of the plot at its bottom side.
Definition: LegendSpecs.hpp:272
auto atOutsideBottomRight() -> LegendSpecs &
Place the legend on the outside of the plot at its bottom-right corner.
Definition: LegendSpecs.hpp:338
LegendSpecs()
Construct a default LegendSpecs instance.
Definition: LegendSpecs.hpp:209
The class used to attach frame options to a type.
Definition: FrameSpecsOf.hpp:38