-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram.cpp
More file actions
321 lines (260 loc) · 11.7 KB
/
Copy pathhistogram.cpp
File metadata and controls
321 lines (260 loc) · 11.7 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "histogram.h"
#include <string>
#include <gd.h>
#include <nlohmann/json.hpp>
#include <vector>
#include <cmath>
#include <map>
#include <gdfonts.h>
#include <curl/curl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctime>
#include <iomanip>
using namespace std;
//Auteur : Raphael De Oliveira
vector<string> split_into_lines(const string& s, int max_width, gdFontPtr font) {
vector<string> lines;
//Calcule le nombre maximum de caractères par ligne
int max_chars = max_width / font->w;
int start = 0;
while (start < s.size()) {
int end = start + max_chars;
//Si nous sommes à la fin de la chaîne ou si nous trouvons un espace pour diviser
if (end >= s.size() || s[end] == ' ' || end - start < max_chars) {
lines.push_back(s.substr(start, end - start));
start = end + 1; // +1 pour sauter l'espace
} else {
//Sinon, remonte pour trouver le dernier espace
while (end > start && s[end] != ' ') {
--end;
}
if (end == start) { //si aucun espace n'a été trouvé, on divise le mot
end = start + max_chars;
lines.push_back(s.substr(start, end - start));
start = end;
} else {
lines.push_back(s.substr(start, end - start));
start = end + 1; //+1 pour sauter l'espace
}
}
}
return lines;
}
// Fonction pour créer un histogramme
void Histogram::createHistogram(const string& filename) {
vector<string> noms;
vector<int> dispo;
vector<int> max;
// Étape 1: Lire le fichier 'disponibilite_parkings.json'
ifstream file("disponibilite_parkings.json");
if (file.is_open()) {
json j;
file >> j;
for (const auto& parking : j) {
if(parking.find("fields") != parking.end()) {
auto fields = parking["fields"];
if(fields.find("libelle") != fields.end() &&
fields.find("dispo") != fields.end() &&
fields.find("max") != fields.end()) {
string nom = fields["libelle"].get<string>();
int dispoValue = fields["dispo"].get<int>();
int maxValue = fields["max"].get<int>();
noms.push_back(nom);
dispo.push_back(dispoValue);
max.push_back(maxValue);
} else {
cerr << "Objet parking malformé: " << parking << endl;
}
}
}
file.close();
}
const int image_width = 1600;
const int image_height = 600;
const int bar_width = image_width / noms.size();
const int MAX_BAR_HEIGHT = image_height - 20;
gdImagePtr im = gdImageCreateTrueColor(image_width, image_height);
int white = gdImageColorAllocate(im, 255, 255, 255);
gdImageFill(im, 0, 0, white);
int black = gdImageColorAllocate(im, 0, 0, 0); //Couleur pour le texte
//Dessin des barres de l'histogramme
for (size_t i = 0; i < dispo.size(); i++) {
int pourcentage = (100 * dispo[i]) / max[i];
int bar_height = (image_height * pourcentage) / 100;
const int RELIEF_OFFSET = 10;
bar_height = min(bar_height, MAX_BAR_HEIGHT);
int rouge = rand() % 255;
int vert = rand() % 255;
int bleu = rand() % 255;
int color = gdImageColorAllocate(im, rouge, vert, bleu);
int bottom_space = MAX_BAR_HEIGHT - bar_height;
//Assombrir légèrement la couleur principale
int darkenedRed = std::max(0, rouge - 30);
int darkenedGreen = std::max(0, vert - 30);
int darkenedBlue = std::max(0, bleu - 30);
int color_relief = gdImageColorAllocate(im, darkenedRed, darkenedGreen, darkenedBlue);
if (pourcentage >= 90) { // Si le pourcentage du parking est supérieur ou égal à 90%
// Dessine le nom du parking complet sur une ligne, suivi du pourcentage
string label = noms[i] + " (" + to_string(pourcentage) + "%)";
gdImageString(im, gdFontGetSmall(), i * bar_width + (bar_width - gdFontGetSmall()->w * label.length()) / 2, image_height - bar_height - 15, (unsigned char*)label.c_str(), black);
} else {
//Dessine le pourcentage au-dessus de la barre
string percentageLabel = "("+to_string(pourcentage) + "%)";
gdImageString(im, gdFontGetSmall(), i * bar_width + (bar_width - gdFontGetSmall()->w * percentageLabel.length()) / 2, image_height - bar_height - 15, (unsigned char*)percentageLabel.c_str(), black);
//Dessine le nom du parking en cascade en utilisant split_into_lines
auto lines = split_into_lines(noms[i], bar_width, gdFontGetSmall());
std::reverse(lines.begin(), lines.end());
int offsetY = 0;
for (const auto& line : lines) {
gdImageString(im, gdFontGetSmall(), i * bar_width + (bar_width - gdFontGetSmall()->w * line.length()) / 2, image_height - bar_height - 30 - offsetY, (unsigned char*)line.c_str(), black);
offsetY += gdFontGetSmall()->h + 2; // Ajuste l'espacement entre les lignes
}
}
// Dessine le rectangle principal
gdImageFilledRectangle(im, i * bar_width, image_height - bar_height, (i + 1) * bar_width - RELIEF_OFFSET, image_height, color);
//On dessine le relief 3D
for (int offset = 1; offset <= RELIEF_OFFSET; offset++) {
gdImageFilledRectangle(im, (i + 1) * bar_width - offset, image_height - bar_height - offset + RELIEF_OFFSET, (i + 1) * bar_width - offset + 1, image_height - offset + RELIEF_OFFSET, color_relief);
}
}
FILE* out = fopen(filename.c_str(), "wb");
gdImagePng(im, out);
fclose(out);
gdImageDestroy(im);
}
void Histogram::createEvolutionHistogramFromJSON(const std::string& filename, const std::string& jsonFilePath) {
json data;
std::ifstream jsonFile(jsonFilePath);
jsonFile >> data;
jsonFile.close();
std::vector<std::string> dates;
std::vector<int> availabilityHistory;
std::vector<int> maxValues;
for (const auto& item : data) {
if (item.contains("dispo") && item.contains("max") && item.contains("date")) {
int availability = item["dispo"].get<int>();
int maxCapacity = item["max"].get<int>();
std::string date = item["date"].get<std::string>();
dates.push_back(date);
availabilityHistory.push_back(availability);
maxValues.push_back(maxCapacity);
}
}
const int image_width = 1600;
const int image_height = 600;
const int bar_width = image_width / availabilityHistory.size();
const int MAX_BAR_HEIGHT = image_height - 20;
gdImagePtr im = gdImageCreateTrueColor(image_width, image_height);
int white = gdImageColorAllocate(im, 255, 255, 255);
gdImageFill(im, 0, 0, white);
int black = gdImageColorAllocate(im, 0, 0, 0); // Couleur pour le texte
gdImageString(im, gdFontGetSmall(), 10, 10, (unsigned char*)filename.c_str(), black);
for (size_t i = 0; i < availabilityHistory.size(); i++) {
int availability = availabilityHistory[i];
int maxCapacity = maxValues[i];
int pourcentage = (100 * availability) / maxCapacity; // Calcul du pourcentage
int bar_height = (image_height * pourcentage) / 100;
bar_height = min(bar_height, MAX_BAR_HEIGHT);
int rouge = rand() % 255;
int vert = rand() % 255;
int bleu = rand() % 255;
int color = gdImageColorAllocate(im, rouge, vert, bleu);
gdImageFilledRectangle(im, i * bar_width, image_height - bar_height, (i + 1) * bar_width, image_height, color);
string label = to_string(pourcentage) + "%";
gdImageString(im, gdFontGetSmall(), i * bar_width + 5, image_height - bar_height - 15, (unsigned char*)label.c_str(), black);
string date = dates[i];
size_t pos = date.find(' ');
if (pos != string::npos) {
string dateStr = date.substr(0, pos);
string heureStr = date.substr(pos + 1);
int textOffset = (bar_width - gdFontGetSmall()->w * (dateStr.length() + heureStr.length())) / 2;
gdImageString(im, gdFontGetSmall(), i * bar_width + textOffset, image_height - 30, (unsigned char*)dateStr.c_str(), black);
gdImageString(im, gdFontGetSmall(), i * bar_width + textOffset, image_height - 15, (unsigned char*)heureStr.c_str(), black);
}
}
FILE* out = fopen(filename.c_str(), "wb");
gdImagePng(im, out);
fclose(out);
gdImageDestroy(im);
}
void Histogram::showGeneratedImagesHTML() const {
//Chemin du répertoire contenant les images .png générées
const std::string path = "Images_PNG/";
//Nom du fichier HTML à créer
const std::string htmlFilename = "Images_histograms.html";
//Ouvre le fichier pour écrire le HTML
ofstream htmlFile(htmlFilename);
if (!htmlFile.is_open()) {
cerr << "Erreur lors de la création du fichier HTML." << endl;
return;
}
//Écris l'en-tête du fichier HTML
htmlFile << "<!DOCTYPE html>\n<html>\n<head>\n";
htmlFile << "<title>Histogrammes des parkings</title>\n";
htmlFile << "</head>\n<body>\n";
//On Ouvre le dossier et lisez chaque fichier
DIR *dir;
struct dirent *ent;
if ((dir = opendir(path.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
std::string filename = ent->d_name;
//On Vérifi si le fichier est une image .png
if (filename.size() > 4 && filename.substr(filename.size() - 4) == ".png") {
// Ajoute chaque image au fichier HTML
htmlFile << "<img src=\"" << path << filename << "\" alt=\"" << filename << "\" style=\"width:100%;margin-top:20px;\">\n";
}
}
closedir(dir);
} else {
//Impossible d'ouvrir le répertoire
perror("");
htmlFile.close();
return;
}
//Ferme la balise body et html
htmlFile << "</body>\n</html>\n";
htmlFile.close();
//Ouvre le fichier HTML avec le navigateur par défaut
std::string commandXdgOpen = "xdg-open " + htmlFilename;
system(commandXdgOpen.c_str());
}
void Histogram::showTerminalHistogram() {
vector<string> noms;
vector<int> dispo;
vector<int> max;
//Étape 1: Lire le fichier 'disponibilite_parkings.json'
ifstream file("disponibilite_parkings.json");
if (file.is_open()) {
json j;
file >> j;
for (const auto& parking : j) {
if(parking.find("fields") != parking.end()) {
auto fields = parking["fields"];
if(fields.find("libelle") != fields.end() &&
fields.find("dispo") != fields.end() &&
fields.find("max") != fields.end()) {
string nom = fields["libelle"].get<string>();
int dispoValue = fields["dispo"].get<int>();
int maxValue = fields["max"].get<int>();
noms.push_back(nom);
dispo.push_back(dispoValue);
max.push_back(maxValue);
}
}
}
file.close();
}
//on affiche les histogrammes dans le terminal
const int MAX_BAR_LENGTH = 50; //Nombre maximum de caractères pour représenter 100%
for (size_t i = 0; i < noms.size(); ++i) {
int pourcentage = (100 * dispo[i]) / max[i];
int barLength = (MAX_BAR_LENGTH * pourcentage) / 100;
cout << setw(20) << left << noms[i] << ": ";
for (int j = 0; j < barLength; ++j) {
cout << "█";
}
cout << " " << pourcentage << "%" << endl<<endl;
}
}