Bridges-C++ 3.5.1
Bridges(C++ API)
Loading...
Searching...
No Matches
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
10namespace bridges {
11 namespace datastructure {
46 class LineChart : public DataStructure {
47
48 private:
49 string plotTitle;
50 string plotSubtitle;
51 string xLabel;
52 string yLabel;
53 bool mouseTrack;
54 bool dataLabel;
55 bool logarithmicx;
56 bool logarithmicy;
57
58 unordered_map<string, vector<double >> xaxisData;
59 unordered_map<string, vector<double >> yaxisData;
60 unordered_map<string, int> line_width; // indexed by data series name
61
62 public:
64 plotTitle = "";
65 plotSubtitle = "";
66 yLabel = "";
67 xLabel = "";
68 mouseTrack = false;
69 dataLabel = true;
70 logarithmicx = false;
71 logarithmicy = false;
72 }
73
78 virtual const string getDStype() const override {
79 return "LineChart";
80 }
81
90 void toggleMouseTrack(bool val) {
91 mouseTrack = val;
92 }
93
102 void toggleSeriesLabel(bool val) {
103 dataLabel = val;
104 }
105
113 void toggleLogarithmicX(bool val) {
114 logarithmicx = val;
115 }
116
124 void toggleLogarithmicY(bool val) {
125 logarithmicy = val;
126 }
127
133 void setTitle(string t) {
134 plotTitle = t;
135 }
136
142 string getTitle() const {
143 return plotTitle;
144 }
145
151 void setSubTitle(string s) {
152 plotSubtitle = s;
153 }
154
160 string getSubTitle() const {
161 return plotSubtitle;
162 }
163
169 void setYLabel(string yaxisName) {
170 yLabel = yaxisName;
171 }
172
178 string getYLabel() const {
179 return yLabel;
180 }
181
187 void setXLabel(string xaxisName) {
188 xLabel = xaxisName;
189 }
190
196 string getXLabel() const {
197 return xLabel;
198 }
199
205 void setLineWidth(string series, int w) {
206 line_width[series] = w;
207 }
208
214 int getLineWidth(string series) {
215 return line_width[series];
216 }
217
225 void setDataSeries(string seriesName, vector<double> xdata, vector<double> ydata) {
226 setXData(seriesName, xdata);
227 setYData(seriesName, ydata);
228 line_width[seriesName] = 1; // default is lines on at size of 1 pixel
229 }
230
237 void setXData(string series, vector<double> xdata) {
238 xaxisData[series] = xdata;
239 }
240
247 vector<double> getXData(string series) {
248 return xaxisData[series];
249 }
250
257 void setYData(string series, vector<double> ydata) {
258 yaxisData[series] = ydata;
259 }
260
267 vector<double> getYData(string series) {
268 return yaxisData[series];
269 }
270
271 private:
283 bool check() const {
284 bool correct = true;
285 for (auto& entry : xaxisData) {
286 string series = entry.first;
287 vector<double> xdata = entry.second;
288 vector<double> ydata = yaxisData.at(series);
289 if (!ydata.size()) {
290 cout << "Series \"" + series + "\" has xdata but no ydata";
291 correct = false;
292 }
293 if (xdata.size() != ydata.size()) {
294 cout << "Series \"" + series + "\" has xdata and ydata of different sizes";
295 correct = false;
296 }
297 if (logarithmicx) {
298 for (int i = 0; i < xdata.size(); ++i) {
299 if (xdata[i] == 0) {
300 cout << "Xaxis scale is logarithmic but series \"" + series
301 + "\" has xdata[" << i << "] = " << xdata[i] <<
302 " (should be stricly positive)";
303 }
304 }
305 }
306 if (logarithmicy) {
307 for (int i = 0; i < ydata.size(); ++i) {
308 if (ydata[i] == 0) {
309 cout << "Yaxis scale is logarithmic but series \"" +
310 series + "\" has ydata[" << i << "] = " << ydata[i] <<
311 " (should be stricly positive)";
312 }
313 }
314 }
315 }
316 for (auto& entry : yaxisData) {
317 string series = entry.first;
318 vector<double> ydata = entry.second;
319 vector<double> xdata = xaxisData.at(series);
320 if (!xdata.size()) {
321 cout << "Series: " + series + " has ydata but no xdata";
322 correct = false;
323 }
324 //Everything else already checked.
325 }
326 return correct;
327 }
328
329 public:
330 virtual const string getDataStructureRepresentation() const override {
332 check();
333 string xaxis_json = "";
334 for (auto& entry : xaxisData) {
335 string key = entry.first;
336 vector<double> value = entry.second;
337
338 xaxis_json += OPEN_CURLY + JSONencode("Plot_Name")
339 + COLON + JSONencode( key ) + COMMA +
340 JSONencode("xaxis_data") + COLON + OPEN_BOX;
341 for ( int i = 0; i < value.size() ; i++) {
342 xaxis_json += JSONencode(value[i]) + COMMA;
343 }
344 xaxis_json = xaxis_json.erase(xaxis_json.size() - 1);
345 xaxis_json += CLOSE_BOX + CLOSE_CURLY + COMMA;
346 }
347 xaxis_json = xaxis_json.erase(xaxis_json.length() - 1);
348
349 string yaxis_json = "";
350 for (auto& entry : yaxisData) {
351 string key = entry.first;
352 vector<double> value = entry.second;
353 yaxis_json += OPEN_CURLY + JSONencode("Plot_Name")
354 + COLON + JSONencode( key) + COMMA +
355 JSONencode("yaxis_data") + COLON + OPEN_BOX;
356 for ( int i = 0; i < value.size() ; i++) {
357 yaxis_json += JSONencode(value[i]) + COMMA;
358 }
359 yaxis_json = yaxis_json.erase(yaxis_json.length() - 1);
360 yaxis_json += CLOSE_BOX + CLOSE_CURLY + COMMA;
361 }
362 yaxis_json = yaxis_json.erase(yaxis_json.length() - 1);
363
364 // get line width of each series into JSON
365 string lw_json = JSONencode("linewidth") + COLON + OPEN_CURLY;
366 for (auto k: line_width) {
367 lw_json += QUOTE + k.first + QUOTE + COLON + std::to_string(k.second) + COMMA;
368 }
369 lw_json = lw_json.erase(lw_json.length()-1);
370 lw_json += CLOSE_CURLY;
371
372 string json_str =
373 JSONencode("plot_title") + COLON + JSONencode(getTitle()) + COMMA +
374 JSONencode("subtitle") + COLON + JSONencode(getSubTitle()) + COMMA +
375 JSONencode("xLabel") + COLON + JSONencode(getXLabel()) + COMMA +
376 JSONencode("yLabel") + COLON + JSONencode(getYLabel()) + COMMA +
377 JSONencode("xaxisType") + COLON + JSONencode(logarithmicx) + COMMA +
378 JSONencode("yaxisType") + COLON + JSONencode(logarithmicy) + COMMA +
379 JSONencode("options") + COLON + OPEN_CURLY + JSONencode("mouseTracking") + COLON +
380 JSONencode(mouseTrack) + COMMA + JSONencode("dataLabels") + COLON + JSONencode(dataLabel) + CLOSE_CURLY + COMMA +
381 JSONencode("xaxis_data") + COLON + OPEN_BOX + xaxis_json +
382 CLOSE_BOX + COMMA +
383 JSONencode("yaxis_data") + COLON + OPEN_BOX + yaxis_json +
384 CLOSE_BOX + COMMA +
385 lw_json +
387
388 return json_str;
389 }
390 };
391 }
392}
393#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:46
string getSubTitle() const
Subtitle of the plot.
Definition LineChart.h:160
int getLineWidth(string series)
Title of the plot.
Definition LineChart.h:214
virtual const string getDataStructureRepresentation() const override
Definition LineChart.h:330
void toggleSeriesLabel(bool val)
Enables or disables series labels.
Definition LineChart.h:102
void toggleLogarithmicY(bool val)
Change the Y-axis scale to logarithmic.
Definition LineChart.h:124
virtual const string getDStype() const override
Get the data type.
Definition LineChart.h:78
void toggleLogarithmicX(bool val)
Change the X-axis scale to logarithmic.
Definition LineChart.h:113
void setSubTitle(string s)
Subtitle of the plot.
Definition LineChart.h:151
string getYLabel() const
Returns the label for the Y-axis.
Definition LineChart.h:178
void setLineWidth(string series, int w)
width of line segments in the plot
Definition LineChart.h:205
void setYData(string series, vector< double > ydata)
Changes the Y data for a series.
Definition LineChart.h:257
string getTitle() const
Title of the plot.
Definition LineChart.h:142
void setXData(string series, vector< double > xdata)
Changes the X data for a series.
Definition LineChart.h:237
void setXLabel(string xaxisName)
Change the label for the X-axis.
Definition LineChart.h:187
LineChart()
Definition LineChart.h:63
string getXLabel() const
Returns the label for the Y-axis.
Definition LineChart.h:196
void setYLabel(string yaxisName)
Change the label for the Y-axis.
Definition LineChart.h:169
void setTitle(string t)
Title of the plot.
Definition LineChart.h:133
void setDataSeries(string seriesName, vector< double > xdata, vector< double > ydata)
Add a series (or update it)
Definition LineChart.h:225
vector< double > getYData(string series)
Returns the Y data for a series.
Definition LineChart.h:267
vector< double > getXData(string series)
Returns the X data for a series.
Definition LineChart.h:247
void toggleMouseTrack(bool val)
Enables or disables mouse tracking.
Definition LineChart.h:90
std::string JSONencode(const T &d)
Definition JSONutil.h:38
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: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
const string QUOTE
Definition DataStructure.h:50