Bridges-C++  3.4.4
Bridges(C++ API)
BarChart.h
Go to the documentation of this file.
1 #ifndef BAR_CHART_H
2 #define BAR_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 {
12 
13  class BarChart : public DataStructure {
14  private:
15  std::string xLabel;
16  std::string yLabel;
17  std::string plotTitle;
18  std::string plotSubTitle;
19  std::string tooltipSuffix;
20  std::string alignment;
21 
22  unordered_map<string, vector<double>> seriesData;
23  std::vector<std::string> seriesBins;
24 
25  public:
27  plotTitle = "";
28  plotSubTitle = "";
29  yLabel = "";
30  xLabel = "";
31  tooltipSuffix = "";
32  alignment = "horizontal";
33  }
34 
39  virtual const string getDStype() const override {
40  return "BarChart";
41  }
42 
48  void setTitle(std::string t) {
49  plotTitle = t;
50  }
51 
57  std::string getTitle() const {
58  return plotTitle;
59  }
60 
66  void setSubTitle(std::string s) {
67  plotSubTitle = s;
68  }
69 
75  std::string getSubTitle() const {
76  return plotSubTitle;
77  }
78 
84  void setSeriesLabel(std::string yaxisName) {
85  yLabel = yaxisName;
86  }
87 
93  std::string getSeriesLabel() const {
94  return yLabel;
95  }
96 
102  void setBinsLabel(std::string xaxisName) {
103  xLabel = xaxisName;
104  }
105 
111  std::string getBinsLabel() const {
112  return xLabel;
113  }
114 
115  void setBarAlignment(std::string align) {
116  alignment = align;
117  }
118 
119  std::string getBarAlignment() const {
120  return alignment;
121  }
122 
123  void setTooltipSuffix(std::string suffix) {
124  tooltipSuffix = suffix;
125  }
126 
127  std::string getTooltipSuffix() const {
128  return tooltipSuffix;
129  }
130 
131  void setSeriesBins(std::vector<std::string> bins) {
132  seriesBins = bins;
133  }
134 
142  void addDataSeries(std::string seriesName, std::vector<double> data) {
143  seriesData[seriesName] = data;
144  }
145 
146  virtual const std::string getDataStructureRepresentation() const override {
148  std::string bins = "";
149  bins += JSONencode("xAxis") + COLON + OPEN_CURLY + JSONencode("categories") + COLON + OPEN_BOX;
150  for (auto& entry : seriesBins) {
151  bins += JSONencode(entry) + COMMA;
152  }
153  bins = bins.erase(bins.length() - 1);
154  bins += CLOSE_BOX + CLOSE_CURLY;
155 
156  std::string series = "";
157  series += JSONencode("series") + COLON + OPEN_BOX;
158  for (auto& entry : seriesData) {
159  series += OPEN_CURLY;
160  std::string key = entry.first;
161  std::vector<double> value = entry.second;
162 
163  series += JSONencode("name") + COLON + JSONencode(key) + COMMA + JSONencode("data") + COLON + OPEN_BOX;
164  for (int i = 0; i < value.size(); i++) {
165  series += JSONencode(value[i]) + COMMA;
166  }
167  series = series.erase(series.length() - 1);
168  series += CLOSE_BOX + CLOSE_CURLY + COMMA;
169  }
170  series = series.erase(series.length() - 1);
171  series += CLOSE_BOX;
172 
173  std::string json_str = JSONencode("plot_title") + COLON + JSONencode(getTitle()) + COMMA +
174  JSONencode("subtitle") + COLON + JSONencode(getSubTitle()) + COMMA +
175  JSONencode("xLabel") + COLON + JSONencode(getBinsLabel()) + COMMA +
176  JSONencode("yLabel") + COLON + JSONencode(getSeriesLabel()) + COMMA +
177  JSONencode("tooltipSuffix") + COLON + JSONencode(getTooltipSuffix()) + COMMA +
178  JSONencode("alignment") + COLON + JSONencode(getBarAlignment()) + COMMA +
179  JSONencode("xaxis_data") + COLON + OPEN_CURLY + bins + CLOSE_CURLY + COMMA +
180  JSONencode("yaxis_data") + COLON + OPEN_CURLY + series + CLOSE_CURLY + CLOSE_CURLY;
181 
182  return json_str;
183  }
184  };
185  }
186 
187 }
188 #endif
Definition: BarChart.h:13
void setBinsLabel(std::string xaxisName)
Change the label for the X-axis.
Definition: BarChart.h:102
BarChart()
Definition: BarChart.h:26
std::string getBinsLabel() const
Returns the label for the Y-axis.
Definition: BarChart.h:111
std::string getTitle() const
Title of the plot.
Definition: BarChart.h:57
std::string getBarAlignment() const
Definition: BarChart.h:119
void setSubTitle(std::string s)
Subtitle of the plot.
Definition: BarChart.h:66
void setSeriesLabel(std::string yaxisName)
Change the label for the Y-axis.
Definition: BarChart.h:84
void setSeriesBins(std::vector< std::string > bins)
Definition: BarChart.h:131
std::string getSubTitle() const
Subtitle of the plot.
Definition: BarChart.h:75
void setTooltipSuffix(std::string suffix)
Definition: BarChart.h:123
std::string getSeriesLabel() const
Returns the label for the Y-axis.
Definition: BarChart.h:93
virtual const std::string getDataStructureRepresentation() const override
Definition: BarChart.h:146
void addDataSeries(std::string seriesName, std::vector< double > data)
Add a series (or update it)
Definition: BarChart.h:142
void setTitle(std::string t)
Title of the plot.
Definition: BarChart.h:48
std::string getTooltipSuffix() const
Definition: BarChart.h:127
void setBarAlignment(std::string align)
Definition: BarChart.h:115
virtual const string getDStype() const override
Get the data type.
Definition: BarChart.h:39
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
std::string JSONencode(const T &d)
Definition: JSONutil.h:36
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