Bridges-C++  3.4.2
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 
160 
166  void setYLabel(string yaxisName) {
167  yLabel = yaxisName;
168  }
169 
175  string getYLabel() const {
176  return yLabel;
177  }
178 
184  void setXLabel(string xaxisName) {
185  xLabel = xaxisName;
186  }
187 
193  string getXLabel() const {
194  return xLabel;
195  }
196 
197 
205  void setDataSeries(string seriesName, vector<double> xdata, vector<double> ydata) {
206  setXData(seriesName, xdata);
207  setYData(seriesName, ydata);
208  }
209 
210 
217  void setXData(string series, vector<double> xdata) {
218  xaxisData[series] = xdata;
219  }
220 
227  vector<double> getXData(string series) {
228  return xaxisData[series];
229  }
230 
237  void setYData(string series, vector<double> ydata) {
238  yaxisData[series] = ydata;
239  }
240 
247  vector<double> getYData(string series) {
248  return yaxisData[series];
249  }
250 
251  private:
263  bool check() const {
264  bool correct = true;
265  for (auto& entry : xaxisData) {
266  string series = entry.first;
267  vector<double> xdata = entry.second;
268  vector<double> ydata = yaxisData.at(series);
269  if (!ydata.size()) {
270  cout << "Series \"" + series + "\" has xdata but no ydata";
271  correct = false;
272  }
273  if (xdata.size() != ydata.size()) {
274  cout << "Series \"" + series + "\" has xdata and ydata of different sizes";
275  correct = false;
276  }
277  if (logarithmicx) {
278  for (int i = 0; i < xdata.size(); ++i) {
279  if (xdata[i] == 0) {
280  cout << "Xaxis scale is logarithmic but series \"" + series
281  + "\" has xdata[" << i << "] = " << xdata[i] <<
282  " (should be stricly positive)";
283  }
284  }
285  }
286  if (logarithmicy) {
287  for (int i = 0; i < ydata.size(); ++i) {
288  if (ydata[i] == 0) {
289  cout << "Yaxis scale is logarithmic but series \"" +
290  series + "\" has ydata[" << i << "] = " << ydata[i] <<
291  " (should be stricly positive)";
292  }
293  }
294  }
295  }
296  for (auto& entry : yaxisData) {
297  string series = entry.first;
298  vector<double> ydata = entry.second;
299  vector<double> xdata = xaxisData.at(series);
300  if (!xdata.size()) {
301  cout << "Series: " + series + " has ydata but no xdata";
302  correct = false;
303  }
304  //Everything else already checked.
305  }
306  return correct;
307  }
308 
309  public:
310  virtual const string getDataStructureRepresentation() const override {
312  check();
313  string xaxis_json = "";
314  for (auto& entry : xaxisData) {
315  string key = entry.first;
316  vector<double> value = entry.second;
317 
318  xaxis_json += OPEN_CURLY + JSONencode("Plot_Name")
319  + COLON + JSONencode( key ) + COMMA +
320  JSONencode("xaxis_data") + COLON + OPEN_BOX;
321  for ( int i = 0; i < value.size() ; i++) {
322  xaxis_json += JSONencode(value[i]) + COMMA;
323  }
324  xaxis_json = xaxis_json.erase(xaxis_json.size() - 1);
325  xaxis_json += CLOSE_BOX + CLOSE_CURLY + COMMA;
326  }
327  xaxis_json = xaxis_json.erase(xaxis_json.length() - 1);
328 
329  string yaxis_json = "";
330  for (auto& entry : yaxisData) {
331  string key = entry.first;
332  vector<double> value = entry.second;
333  yaxis_json += OPEN_CURLY + JSONencode("Plot_Name")
334  + COLON + JSONencode( key) + COMMA +
335  JSONencode("yaxis_data") + COLON + OPEN_BOX;
336  for ( int i = 0; i < value.size() ; i++) {
337  yaxis_json += JSONencode(value[i]) + COMMA;
338  }
339  yaxis_json = yaxis_json.erase(yaxis_json.length() - 1);
340  yaxis_json += CLOSE_BOX + CLOSE_CURLY + COMMA;
341  }
342  yaxis_json = yaxis_json.erase(yaxis_json.length() - 1);
343 
344 
345  string json_str = JSONencode("plot_title") + COLON + JSONencode(getTitle()) + COMMA +
346  JSONencode("subtitle") + COLON + JSONencode(getSubTitle()) + COMMA +
347  JSONencode("xLabel") + COLON + JSONencode(getXLabel()) + COMMA +
348  JSONencode("yLabel") + COLON + JSONencode(getYLabel()) + COMMA +
349  JSONencode("xaxisType") + COLON + JSONencode(logarithmicx) + COMMA +
350  JSONencode("yaxisType") + COLON + JSONencode(logarithmicy) + COMMA +
351  JSONencode("options") + COLON + OPEN_CURLY + JSONencode("mouseTracking") + COLON +
352  JSONencode(mouseTrack) + COMMA + JSONencode("dataLabels") + COLON + JSONencode(dataLabel) + CLOSE_CURLY + COMMA +
353  JSONencode("xaxis_data") + COLON + OPEN_BOX + xaxis_json + CLOSE_BOX + COMMA +
354  JSONencode("yaxis_data") + COLON + OPEN_BOX + yaxis_json + CLOSE_BOX +
355  CLOSE_CURLY;
356 
357  return json_str;
358  }
359 
360  };
361  }
362 }
363 #endif
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
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:310
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:175
vector< double > getYData(string series)
Returns the Y data for a series.
Definition: LineChart.h:247
void setYData(string series, vector< double > ydata)
Changes the Y data for a series.
Definition: LineChart.h:237
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:217
void setXLabel(string xaxisName)
Change the label for the X-axis.
Definition: LineChart.h:184
LineChart()
Definition: LineChart.h:59
string getXLabel() const
Returns the label for the Y-axis.
Definition: LineChart.h:193
void setYLabel(string yaxisName)
Change the label for the Y-axis.
Definition: LineChart.h:166
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:205
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:227
std::string JSONencode(const T &d)
Definition: JSONutil.h:37
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:51
const string OPEN_BOX
Definition: DataStructure.h:54
const string COMMA
Definition: DataStructure.h:50
const string OPEN_CURLY
Definition: DataStructure.h:52
const string CLOSE_BOX
Definition: DataStructure.h:55
const string CLOSE_CURLY
Definition: DataStructure.h:53