diff --git a/CMakeLists.txt b/CMakeLists.txt index 982b173..ff0fa86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,11 @@ set(PROJECT_SOURCES mainwindow.ui gloab.h gloab.cpp + lib/qcustomplot.h + lib/qcustomplot.cpp + qcustplot.cpp + qcustplot.h + qcustplot.ui ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) diff --git a/gloab.cpp b/gloab.cpp index f558af2..5a022bf 100644 --- a/gloab.cpp +++ b/gloab.cpp @@ -4,10 +4,11 @@ QString Gloab::fileName=""; QVector> Gloab::g_param[4]; QMap Gloab::temData; QMap Gloab::namelist; -QList Gloab::m_chartViews; +QList Gloab::m_QCView; QWidget * Gloab::widget; QVector Gloab::rule; QFile Gloab::file; qint64 Gloab::fileSize; qint64 Gloab::bytesRead; QCompleter *Gloab::completer; +QVector Gloab::xData,Gloab::yData; diff --git a/gloab.h b/gloab.h index 742aa3c..186a99d 100644 --- a/gloab.h +++ b/gloab.h @@ -14,19 +14,22 @@ #include #include #include +#include "qcustplot.h" + class Gloab{ public: static QString fileName; static QVector> g_param[4]; static QMap temData; static QMap namelist; - static QList m_chartViews; + static QList m_QCView; static QWidget *widget; static QVector rule; static QFile file; static qint64 fileSize; static qint64 bytesRead; static QCompleter *completer; + static QVector xData,yData; }; diff --git a/log7.pro b/log7.pro index f13cd78..8a2a93f 100644 --- a/log7.pro +++ b/log7.pro @@ -1,6 +1,6 @@ QT += core gui charts -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport CONFIG += c++11 @@ -10,21 +10,26 @@ CONFIG += c++11 SOURCES += \ gloab.cpp \ + lib/qcustomplot.cpp \ main.cpp \ mainwindow.cpp \ + qcustplot.cpp \ threadchart.cpp \ threadpoltchart.cpp \ threadread.cpp HEADERS += \ gloab.h \ + lib/qcustomplot.h \ mainwindow.h \ + qcustplot.h \ threadchart.h \ threadpoltchart.h \ threadread.h FORMS += \ - mainwindow.ui + mainwindow.ui \ + qcustplot.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin diff --git a/log7.pro.user b/log7.pro.user index 067dccb..5268dfd 100644 --- a/log7.pro.user +++ b/log7.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/mainwindow.cpp b/mainwindow.cpp index 6a8b93d..e3e45a8 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,6 +1,7 @@ #include "mainwindow.h" #include +#include "lib/qcustomplot.h" #include "./ui_mainwindow.h" @@ -23,15 +24,11 @@ MainWindow::MainWindow(QWidget *parent) connect(thread_read, &threadRead::closeProg, this, &MainWindow::closeProg); //chart - connect(thread_chart, &ThreadChart::newCandS, this, &MainWindow::newCandS); - connect(thread_chart, &ThreadChart::appSeries, this, &MainWindow::appSeries); connect(thread_chart, &ThreadChart::setChartView, this, &MainWindow::setChartView); connect(thread_chart, &ThreadChart::setWidget, this, &MainWindow::setWidget); connect(thread_chart, &ThreadChart::setProgressBar, this, &MainWindow::setProgressBar); //plot - connect(thread_polt, &threadPoltChart::newCandS, this, &MainWindow::newPlotCandS); - connect(thread_polt, &threadPoltChart::appSeries, this, &MainWindow::appSeries); connect(thread_polt, &threadPoltChart::setChartView, this, &MainWindow::setPlotChartView); connect(thread_polt, &threadPoltChart::setWidget, this, &MainWindow::setWidget); connect(thread_polt, &threadPoltChart::setProgressBar, this, &MainWindow::setProgressBar); @@ -94,7 +91,6 @@ void MainWindow::addCombo(const QStringList &list) { void MainWindow::setProgressBar(int percent) { progressBar->setValue(percent); - // qApp->processEvents(); // 更新UI } void MainWindow::closeProg() { @@ -103,76 +99,30 @@ void MainWindow::closeProg() { ui->widget->close(); } -void MainWindow::newCandS() { - chart = new QChart; - series = new QLineSeries; - // series->setPointsVisible(); -} - -void MainWindow::appSeries(qreal A, qreal B) { - series->append(A, B); -} void MainWindow::setChartView(int jd, bool isEnum) { // 设置图表标题 - chart->setTitle(QString("基带 %1").arg(jd+1)); - chart->addSeries(series); - if (isEnum) { - chart->createDefaultAxes(); - // 获取X轴 - QList xAxes = chart->axes(Qt::Horizontal); - // 断开所有与该轴关联的系列 - series->detachAxis(xAxes[0]); - // 从图表中移除轴 - chart->removeAxis(xAxes[0]); - } else { - // 创建类别Y轴 - QCategoryAxis *axisY = new QCategoryAxis(); - for (int var = 1; var < Gloab::rule.size(); ++var) { - axisY->append(Gloab::rule[var], var); - } - //刻度线和刻度的label对齐 - axisY->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue); - axisY->setRange(0, Gloab::rule.size() + 1); - chart->setAxisY(axisY, series); - // series->attachAxis(axisY); - } - - - // 创建一个时间轴 - QDateTimeAxis *axisX = new QDateTimeAxis; - axisX->setTickCount(10); - axisX->setFormat("HH:mm"); // 设置时间格式 - chart->addAxis(axisX, Qt::AlignBottom); - series->attachAxis(axisX); - - - // 创建QChartView - QChartView *chartView = new QChartView(chart); - chartView->setRenderHint(QPainter::Antialiasing); - - // 将chartView添加到容器中 - Gloab::m_chartViews.append(chartView); + QCustPlot *qCustPlot = new QCustPlot(Gloab::xData, Gloab::yData, jd, isEnum, true); + // 将qCustPlot添加到容器中 + Gloab::m_QCView.append(qCustPlot); } void MainWindow::setWidget() { // 设置布局 int wide = 850; int hight = 600; - if (Gloab::m_chartViews.size() > 1) { + if (Gloab::m_QCView.size() > 1) { wide = 1800; } QGridLayout *gridLayout = new QGridLayout(Gloab::widget); - for (int i = 0, j = 0; i < Gloab::m_chartViews.size(); i++) { + for (int i = 0, j = 0; i < Gloab::m_QCView.size(); i++) { if (i != 0 && i % 2 == 0) { j++; hight += 350; } - gridLayout->addWidget(Gloab::m_chartViews[i], j, i - 2 * j); + gridLayout->addWidget(Gloab::m_QCView[i], j, i - 2 * j); } Gloab::widget->resize(wide, hight); - // Gloab::widget->setMaximumHeight(950); - Gloab::widget->move(70, 20); Gloab::widget->show(); @@ -181,48 +131,13 @@ void MainWindow::setWidget() { ui->widget->close(); } -void MainWindow::newPlotCandS() { - chart = new QChart; - series = new QLineSeries; -// series->setPointsVisible(); -} + void MainWindow::setPlotChartView(int jd, bool isEnum) { // 设置图表标题 - chart->setTitle(QString("基带 %1").arg(jd+1)); - chart->addSeries(series); - if (isEnum) { - chart->createDefaultAxes(); - - } else { - //创建默认XY轴 - chart->createDefaultAxes(); - //删除Y轴 - // 获取Y轴 - QList yAxes = chart->axes(Qt::Vertical); - // 断开所有与该轴关联的系列 - series->detachAxis(yAxes[0]); - // 从图表中移除轴 - chart->removeAxis(yAxes[0]); - // 创建类别Y轴 - QCategoryAxis *axisY = new QCategoryAxis(); - for (int var = 1; var < Gloab::rule.size(); ++var) { - axisY->append(Gloab::rule[var], var); - } - //刻度线和刻度的label对齐 - axisY->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue); - axisY->setRange(0, Gloab::rule.size() + 1); - chart->setAxisY(axisY, series); - // plotSeries->attachAxis(axisY); - } - - - // 创建QChartView - QChartView *chartView = new QChartView(chart); - chartView->setRenderHint(QPainter::Antialiasing); - - // 将chartView添加到容器中 - Gloab::m_chartViews.append(chartView); + QCustPlot *qCustPlot = new QCustPlot(Gloab::xData, Gloab::yData, jd, isEnum, false); + // 将qCustPlot添加到容器中 + Gloab::m_QCView.append(qCustPlot); } void MainWindow::on_lineChart_clicked() { diff --git a/mainwindow.h b/mainwindow.h index a7e6af8..50dce2b 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -8,7 +8,7 @@ #include #include #include - +#include "qcustplot.h" #include "threadchart.h" #include "threadpoltchart.h" @@ -38,12 +38,9 @@ private slots: void setProgressBar(int percent); void closeProg(); - void newCandS(); - void appSeries(qreal A,qreal B); void setChartView(int jd,bool isEnum); void setWidget(); - void newPlotCandS(); void setPlotChartView(int jd,bool isEnum); void on_lineChart_clicked(); diff --git a/qcustplot.cpp b/qcustplot.cpp new file mode 100644 index 0000000..d2cf850 --- /dev/null +++ b/qcustplot.cpp @@ -0,0 +1,60 @@ +#include "qcustplot.h" +#include "ui_qcustplot.h" +#include +#include +#include "gloab.h" + +QCustPlot::QCustPlot(QVector xData, QVector yData, int jd, bool isNub, bool isTime, + QWidget *parent) + : QWidget(parent) + , ui(new Ui::QCustPlot) +{ + ui->setupUi(this); + + //标题 + QCPTextElement *m_title; + ui->plot->plotLayout()->insertRow(0); + m_title = new QCPTextElement(ui->plot, QString("基带 %1").arg(jd+1)); + ui->plot->plotLayout()->addElement(0, 0, m_title); + + // X轴 + if (isTime) { + QSharedPointer timeTicker(new QCPAxisTickerDateTime); + timeTicker->setDateTimeFormat("hh:mm:ss"); + ui->plot->xAxis->setTicker(timeTicker); + } + + // Y轴 + if(!isNub){ + QVector ticks; + QVector labels; + for (int i = 1; i < Gloab::rule.size(); ++i) { + ticks.append(i); + labels.append(Gloab::rule[i]); + } + QSharedPointer textTicker(new QCPAxisTickerText); + textTicker->addTicks(ticks, labels); + ui->plot->yAxis->setTicker(textTicker); + } + + // 添加图形 + ui->plot->addGraph(); + ui->plot->graph(0)->setData(xData, yData); + + + // 自动调整轴范围 + ui->plot->rescaleAxes(); + + ui->plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom ); + + if(!xData.empty()){ + ui->plot->xAxis->setRange(xData.first(),xData.last()); + } + +} + +QCustPlot::~QCustPlot() +{ + delete ui; +} + diff --git a/qcustplot.h b/qcustplot.h new file mode 100644 index 0000000..d32ee6a --- /dev/null +++ b/qcustplot.h @@ -0,0 +1,25 @@ +#ifndef QCUSTPLOT_H +#define QCUSTPLOT_H + +#include +#include "lib/qcustomplot.h" + +namespace Ui { +class QCustPlot; +} + +class QCustPlot : public QWidget +{ + Q_OBJECT + +public: + explicit QCustPlot(QVector xData, QVector yData, int jd, bool isNub, bool isTime, + QWidget *parent = nullptr); + ~QCustPlot(); + +private: + Ui::QCustPlot *ui; + +}; + +#endif // QCUSTPLOT_H diff --git a/qcustplot.ui b/qcustplot.ui new file mode 100644 index 0000000..b8dbe21 --- /dev/null +++ b/qcustplot.ui @@ -0,0 +1,39 @@ + + + QCustPlot + + + + 0 + 0 + 673 + 480 + + + + Form + + + + + + + 0 + 0 + + + + + + + + + QCustomPlot + QWidget +
lib/qcustomplot.h
+ 1 +
+
+ + +
diff --git a/threadchart.cpp b/threadchart.cpp index 4a31f71..903de7c 100644 --- a/threadchart.cpp +++ b/threadchart.cpp @@ -14,13 +14,15 @@ void ThreadChart::setValue(QVector jd, QString dataName, QDateTime beginTim void ThreadChart::run() { - Gloab::m_chartViews.clear(); + Gloab::m_QCView.clear(); Gloab::rule.clear(); Gloab::rule.append("状态"); //判断右侧数据是否为数字型 bool isEnum = Gloab::namelist[dataName]; for (int i = 0; i < jd.size(); ++i) { - emit newCandS(); +// emit newCandS(); + Gloab::xData.clear(); + Gloab::yData.clear(); qreal y = 114.514; QString tem1 = "无数据"; // 为每个图表添加数据 @@ -35,16 +37,13 @@ void ThreadChart::run() { if (temTime >= beginTime && temTime < endTime) { if (!Gloab::g_param[jd[i]][j].contains(dataName)) { if (y != 114.514) { - emit appSeries(temTime.toMSecsSinceEpoch(), y); + Gloab::xData.append(temTime.toMSecsSinceEpoch()/1000.0); + Gloab::yData.append(y); } } else { - if (Gloab::g_param[jd[i]][j][dataName].contains(".")) { - y = Gloab::g_param[jd[i]][j][dataName].toDouble(); - emit appSeries(temTime.toMSecsSinceEpoch(), y); - } else { - y = Gloab::g_param[jd[i]][j][dataName].toLongLong(); - emit appSeries(temTime.toMSecsSinceEpoch(), y); - } + y = Gloab::g_param[jd[i]][j][dataName].toDouble(); + Gloab::xData.append(temTime.toMSecsSinceEpoch()/1000.0); + Gloab::yData.append(y); } } } @@ -62,14 +61,16 @@ void ThreadChart::run() { if (!Gloab::rule.contains(tem1)) { Gloab::rule.append(tem1); } - emit appSeries(temTime.toMSecsSinceEpoch(), Gloab::rule.indexOf(tem1)); + Gloab::xData.append(temTime.toMSecsSinceEpoch()/1000.0); + Gloab::yData.append(Gloab::rule.indexOf(tem1)); } } else { tem1 = Gloab::g_param[jd[i]][j][dataName]; if (!Gloab::rule.contains(tem1)) { Gloab::rule.append(tem1); } - emit appSeries(temTime.toMSecsSinceEpoch(), Gloab::rule.indexOf(tem1)); + Gloab::xData.append(temTime.toMSecsSinceEpoch()/1000.0); + Gloab::yData.append(Gloab::rule.indexOf(tem1)); } } } diff --git a/threadpoltchart.cpp b/threadpoltchart.cpp index 247ed58..7157467 100644 --- a/threadpoltchart.cpp +++ b/threadpoltchart.cpp @@ -2,7 +2,7 @@ #include threadPoltChart::threadPoltChart(QObject *parent) - : QThread{parent} { + : QThread{parent} { } void threadPoltChart::setValue(QVector jd, QString dataName, QDateTime beginTime, QDateTime endTime) { @@ -13,21 +13,18 @@ void threadPoltChart::setValue(QVector jd, QString dataName, QDateTime begi } - void threadPoltChart::run() { - Gloab::m_chartViews.clear(); + Gloab::m_QCView.clear(); Gloab::rule.clear(); Gloab::rule.append("状态"); + //判断右侧数据是否为数字型 + bool isEnum = Gloab::namelist[dataName]; for (int i = 0; i < jd.size(); ++i) { int X = 1; - emit newCandS(); + Gloab::xData.clear(); + Gloab::yData.clear(); // 为每个图表添加数据 - //判断右侧数据是否为数字型 - bool isEnum = Gloab::namelist[dataName]; - if (dataName.contains("版本号")) { - isEnum = false; - } if (isEnum) { for (int j = 0; j < Gloab::g_param[jd[i]].size(); j++) { //更新进度条 @@ -35,11 +32,11 @@ void threadPoltChart::run() { int percent = Gloab::bytesRead * 100 / Gloab::fileSize; emit setProgressBar(percent); - if (Gloab::g_param[jd[i]][j].keys().size()<=deleteName.size()) { - bool f=true; - for(auto key : Gloab::g_param[jd[i]][j].keys()) { + if (Gloab::g_param[jd[i]][j].keys().size() <= deleteName.size()) { + bool f = true; + for (auto key: Gloab::g_param[jd[i]][j].keys()) { if (!deleteName.contains(key)) { - f=false; + f = false; break; } } @@ -52,11 +49,8 @@ void threadPoltChart::run() { QDateTime temTime = QDateTime::fromString(Gloab::g_param[jd[i]][j]["时间"], "yyyy-MM-dd hh:mm:ss:zzz"); if (temTime >= beginTime && temTime < endTime) { if (Gloab::g_param[jd[i]][j].contains(dataName)) { - if (Gloab::g_param[jd[i]][j][dataName].contains(".")) { - emit appSeries(X++, Gloab::g_param[jd[i]][j][dataName].toDouble()); - } else { - emit appSeries(X++, Gloab::g_param[jd[i]][j][dataName].toLongLong()); - } + Gloab::xData.append(X++); + Gloab::yData.append(Gloab::g_param[jd[i]][j][dataName].toDouble()); } } } @@ -74,7 +68,8 @@ void threadPoltChart::run() { if (!Gloab::rule.contains(tem1)) { Gloab::rule.append(tem1); } - emit appSeries(X++, Gloab::rule.indexOf(tem1)); + Gloab::xData.append(X++); + Gloab::yData.append(Gloab::rule.indexOf(tem1)); } } } diff --git a/threadpoltchart.h b/threadpoltchart.h index 2ddb65f..b3ef874 100644 --- a/threadpoltchart.h +++ b/threadpoltchart.h @@ -25,10 +25,6 @@ public: signals: - void newCandS(); - - void appSeries(qreal A, qreal B); - void setChartView(int jd, bool isEnum); void setWidget();