-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.cpp
More file actions
68 lines (50 loc) · 1.8 KB
/
Copy pathstats.cpp
File metadata and controls
68 lines (50 loc) · 1.8 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
#include <QSqlQuery>
#include <QSqlRecord>
#include <QDebug>
#include "stats.h"
#include "ui_stats.h"
Stats::Stats(QWidget *parent)
: QWidget(parent), chartView(nullptr) {
}
Stats::~Stats() {
}
void Stats::calculateAndDisplayChart() {
QVector<QPair<QString, int>> posteCountData = getPosteCountDataFromDB();
if (posteCountData.isEmpty()) {
QMessageBox::information(this, "No Data", "No data available for statistics.");
return;
}
// Create a chart and add the series
QtCharts::QChart *chart = new QtCharts::QChart();
// Create a bar series
QtCharts::QBarSeries *barSeries = new QtCharts::QBarSeries();
// Create a bar set for each category with different colors
for (const auto& pair : posteCountData) {
QtCharts::QBarSet *barSet = new QtCharts::QBarSet(pair.first);
*barSet << pair.second;
barSeries->append(barSet);
}
chart->addSeries(barSeries);
chart->setTitle("Poste Counts");
chart->createDefaultAxes();
// Set up a new QMainWindow to display the chart
QMainWindow *newWindow = new QMainWindow();
chartView = new QtCharts::QChartView(chart);
newWindow->setCentralWidget(chartView);
newWindow->setWindowTitle("Statistics Chart");
newWindow->show();
}
QVector<QPair<QString, int>> Stats::getPosteCountDataFromDB() {
QVector<QPair<QString, int>> posteCountData;
QSqlQuery query;
query.prepare("SELECT POSTE, COUNT(*) FROM EMPLOYE GROUP BY POSTE");
if (query.exec()) {
while (query.next()) {
QString poste = query.value(0).toString();
int count = query.value(1).toInt();
posteCountData.push_back(qMakePair(poste, count));
}
} else {
}
return posteCountData;
}