Bridges-C++  3.4.5-dev1-6-g935685a
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 
46 namespace bridges {
47  namespace datastructure {
48 
49  class BarChart : public DataStructure {
50  private:
51  std::string cLabel;
52  std::string vLabel;
53  std::string title;
54  std::string subTitle;
55  std::string tooltipSuffix;
56  std::string orientation;
57 
58  std::vector<std::pair<string, vector<double >>> seriesData; //name, data
59  std::vector<std::string> categories;
60 
61  public:
63  title = "";
64  subTitle = "";
65  vLabel = "";
66  cLabel = "";
67  tooltipSuffix = "";
68  orientation = "horizontal";
69  }
70 
75  virtual const string getDStype() const override {
76  return "BarChart";
77  }
78 
84  void setTitle(std::string t) {
85  title = t;
86  }
87 
93  std::string getTitle() const {
94  return title;
95  }
96 
102  void setSubTitle(const std::string& s) {
103  subTitle = s;
104  }
105 
111  std::string getSubTitle() const {
112  return subTitle;
113  }
114 
120  void setValueLabel(const std::string& vAxisName) {
121  vLabel = vAxisName;
122  }
123 
129  std::string getValueLabel() const {
130  return vLabel;
131  }
132 
138  void setCategoriesLabel(const std::string& cAxisName) {
139  cLabel = cAxisName;
140  }
141 
147  std::string getCategoriesLabel() const {
148  return cLabel;
149  }
150 
156  void setBarOrientation(const std::string& orient) {
157  if (orient != "horizontal" && orient != "vertical")
158  throw std::invalid_argument("Orientation should be \"horizontal\" or \"vertical\".");
159  orientation = orient;
160  }
161 
167  std::string getBarOrientation() const {
168  return orientation;
169  }
170 
178  void setTooltipSuffix(const std::string& suffix) {
179  tooltipSuffix = suffix;
180  }
181 
187  std::string getTooltipSuffix() const {
188  return tooltipSuffix;
189  }
190 
198  void setCategories(std::vector<std::string> categories) {
199  if (seriesData.size() > 0)
200  throw std::runtime_error ("Can't change categoriess after series have been added.");
201  this->categories = categories;
202  }
203 
216  void addDataSeries(std::string seriesName, std::vector<double> data) {
217  if (data.size() != categories.size())
218  throw std::runtime_error ("The data vector should have the same size as the number of categories.");
219 
220  for (auto& entry : seriesData) {
221  std::string key = entry.first;
222  if (key == seriesName)
223  throw std::runtime_error ("Can't have two series with the same name.");
224  }
225 
226  seriesData.push_back(std::make_pair(seriesName, data));
227  }
228 
232  virtual const std::string getDataStructureRepresentation() const override {
234  std::string bins = "";
235  bins += JSONencode("xAxis") + COLON + OPEN_CURLY + JSONencode("categories") + COLON + OPEN_BOX;
236  for (auto& entry : categories) {
237  bins += JSONencode(entry) + COMMA;
238  }
239  bins = bins.erase(bins.length() - 1);
240  bins += CLOSE_BOX + CLOSE_CURLY;
241 
242  std::string series = "";
243  series += JSONencode("series") + COLON + OPEN_BOX;
244  for (auto& entry : seriesData) {
245  series += OPEN_CURLY;
246  std::string key = entry.first;
247  std::vector<double> value = entry.second;
248 
249  series += JSONencode("name") + COLON + JSONencode(key) + COMMA + JSONencode("data") + COLON + OPEN_BOX;
250  for (int i = 0; i < value.size(); i++) {
251  series += JSONencode(value[i]) + COMMA;
252  }
253  series = series.erase(series.length() - 1);
254  series += CLOSE_BOX + CLOSE_CURLY + COMMA;
255  }
256  series = series.erase(series.length() - 1);
257  series += CLOSE_BOX;
258 
259  std::string json_str = JSONencode("plot_title") + COLON + JSONencode(getTitle()) + COMMA +
260  JSONencode("subtitle") + COLON + JSONencode(getSubTitle()) + COMMA +
261  JSONencode("xLabel") + COLON + JSONencode(getCategoriesLabel()) + COMMA +
262  JSONencode("yLabel") + COLON + JSONencode(getValueLabel()) + COMMA +
263  JSONencode("tooltipSuffix") + COLON + JSONencode(getTooltipSuffix()) + COMMA +
264  JSONencode("alignment") + COLON + JSONencode(getBarOrientation()) + COMMA +
265  JSONencode("xaxis_data") + COLON + OPEN_CURLY + bins + CLOSE_CURLY + COMMA +
266  JSONencode("yaxis_data") + COLON + OPEN_CURLY + series + CLOSE_CURLY + CLOSE_CURLY;
267 
268  return json_str;
269  }
270  };
271  };
272 }
273 #endif
Definition: BarChart.h:49
void setBarOrientation(const std::string &orient)
sets the bar chart orientation
Definition: BarChart.h:156
void setCategories(std::vector< std::string > categories)
set the categories for this bar chart
Definition: BarChart.h:198
BarChart()
Definition: BarChart.h:62
std::string getCategoriesLabel() const
Returns the axis label for the categories.
Definition: BarChart.h:147
std::string getTitle() const
Title of the plot.
Definition: BarChart.h:93
void setTooltipSuffix(const std::string &suffix)
sets the tooltip suffix
Definition: BarChart.h:178
std::string getSubTitle() const
get the subtitle of the plot
Definition: BarChart.h:111
virtual const std::string getDataStructureRepresentation() const override
get the data structure representation as a JSON
Definition: BarChart.h:232
std::string getBarOrientation() const
gets the bar chart orientation
Definition: BarChart.h:167
void addDataSeries(std::string seriesName, std::vector< double > data)
Add a series of data.
Definition: BarChart.h:216
void setTitle(std::string t)
Title of the plot.
Definition: BarChart.h:84
void setValueLabel(const std::string &vAxisName)
Change the label for the value axis.
Definition: BarChart.h:120
std::string getTooltipSuffix() const
gets the tooltip suffix
Definition: BarChart.h:187
std::string getValueLabel() const
Returns the label for the value axis.
Definition: BarChart.h:129
void setCategoriesLabel(const std::string &cAxisName)
Change the label for categoryt axis.
Definition: BarChart.h:138
virtual const string getDStype() const override
Get the data type.
Definition: BarChart.h:75
void setSubTitle(const std::string &s)
set subtitle of the plot
Definition: BarChart.h:102
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:74
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