Bridges-C++  3.4.5-dev1-6-g935685a
Bridges(C++ API)
LineChart.h
Go to the documentation of this file.
1 #ifndef LINE_CHART_H
2 #define LINE_CHART_H
3 
4 #include <unordered_map>
5 #include <vector>
6 #include <string>
7 #include <DataStructure.h>
8 #include <JSONutil.h>
9 
10 namespace bridges {
11  namespace datastructure {
43  class LineChart : public DataStructure {
44 
45  private:
46  string plotTitle;
47  string plotSubtitle;
48  string xLabel;
49  string yLabel;
50  bool mouseTrack;
51  bool dataLabel;
52  bool logarithmicx;
53  bool logarithmicy;
54 
55  unordered_map<string, vector<double >> xaxisData;
56  unordered_map<string, vector<double >> yaxisData;
57 
58  public:
60  plotTitle = "";
61  plotSubtitle = "";
62  yLabel = "";
63  xLabel = "";
64  mouseTrack = false;
65  dataLabel = true;
66  logarithmicx = false;
67  logarithmicy = false;
68  }
69 
74  virtual const string getDStype() const override {
75  return "LineChart";
76  }
77 
86  void toggleMouseTrack(bool val) {
87  mouseTrack = val;
88  }
89 
98  void toggleSeriesLabel(bool val) {
99  dataLabel = val;
100  }
101 
109  void toggleLogarithmicX(bool val) {
110  logarithmicx = val;
111  }
112 
120  void toggleLogarithmicY(bool val) {
121  logarithmicy = val;
122  }
123 
129  void setTitle(string t) {
130  plotTitle = t;
131  }
132 
138  string getTitle() const {
139  return plotTitle;
140  }
141 
147  void setSubTitle(string s) {
148  plotSubtitle = s;
149  }
150 
156  string getSubTitle() const {
157  return plotSubtitle;
158  }
159 
165  void setYLabel(string yaxisName) {
166  yLabel = yaxisName;
167  }
168 
174  string getYLabel() const {
175  return yLabel;
176  }
177 
183  void setXLabel(string xaxisName) {
184  xLabel = xaxisName;
185  }
186 
192  string getXLabel() const {
193  return xLabel;
194  }
195 
203  void setDataSeries(string seriesName, vector<double> xdata, vector<double> ydata) {
204  setXData(seriesName, xdata);
205  setYData(seriesName, ydata);
206  }
207 
214  void setXData(string series, vector<double> xdata) {
215  xaxisData[series] = xdata;
216  }
217 
224  vector<double> getXData(string series) {
225  return xaxisData[series];
226  }
227 
234  void setYData(string series, vector<double> ydata) {
235  yaxisData[series] = ydata;
236  }
237 
244  vector<double> getYData(string series) {
245  return yaxisData[series];
246  }
247 
248  private:
260  bool check() const {
261  bool correct = true;
262  for (auto& entry : xaxisData) {
263  string series = entry.first;
264  vector<double> xdata = entry.second;
265  vector<double> ydata = yaxisData.at(series);
266  if (!ydata.size()) {
267  cout << "Series \"" + series + "\" has xdata but no ydata";
268  correct = false;
269  }
270  if (xdata.size() != ydata.size()) {
271  cout << "Series \"" + series + "\" has xdata and ydata of different sizes";
272  correct = false;
273  }
274  if (logarithmicx) {
275  for (int i = 0; i < xdata.size(); ++i) {
276  if (xdata[i] == 0) {
277  cout << "Xaxis scale is logarithmic but series \"" + series
278  + "\" has xdata[" << i << "] = " << xdata[i] <<
279  " (should be stricly positive)";
280  }
281  }
282  }
283  if (logarithmicy) {
284  for (int i = 0; i < ydata.size(); ++i) {
285  if (ydata[i] == 0) {
286  cout << "Yaxis scale is logarithmic but series \"" +
287  series + "\" has ydata[" << i << "] = " << ydata[i] <<
288  " (should be stricly positive)";
289  }
290  }
291  }
292  }
293  for (auto& entry : yaxisData) {
294  string series = entry.first;
295  vector<double> ydata = entry.second;
296  vector<double> xdata = xaxisData.at(series);
297  if (!xdata.size()) {
298  cout << "Series: " + series + " has ydata but no xdata";
299  correct = false;
300  }
301  //Everything else already checked.
302  }
303  return correct;
304  }
305 
306  public:
307  virtual const string getDataStructureRepresentation() const override {
309  check();
310  string xaxis_json = "";
311  for (auto& entry : xaxisData) {
312  string key = entry.first;
313  vector<double> value = entry.second;
314 
315  xaxis_json += OPEN_CURLY + JSONencode("Plot_Name")
316  + COLON + JSONencode( key ) + COMMA +
317  JSONencode("xaxis_data") + COLON + OPEN_BOX;
318  for ( int i = 0; i < value.size() ; i++) {
319  xaxis_json += JSONencode(value[i]) + COMMA;
320  }
321  xaxis_json = xaxis_json.erase(xaxis_json.size() - 1);
322  xaxis_json += CLOSE_BOX + CLOSE_CURLY + COMMA;
323  }
324  xaxis_json = xaxis_json.erase(xaxis_json.length() - 1);
325 
326  string yaxis_json = "";
327  for (auto& entry : yaxisData) {
328  string key = entry.first;
329  vector<double> value = entry.second;
330  yaxis_json += OPEN_CURLY + JSONencode("Plot_Name")
331  + COLON + JSONencode( key) + COMMA +
332  JSONencode("yaxis_data") + COLON + OPEN_BOX;
333  for ( int i = 0; i < value.size() ; i++) {
334  yaxis_json += JSONencode(value[i]) + COMMA;
335  }
336  yaxis_json = yaxis_json.erase(yaxis_json.length() - 1);
337  yaxis_json += CLOSE_BOX + CLOSE_CURLY + COMMA;
338  }
339  yaxis_json = yaxis_json.erase(yaxis_json.length() - 1);
340 
341  string json_str = JSONencode("plot_title") + COLON + JSONencode(getTitle()) + COMMA +
342  JSONencode("subtitle") + COLON + JSONencode(getSubTitle()) + COMMA +
343  JSONencode("xLabel") + COLON + JSONencode(getXLabel()) + COMMA +
344  JSONencode("yLabel") + COLON + JSONencode(getYLabel()) + COMMA +
345  JSONencode("xaxisType") + COLON + JSONencode(logarithmicx) + COMMA +
346  JSONencode("yaxisType") + COLON + JSONencode(logarithmicy) + COMMA +
347  JSONencode("options") + COLON + OPEN_CURLY + JSONencode("mouseTracking") + COLON +
348  JSONencode(mouseTrack) + COMMA + JSONencode("dataLabels") + COLON + JSONencode(dataLabel) + CLOSE_CURLY + COMMA +
349  JSONencode("xaxis_data") + COLON + OPEN_BOX + xaxis_json + CLOSE_BOX + COMMA +
350  JSONencode("yaxis_data") + COLON + OPEN_BOX + yaxis_json + CLOSE_BOX +
351  CLOSE_CURLY;
352 
353  return json_str;
354  }
355  /*
356  virtual void getDataStructureRepresentation(rapidjson::Document& d)
357  const override {
358  using namespace rapidjson;
359  check();
360  Document::AllocatorType& allocator = d.GetAllocator();
361  Value xdata_charts(kArrayType);
362  for (auto& entry : xaxisData) {
363  Value key, value, obj;
364 
365  obj.SetObject();
366  key.SetString("Plot_Name", allocator);
367  value.SetString(entry.first.c_str(), allocator);
368  obj.AddMember(key, value, allocator);
369 
370  Value xaxis_arr(kArrayType);
371  for (auto val : entry.second) {
372  Value v; v.SetDouble(val);
373  xaxis_arr.PushBack(v, allocator);
374  }
375  obj.AddMember("xaxis_data", xaxis_arr, allocator);
376  xdata_charts.PushBack(obj, allocator);
377  }
378  d.AddMember("xaxis_data", xdata_charts, allocator);
379 
380  Value yaxis_arr(kArrayType);
381  for (auto& entry : yaxisData) {
382  Value key, value, obj;
383 
384  obj.SetObject();
385  key.SetString("Plot_Name", allocator);
386  value.SetString(entry.first.c_str(), allocator);
387  obj.AddMember(key, value, allocator);
388 
389  key.SetString("yaxis_data", allocator);
390  Value plot_arr(kArrayType);
391  for (auto val : entry.second) {
392  Value v; v.SetDouble(val);
393  plot_arr.PushBack(v, allocator);
394  }
395  obj.AddMember("yaxis_data", plot_arr, allocator);
396  yaxis_arr.PushBack(obj, allocator);
397  }
398  d.AddMember("yaxis_data", yaxis_arr, allocator);
399  Value v;
400  v.SetString(getTitle().c_str(), allocator);
401  d.AddMember("plot_title", v, allocator);
402  v.SetString(getXLabel().c_str(), allocator);
403  d.AddMember("xLabel", v, allocator);
404  v.SetString(getYLabel().c_str(), allocator);
405  d.AddMember("yLabel", v, allocator);
406  d.AddMember("xaxisType", v.SetBool(logarithmicx), allocator);
407  d.AddMember("yaxisType", v.SetBool(logarithmicy), allocator);
408 
409  Value obj;
410  obj.SetObject();
411  obj.AddMember("mouseTracking", v.SetBool(mouseTrack), allocator);
412  obj.AddMember("dataLabels", v.SetBool(dataLabel), allocator);
413  d.AddMember("options", obj, allocator);
414 
415  StringBuffer sb;
416  Writer<StringBuffer> writer(sb);
417  d.Accept(writer);
418  };
419  */
420  };
421  }
422 }
423 #endif
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:74
Show series of data or functions using a line chart.
Definition: LineChart.h:43
string getSubTitle() const
Subtitle of the plot.
Definition: LineChart.h:156
virtual const string getDataStructureRepresentation() const override
Definition: LineChart.h:307
void toggleSeriesLabel(bool val)
Enables or disables series labels.
Definition: LineChart.h:98
void toggleLogarithmicY(bool val)
Change the Y-axis scale to logarithmic.
Definition: LineChart.h:120
virtual const string getDStype() const override
Get the data type.
Definition: LineChart.h:74
void toggleLogarithmicX(bool val)
Change the X-axis scale to logarithmic.
Definition: LineChart.h:109
void setSubTitle(string s)
Subtitle of the plot.
Definition: LineChart.h:147
string getYLabel() const
Returns the label for the Y-axis.
Definition: LineChart.h:174
vector< double > getYData(string series)
Returns the Y data for a series.
Definition: LineChart.h:244
void setYData(string series, vector< double > ydata)
Changes the Y data for a series.
Definition: LineChart.h:234
string getTitle() const
Title of the plot.
Definition: LineChart.h:138
void setXData(string series, vector< double > xdata)
Changes the X data for a series.
Definition: LineChart.h:214
void setXLabel(string xaxisName)
Change the label for the X-axis.
Definition: LineChart.h:183
LineChart()
Definition: LineChart.h:59
string getXLabel() const
Returns the label for the Y-axis.
Definition: LineChart.h:192
void setYLabel(string yaxisName)
Change the label for the Y-axis.
Definition: LineChart.h:165
void setTitle(string t)
Title of the plot.
Definition: LineChart.h:129
void setDataSeries(string seriesName, vector< double > xdata, vector< double > ydata)
Add a series (or update it)
Definition: LineChart.h:203
void toggleMouseTrack(bool val)
Enables or disables mouse tracking.
Definition: LineChart.h:86
vector< double > getXData(string series)
Returns the X data for a series.
Definition: LineChart.h:224
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
Support for drawing Bar charts.
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:52
const string OPEN_BOX
Definition: DataStructure.h:55
const string COMMA
Definition: DataStructure.h:51
const string OPEN_CURLY
Definition: DataStructure.h:53
const string CLOSE_BOX
Definition: DataStructure.h:56
const string CLOSE_CURLY
Definition: DataStructure.h:54