|
@@ -21,33 +21,72 @@ Homepage::Homepage(QWidget *parent) : QWidget(parent), ui(new Ui::homepage) {
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
QJsonArray dailyCount;
|
|
|
- dailyCount = backendAPIManager::getDailyBlastedCount(15);
|
|
|
+ dailyCount = backendAPIManager::getDailyBlastedCount(7);
|
|
|
+ if (dailyCount.isEmpty()) {
|
|
|
+ ui->chartWidget->setVisible(false);
|
|
|
+ ui->todayBlastCount->setVisible(false);
|
|
|
+ return; // 如果没有数据,直接返回
|
|
|
+ }
|
|
|
|
|
|
- QDate today = QDate::currentDate();
|
|
|
- QString dateTodayStr = QDate::currentDate().toString("yyyy-MM-dd");
|
|
|
+ draw(dailyCount);
|
|
|
+}
|
|
|
+Homepage::~Homepage() { delete ui; }
|
|
|
|
|
|
- QList<QPair<QDate, int>> data;
|
|
|
- // 根据today,倒退15天,检查dailyCount中date是否和日期匹配,如果匹配设置数据,不匹配则设置0
|
|
|
- for (int i = 0; i < 15; ++i) {
|
|
|
- QDate date = today.addDays(-i);
|
|
|
- QString dateStr = date.toString("yyyy-MM-dd");
|
|
|
- bool found = false;
|
|
|
+QChartView *Homepage::drawDetChat(QList<QMap<QString, QVector<int>>> data) {
|
|
|
+ QBarSet *setNormal = new QBarSet("正常");
|
|
|
+ QBarSet *setError = new QBarSet("错误");
|
|
|
+ QStringList categories;
|
|
|
|
|
|
- if (dailyCount[0].toObject().value("blast_at").toString() == dateStr) {
|
|
|
- if (dateStr == dateTodayStr) {
|
|
|
- m_todayCount = dailyCount[0].toObject().value("count").toInt();
|
|
|
- }
|
|
|
- data.append(qMakePair(date, dailyCount[0].toObject().value("count").toInt()));
|
|
|
- dailyCount.removeFirst(); // 移除已处理的元素
|
|
|
- } else {
|
|
|
- data.append(qMakePair(date, 0));
|
|
|
- }
|
|
|
+ int totalCount = 0;
|
|
|
+ for (const auto &entry : data) {
|
|
|
+ QString dateStr = entry.keys().first();
|
|
|
+ QVector<int> counts = entry.values().first();
|
|
|
+ categories << dateStr.mid(5); // Extract MM-dd from yyyy-MM-dd
|
|
|
+ *setNormal << counts[0]; // 正常数量
|
|
|
+ *setError << counts[1]; // 错误数量
|
|
|
}
|
|
|
- draw(data);
|
|
|
+
|
|
|
+ setNormal->setColor(Qt::blue); // 默认色
|
|
|
+ setError->setColor(Qt::red); // 红色
|
|
|
+
|
|
|
+ QBarSeries *series = new QBarSeries();
|
|
|
+ series->append(setNormal);
|
|
|
+ series->append(setError);
|
|
|
+ series->setLabelsVisible(true);
|
|
|
+ QBarSet *set = new QBarSet("");
|
|
|
+ if (totalCount == 0) {
|
|
|
+ set->setLabel("无爆破数据");
|
|
|
+ } else {
|
|
|
+ set->setLabel("总计: " + QString::number(totalCount));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. Create Chart
|
|
|
+ QChart *chart = new QChart();
|
|
|
+ chart->addSeries(series);
|
|
|
+ chart->setTitle("近7日已上传雷管数据统计");
|
|
|
+
|
|
|
+ // 4. X-axis (Category with Dates)
|
|
|
+ QBarCategoryAxis *axisX = new QBarCategoryAxis();
|
|
|
+ axisX->append(categories);
|
|
|
+ axisX->setTitleText("日期");
|
|
|
+ axisX->setGridLineVisible(false);
|
|
|
+ chart->addAxis(axisX, Qt::AlignBottom);
|
|
|
+ series->attachAxis(axisX);
|
|
|
+
|
|
|
+ // 5. Y-axis (Count)
|
|
|
+ QValueAxis *axisY = new QValueAxis();
|
|
|
+ axisY->setTitleText("数量");
|
|
|
+ axisY->setLabelFormat("%d");
|
|
|
+ chart->addAxis(axisY, Qt::AlignLeft);
|
|
|
+ series->attachAxis(axisY);
|
|
|
+
|
|
|
+ // 6. Show Chart
|
|
|
+ QChartView *chartView = new QChartView(chart);
|
|
|
+ chartView->setRenderHint(QPainter::Antialiasing);
|
|
|
+ return chartView;
|
|
|
}
|
|
|
-Homepage::~Homepage() { delete ui; }
|
|
|
|
|
|
-void Homepage::draw(QList<QPair<QDate, int>> data) {
|
|
|
+QChartView *Homepage::drawProjectChat(QList<QPair<QDate, int>> data) {
|
|
|
// 2. Bar Set and Series
|
|
|
QBarSet *set = new QBarSet("");
|
|
|
QStringList categories;
|
|
@@ -70,12 +109,13 @@ void Homepage::draw(QList<QPair<QDate, int>> data) {
|
|
|
// 3. Create Chart
|
|
|
QChart *chart = new QChart();
|
|
|
chart->addSeries(series);
|
|
|
- chart->setTitle("近15日已上传爆破数据统计");
|
|
|
+ chart->setTitle("近7日已上传爆破工程数据统计");
|
|
|
|
|
|
// 4. X-axis (Category with Dates)
|
|
|
QBarCategoryAxis *axisX = new QBarCategoryAxis();
|
|
|
axisX->append(categories);
|
|
|
axisX->setTitleText("日期");
|
|
|
+ axisX->setGridLineVisible(false);
|
|
|
chart->addAxis(axisX, Qt::AlignBottom);
|
|
|
series->attachAxis(axisX);
|
|
|
|
|
@@ -90,9 +130,49 @@ void Homepage::draw(QList<QPair<QDate, int>> data) {
|
|
|
QChartView *chartView = new QChartView(chart);
|
|
|
chartView->setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
+ return chartView;
|
|
|
+}
|
|
|
+
|
|
|
+void Homepage::draw(QJsonArray dailyCount) {
|
|
|
+ QList<QPair<QDate, int>> projectData;
|
|
|
+ QList<QMap<QString, QVector<int>>> detData;
|
|
|
+
|
|
|
+ QDate today = QDate::currentDate();
|
|
|
+ QString dateTodayStr = QDate::currentDate().toString("yyyy-MM-dd");
|
|
|
+
|
|
|
+ // 根据today,倒退7天,检查dailyCount中date是否和日期匹配,如果匹配设置数据,不匹配则设置0
|
|
|
+ for (int i = 0; i < 7; ++i) {
|
|
|
+ QDate date = today.addDays(-i);
|
|
|
+ QString dateStr = date.toString("yyyy-MM-dd");
|
|
|
+ bool found = false;
|
|
|
+
|
|
|
+ if (dailyCount[0].toObject().value("blast_at").toString() == dateStr) {
|
|
|
+ if (dateStr == dateTodayStr) {
|
|
|
+ m_todayCount = dailyCount[0].toObject().value("count").toInt();
|
|
|
+ }
|
|
|
+ projectData.append(qMakePair(date, dailyCount[0].toObject().value("count").toInt()));
|
|
|
+
|
|
|
+ QMap<QString, QVector<int>> detCounts;
|
|
|
+ detCounts[dateStr] = {dailyCount[0].toObject().value("reg_det_count").toInt(),
|
|
|
+ dailyCount[0].toObject().value("error_det_count").toInt()};
|
|
|
+ detData.append(detCounts);
|
|
|
+
|
|
|
+ dailyCount.removeFirst(); // 移除已处理的元素
|
|
|
+ } else {
|
|
|
+ projectData.append(qMakePair(date, 0));
|
|
|
+ QMap<QString, QVector<int>> detCounts;
|
|
|
+ detCounts[dateStr] = {0, 0};
|
|
|
+ detData.append(detCounts);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ QChartView *projectChart = drawProjectChat(projectData);
|
|
|
+ QChartView *detailChart = drawDetChat(detData);
|
|
|
+
|
|
|
+ QHBoxLayout *layout = new QHBoxLayout;
|
|
|
+
|
|
|
+ layout->addWidget(projectChart, 1); // 占 1 份
|
|
|
+ layout->addWidget(detailChart, 1);
|
|
|
// Add the chart view to the widget's layout
|
|
|
- QVBoxLayout *layout = new QVBoxLayout(ui->chartWidget);
|
|
|
- layout->addWidget(chartView);
|
|
|
ui->chartWidget->setLayout(layout);
|
|
|
|
|
|
ui->todayBlastCount->display(m_todayCount);
|