-
Notifications
You must be signed in to change notification settings - Fork 0
/
barchart.h
297 lines (236 loc) · 7.86 KB
/
barchart.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// barchart.h
// TO DO: add header comment here. Also add function header comments below.
#include <iostream>
#include <algorithm>
#include <map>
#include "myrandom.h" // used in autograder, do not remove
#include "bar.h"
using namespace std;
// Constants used for bar chart animation. You will primarily
// use these in barchartanimate.h, but you might find some useful here.
//const string BOX = "\u29C8";
const string BOX = "◇"; //"△";//"⛤" ⚛"; //"۞";
const string CLEARCONSOLE = "\033[2J";
/* Color codes for Mimir (light mode)
const string RED("\033[1;36m");
const string PURPLE("\033[1;32m");
const string BLUE("\033[1;33m");
const string CYAN("\033[1;31m");
const string GREEN("\033[1;35m");
const string GOLD("\033[1;34m");
const string BLACK("\033[1;37m");
const string RESET("\033[0m");
const vector<string> COLORS = {RED, PURPLE, BLUE, CYAN, GREEN, GOLD, BLACK};*/
// Color codes for Codio (light/dark mode)
const string CYAN("\033[1;36m");
const string GREEN("\033[1;32m");
const string GOLD("\033[1;33m");
const string RED("\033[1;31m");
const string PURPLE("\033[1;35m");
const string BLUE("\033[1;34m");
const string WHITE("\033[1;37m");
const string RESET("\033[0m");
const vector<string> COLORS = {CYAN, GREEN, GOLD, RED, PURPLE, BLUE, WHITE, RESET};
//
// BarChart
//
class BarChart {
private:
//
// Private member variables for the abstraction.
// This implementation uses a low-level C array, bars, to store a list of
// Bars. As a result, you must also keep track of the number of elements
// stored (size) and the capacity of the array (capacity). This is not a
// dynamic array, so it does not expand.
//
Bar* bars; // pointer to a C-style array
int capacity;
int size;
string frame;
public:
// default constructor:
BarChart() {
this->bars = nullptr; // points to nothing
this->capacity = 0;
this->size = 0;
this->frame = "";
}
// parameterized constructor:
// Parameter passed in determines memory allocated for bars.
BarChart(int n) {
this->bars = new Bar[n];
this->capacity = n;
this->size = 0;
this->frame = "";
}
//
// copy constructor:
//
// Called automatically by C++ to create an BarChart that contains
// a copy of an existing BarChart. Example: this occurs when passing
// BarChart as a parameter by value.
//
BarChart(const BarChart& other) { // for making a copy of an existing BC in a nonExisting one
this->bars = new Bar[other.capacity]; // create array of Bars with same capacity
this->capacity = other.capacity;
this->size = other.size;
this->frame = other.frame;
for (int g = 0; g < other.size; g++) { // Copies content
this->bars[g] = other.bars[g];
}
}
//
// copy operator=
//
// Called when you assign one BarChart into another, i.e. this = other;
//
BarChart& operator=(const BarChart& other) {
BarChart bc;
if (this == &other) {
return *this;
}
delete[] this->bars;
this->bars = new Bar[other.capacity]; // make new space
for (int h = 0; h < other.size; h++) { // fill in with data
this->bars[h] = other.bars[h];
}
this->size = other.size;
this->capacity = other.capacity;
this->frame = other.frame;
return *this;
}
// clear
// frees memory and resets all private member variables to default values.
void clear() {
delete[] this->bars;
this->bars = nullptr;
this->size = 0;
this->capacity = 0;
this->frame = "0";
}
//
// destructor:
//
// Called automatically by C++ to free the memory associated by the
// BarChart.
//
virtual ~BarChart() {
if (bars != nullptr) {
delete[] this->bars;
}
}
// setFrame
void setFrame(string frame) {
this->frame = frame;
}
// getFrame()
// Returns the frame of the BarChart oboject.
string getFrame() {
return this->frame;
}
// addBar
// adds a Bar to the BarChart.
// returns true if successful
// returns false if there is no room
bool addBar(string name, int value, string category) {
if (this->size < this->capacity) {
Bar theNewBar(name, value, category);
bars[this->size] = theNewBar;
this->size++;
return true;
}
return false;
}
// getSize()
// Returns the size (number of bars) of the BarChart object.
int getSize() {
return this->size;
}
// operator[]
// Returns Bar element in BarChart.
// This gives public access to Bars stored in the Barchart.
// If i is out of bounds, throw an out_of_range error message:
// "BarChart: i out of bounds"
Bar& operator[](int i) {
if (i > this->size || i < 0) {
throw out_of_range("BarChart: i out of bounds");
}
return this->bars[i];
}
// dump
// Used for printing the BarChart object.
// Recommended for debugging purposes. output is any stream (cout,
// file stream, or string stream).
// Format:
// "frame: 1
// aname 5 category1
// bname 4 category2
// cname 3 category3" <-newline here
void dump(ostream &output) {
// first sort the list of bars in each chart
for (int b = 0; b < size; b++) { // for each bar
for (int n = b; n < size; n++) { // compare it to the rest
if (bars[b].getValue() < bars[n].getValue()) { // if bigger bar found // switch bars
Bar tempBar;
tempBar = bars[b];
bars[b] = bars[n];
bars[n] = tempBar;
}
}
}
output << "frame: " << frame << endl;
for (int j= 0; j < size; j++) {
output << bars[j].getName() << " ";
output << bars[j].getValue() << " ";
output << bars[j].getCategory() << endl;
}
}
string findColor(string& theKey, map<string, string> &colorMap) {
auto itter = colorMap.find(theKey);
if (itter == colorMap.end()) {
//not found
return WHITE;
}
return itter->second;
}
// graph
// Used for printing out the bar.
// output is any stream (cout, file stream, string stream)
// colorMap maps category -> color
// top is number of bars you'd like plotted on each frame (top 10? top 12?)
void graph(ostream &output, map<string, string> &colorMap, int top) {
int lenMax = 60; // this is number of BOXs that should be printed
Bar biggestCity;
string theColor = "";
for (int b = 0; b < top; b++) { // for each bar
for (int n = b; n < top; n++) { // compare it to the rest
if (bars[b].getValue() < bars[n].getValue()) { // if bigger bar found // switch bars
Bar tempBar;
tempBar = bars[b];
bars[b] = bars[n];
bars[n] = tempBar;
}
}
biggestCity = bars[0];
}
for (int k = 0; k < top; k++) { // for each bar in bar chart
// find the number of boxes for it
double val1 = bars[k].getValue() * 1.0;
double val2 = biggestCity.getValue() * 1.0;
double num = (val1 / val2);
num = num * lenMax;
num = floor(num);
//Get color of bar
string theCategory = bars[k].getCategory();
// look for category in the map then take its color and set it to this one
theColor = findColor(theCategory, colorMap);
//string theReset = findColor(reset, colorMap);
string barstr = "";
for (int i = 0; i < num; i++) {
barstr += BOX;
}
output << theColor << barstr << " " << bars[k].getName() << " " << bars[k].getValue() << RESET << endl;
num = 0.0;
}
}
};