plotdata.cpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #include "PlotJuggler/plotdata.h"
8 
9 namespace PJ
10 {
11 template <typename T>
12 typename std::unordered_map<std::string, T>::iterator
13 addImpl(std::unordered_map<std::string, T>& series, const std::string& name,
14  PlotGroup::Ptr group)
15 {
16  std::string ID;
17  if (group)
18  {
19  ID = group->name();
20  if (ID.back() != '/')
21  {
22  ID.push_back('/');
23  }
24  }
25  ID += name;
26 
27  return series
28  .emplace(std::piecewise_construct, std::forward_as_tuple(name),
29  std::forward_as_tuple(name, group))
30  .first;
31 }
32 
33 template <typename T>
34 T& getOrCreateImpl(std::unordered_map<std::string, T>& series, const std::string& name,
35  const PlotGroup::Ptr& group)
36 {
37  auto it = series.find(name);
38  if (it == series.end())
39  {
40  it = addImpl(series, name, group);
41  }
42  return it->second;
43 }
44 
45 ScatterXYMap::iterator PlotDataMapRef::addScatterXY(const std::string& name,
46  PlotGroup::Ptr group)
47 {
48  return addImpl(scatter_xy, name, group);
49 }
50 
51 TimeseriesMap::iterator PlotDataMapRef::addNumeric(const std::string& name,
52  PlotGroup::Ptr group)
53 {
54  return addImpl(numeric, name, group);
55 }
56 
57 AnySeriesMap::iterator PlotDataMapRef::addUserDefined(const std::string& name,
58  PlotGroup::Ptr group)
59 {
60  return addImpl(user_defined, name, group);
61 }
62 
63 StringSeriesMap::iterator PlotDataMapRef::addStringSeries(const std::string& name,
64  PlotGroup::Ptr group)
65 {
66  return addImpl(strings, name, group);
67 }
68 
70  PlotGroup::Ptr group)
71 {
72  return getOrCreateImpl(scatter_xy, name, group);
73 }
74 
76  PlotGroup::Ptr group)
77 {
78  return getOrCreateImpl(numeric, name, group);
79 }
80 
82  PlotGroup::Ptr group)
83 {
84  return getOrCreateImpl(strings, name, group);
85 }
86 
88  PlotGroup::Ptr group)
89 {
90  return getOrCreateImpl(user_defined, name, group);
91 }
92 
94 {
95  if (name.empty())
96  {
97  throw std::runtime_error("Group name can not be empty");
98  }
99  auto& group = groups[name];
100  if (!group)
101  {
102  group = std::make_shared<PlotGroup>(name);
103  }
104  return group;
105 }
106 
107 std::unordered_set<std::string> PlotDataMapRef::getAllNames() const
108 {
109  std::unordered_set<std::string> out;
110  for (auto& it : numeric)
111  {
112  out.insert(it.first);
113  }
114  for (auto& it : strings)
115  {
116  out.insert(it.first);
117  }
118  for (auto& it : user_defined)
119  {
120  out.insert(it.first);
121  }
122  return out;
123 }
124 
126 {
127  numeric.clear();
128  strings.clear();
129  user_defined.clear();
130 }
131 
133 {
134  for (auto& it : numeric)
135  {
136  it.second.setMaximumRangeX(range);
137  }
138  for (auto& it : strings)
139  {
140  it.second.setMaximumRangeX(range);
141  }
142  for (auto& it : user_defined)
143  {
144  it.second.setMaximumRangeX(range);
145  }
146 }
147 
148 bool PlotDataMapRef::erase(const std::string& name)
149 {
150  bool erased = false;
151  auto num_it = numeric.find(name);
152  if (num_it != numeric.end())
153  {
154  numeric.erase(num_it);
155  erased = true;
156  }
157 
158  auto str_it = strings.find(name);
159  if (str_it != strings.end())
160  {
161  strings.erase(str_it);
162  erased = true;
163  }
164 
165  auto any_it = user_defined.find(name);
166  if (any_it != user_defined.end())
167  {
168  user_defined.erase(any_it);
169  erased = true;
170  }
171  return erased;
172 }
173 
174 } // namespace PJ
PJ::PlotDataMapRef::getOrCreateUserDefined
PlotDataAny & getOrCreateUserDefined(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:87
PJ::PlotDataMapRef::addScatterXY
ScatterXYMap::iterator addScatterXY(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:45
PJ::TimeseriesBase
Definition: timeseries.h:16
PJ::PlotDataMapRef::addStringSeries
StringSeriesMap::iterator addStringSeries(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:63
PJ::PlotDataMapRef::addNumeric
TimeseriesMap::iterator addNumeric(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:51
PJ::PlotDataMapRef::erase
bool erase(const std::string &name)
Definition: plotdata.cpp:148
PJ::getOrCreateImpl
T & getOrCreateImpl(std::unordered_map< std::string, T > &series, const std::string &name, const PlotGroup::Ptr &group)
Definition: plotdata.cpp:34
PJ::PlotDataMapRef::getOrCreateGroup
PlotGroup::Ptr getOrCreateGroup(const std::string &name)
Definition: plotdata.cpp:93
PJ::PlotDataMapRef::numeric
TimeseriesMap numeric
Numerical timeseries.
Definition: plotdata.h:39
PJ::PlotDataMapRef::setMaximumRangeX
void setMaximumRangeX(double range)
Definition: plotdata.cpp:132
PJ::PlotGroup::Ptr
std::shared_ptr< PlotGroup > Ptr
Definition: plotdatabase.h:83
PJ::PlotDataMapRef::getOrCreateScatterXY
PlotDataXY & getOrCreateScatterXY(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:69
PJ::PlotDataMapRef::scatter_xy
ScatterXYMap scatter_xy
Definition: plotdata.h:36
PJ::PlotDataMapRef::clear
void clear()
Definition: plotdata.cpp:125
PJ::PlotDataMapRef::addUserDefined
AnySeriesMap::iterator addUserDefined(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:57
PJ::PlotDataMapRef::groups
std::unordered_map< std::string, PlotGroup::Ptr > groups
Each series can have (optionally) a group. Groups can have their own properties.
Definition: plotdata.h:52
PJ::PlotDataMapRef::getOrCreateNumeric
PlotData & getOrCreateNumeric(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:75
plotdata.h
PJ::addImpl
std::unordered_map< std::string, T >::iterator addImpl(std::unordered_map< std::string, T > &series, const std::string &name, PlotGroup::Ptr group)
Definition: plotdata.cpp:13
PJ
Definition: dataloader_base.h:16
PJ::PlotDataMapRef::getAllNames
std::unordered_set< std::string > getAllNames() const
Definition: plotdata.cpp:107
PJ::PlotDataMapRef::getOrCreateStringSeries
StringSeries & getOrCreateStringSeries(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:81
PJ::PlotDataMapRef::user_defined
AnySeriesMap user_defined
Definition: plotdata.h:43
PJ::PlotDataBase
Definition: plotdatabase.h:122
PJ::PlotDataMapRef::strings
StringSeriesMap strings
Series of strings.
Definition: plotdata.h:46
PJ::StringSeries
Definition: stringseries.h:17


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45