Bridges-C++ 3.5.0-dev2-1-ge3e57bf
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
11namespace bridges {
12 namespace datastructure {
47 class BarChart : public DataStructure {
48 private:
49 std::string cLabel;
50 std::string vLabel;
51 std::string title;
52 std::string subTitle;
53 std::string tooltipSuffix;
54 std::string orientation;
55
56 std::vector<std::pair<string, vector<double >>> seriesData; //name, data
57 std::vector<std::string> categories;
58
59 public:
64 title = "";
65 subTitle = "";
66 vLabel = "";
67 cLabel = "";
68 tooltipSuffix = "";
69 orientation = "horizontal";
70 }
71
79 virtual const string getDStype() const override {
80 return "BarChart";
81 }
82
88 void setTitle(std::string t) {
89 title = t;
90 }
91
97 std::string getTitle() const {
98 return title;
99 }
100
106 void setSubTitle(const std::string& s) {
107 subTitle = s;
108 }
109
115 std::string getSubTitle() const {
116 return subTitle;
117 }
118
124 void setValueLabel(const std::string& vAxisName) {
125 vLabel = vAxisName;
126 }
127
133 std::string getValueLabel() const {
134 return vLabel;
135 }
136
142 void setCategoriesLabel(const std::string& cAxisName) {
143 cLabel = cAxisName;
144 }
145
151 std::string getCategoriesLabel() const {
152 return cLabel;
153 }
154
160 void setBarOrientation(const std::string& orient) {
161 if (orient != "horizontal" && orient != "vertical")
162 throw std::invalid_argument("Orientation should be \"horizontal\" or \"vertical\".");
163 orientation = orient;
164 }
165
171 std::string getBarOrientation() const {
172 return orientation;
173 }
174
182 void setTooltipSuffix(const std::string& suffix) {
183 tooltipSuffix = suffix;
184 }
185
191 std::string getTooltipSuffix() const {
192 return tooltipSuffix;
193 }
194
202 void setCategories(const std::vector<std::string>& categories) {
203 if (seriesData.size() > 0)
204 throw std::runtime_error ("Can't change categoriess after series have been added.");
205 this->categories = categories;
206 }
207
220 void addDataSeries(std::string seriesName, std::vector<double> data) {
221 if (data.size() != categories.size())
222 throw std::runtime_error ("The data vector should have the same size as the number of categories.");
223
224 for (auto& entry : seriesData) {
225 std::string key = entry.first;
226 if (key == seriesName)
227 throw std::runtime_error ("Can't have two series with the same name.");
228 }
229
230 seriesData.push_back(std::make_pair(seriesName, data));
231 }
232
239 virtual const std::string getDataStructureRepresentation() const override {
241 std::string bins = "";
242 bins += JSONencode("xAxis") + COLON + OPEN_CURLY + JSONencode("categories") + COLON + OPEN_BOX;
243 for (auto& entry : categories) {
244 bins += JSONencode(entry) + COMMA;
245 }
246 bins = bins.erase(bins.length() - 1);
247 bins += CLOSE_BOX + CLOSE_CURLY;
248
249 std::string series = "";
250 series += JSONencode("series") + COLON + OPEN_BOX;
251 for (auto& entry : seriesData) {
252 series += OPEN_CURLY;
253 std::string key = entry.first;
254 std::vector<double> value = entry.second;
255
256 series += JSONencode("name") + COLON + JSONencode(key) + COMMA + JSONencode("data") + COLON + OPEN_BOX;
257 for (int i = 0; i < value.size(); i++) {
258 series += JSONencode(value[i]) + COMMA;
259 }
260 series = series.erase(series.length() - 1);
261 series += CLOSE_BOX + CLOSE_CURLY + COMMA;
262 }
263 series = series.erase(series.length() - 1);
264 series += CLOSE_BOX;
265
266 std::string json_str = JSONencode("plot_title") + COLON + JSONencode(getTitle()) + COMMA +
267 JSONencode("subtitle") + COLON + JSONencode(getSubTitle()) + COMMA +
269 JSONencode("yLabel") + COLON + JSONencode(getValueLabel()) + COMMA +
270 JSONencode("tooltipSuffix") + COLON + JSONencode(getTooltipSuffix()) + COMMA +
271 JSONencode("alignment") + COLON + JSONencode(getBarOrientation()) + COMMA +
272 JSONencode("xaxis_data") + COLON + OPEN_CURLY + bins + CLOSE_CURLY + COMMA +
273 JSONencode("yaxis_data") + COLON + OPEN_CURLY + series + CLOSE_CURLY + CLOSE_CURLY;
274
275 return json_str;
276 }
277 };
278 };
279}
280#endif
Support for drawing Bar charts.
Definition: BarChart.h:47
void setBarOrientation(const std::string &orient)
sets the bar chart orientation
Definition: BarChart.h:160
BarChart()
makes an empty BarChart
Definition: BarChart.h:63
std::string getCategoriesLabel() const
Returns the axis label for the categories.
Definition: BarChart.h:151
std::string getTitle() const
Get title of the plot.
Definition: BarChart.h:97
void setCategories(const std::vector< std::string > &categories)
set the categories for this bar chart
Definition: BarChart.h:202
void setTooltipSuffix(const std::string &suffix)
sets the tooltip suffix
Definition: BarChart.h:182
std::string getSubTitle() const
get the subtitle of the plot
Definition: BarChart.h:115
virtual const std::string getDataStructureRepresentation() const override
get the data structure representation as a JSON
Definition: BarChart.h:239
std::string getBarOrientation() const
gets the bar chart orientation
Definition: BarChart.h:171
void addDataSeries(std::string seriesName, std::vector< double > data)
Add a series of data.
Definition: BarChart.h:220
void setTitle(std::string t)
Set title of the plot.
Definition: BarChart.h:88
void setValueLabel(const std::string &vAxisName)
Set the label for the value axis.
Definition: BarChart.h:124
std::string getTooltipSuffix() const
gets the tooltip suffix
Definition: BarChart.h:191
std::string getValueLabel() const
Returns the label for the value axis.
Definition: BarChart.h:133
void setCategoriesLabel(const std::string &cAxisName)
Change the label for category axis.
Definition: BarChart.h:142
virtual const string getDStype() const override
Get the data type.
Definition: BarChart.h:79
void setSubTitle(const std::string &s)
set subtitle of the plot
Definition: BarChart.h:106
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
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