diff --git a/libzqt/widgetplot2d.cpp b/libzqt/widgetplot2d.cpp index 0fca44f..9c47202 100644 --- a/libzqt/widgetplot2d.cpp +++ b/libzqt/widgetplot2d.cpp @@ -4,7 +4,7 @@ #include "ui_widgetplot2d.h" -WidgetPlot2D::WidgetPlot2D(QWidget* parent) : QWidget(parent), ui(new Ui::WidgetPlot2D), time(QTime::currentTime()) { +WidgetPlot2D::WidgetPlot2D(QWidget* parent) : QWidget(parent), ui(new Ui::WidgetPlot2D) { ui->setupUi(this); // QCP绘图初始化 initQCP(); @@ -19,7 +19,7 @@ WidgetPlot2D::WidgetPlot2D(QWidget* parent) : QWidget(parent), ui(new Ui::Widget // 滚动条设置 ui->horizontalScrollBar->setRange(0, 0); // 拖动水平滚动条,图幅跟随变化 - connect(ui->horizontalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(horzScrollBarChanged(int))); + // connect(ui->horizontalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(horzScrollBarChanged(int))); // 默认坐标范围 ui->customPlot->yAxis->setRange(-20, 20); ui->customPlot->replot(); @@ -113,17 +113,29 @@ void WidgetPlot2D::initGraphName(QStringList name) { void WidgetPlot2D::addData(QString name, double value, int offms) { // 如果点击了“暂停”按钮,则不绘制图形 if (ui->pausePBtn->text() == "开始") return; + + double key = 0; + double val = value; + + pointCnt[nameToGraphMap[name]]++; + key = pointCnt[nameToGraphMap[name]]; + + if (ui->autoRangeCheck->isChecked()) { + ui->customPlot->rescaleAxes(); + } + ui->customPlot->graph(nameToGraphMap[name])->addData(key, value); + ui->customPlot->xAxis->setRange(key, ui->customPlot->xAxis->range().size(), Qt::AlignRight); + ui->customPlot->replot(QCustomPlot::rpQueuedReplot); + +#if 0 // 系统当前时间 = 系统运行初始时间 + 系统运行时间 static double start = time.hour() * 60 * 60 + time.minute() * 60 + time.second() + time.msec() / 1000.0; double key = start + time.elapsed() / 1000.0 + offms / 1000; - // double start = 0; - // static int key = 0; - // key++; - // qDebug() << "start" << start; - // qDebug() << "key" << key; + // 子网格显示 ui->customPlot->xAxis->grid()->setSubGridVisible(ui->subGridCheck->isChecked()); ui->customPlot->yAxis->grid()->setSubGridVisible(ui->subGridCheck->isChecked()); + // 自适应量程 if (ui->autoRangeCheck->isChecked()) { ui->customPlot->rescaleAxes(); @@ -165,6 +177,7 @@ void WidgetPlot2D::addData(QString name, double value, int offms) { valueLabelVector[t]->setText(QString::number(valueVector[t])); } } +#endif //----------------------------------------------------------------------------------------// } @@ -235,9 +248,9 @@ void WidgetPlot2D::initQCP() { // 可放大缩小和移动 ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); // x轴以时间形式显示 - QSharedPointer timeTicker(new QCPAxisTickerTime); - timeTicker->setTimeFormat("%h:%m:%s"); - ui->customPlot->xAxis->setTicker(timeTicker); + // QSharedPointer timeTicker(new QCPAxisTickerTime); + // timeTicker->setTimeFormat("%h:%m:%s"); + // ui->customPlot->xAxis->setTicker(timeTicker); ui->customPlot->axisRect()->setupFullAxesBox(); ui->customPlot->replot(); } @@ -382,7 +395,10 @@ void WidgetPlot2D::plotOperation() { // 先获得每条曲线的数据指针,然后删除数据 ui->customPlot->graph(i)->data()->clear(); ui->customPlot->replot(); - time = QTime::currentTime(); + + for (size_t i = 0; i < 100; i++) { + pointCnt[i] = 0; + } } } if (btn->text() == "整图") { @@ -437,10 +453,4 @@ void WidgetPlot2D::savePlotPng() { } /* 水平滚动条移动 */ -void WidgetPlot2D::horzScrollBarChanged(int value) { - // 若全程显示,则不移动绘图坐标 - if (ui->fullShowCheck->isChecked()) return; - int timeAxis = ui->timeAxisSpin->value(); - ui->customPlot->xAxis->setRange(value, timeAxis, Qt::AlignRight); - ui->customPlot->replot(); -} +void WidgetPlot2D::horzScrollBarChanged(int value) {} diff --git a/libzqt/widgetplot2d.h b/libzqt/widgetplot2d.h index 806e4a6..8880ee1 100644 --- a/libzqt/widgetplot2d.h +++ b/libzqt/widgetplot2d.h @@ -1,54 +1,52 @@ #ifndef WIDGETPLOT2D_H #define WIDGETPLOT2D_H -#include #include #include #include +#include + #include "qcustomplot.h" namespace Ui { class WidgetPlot2D; } -class WidgetPlot2D : public QWidget -{ - Q_OBJECT - -public: - explicit WidgetPlot2D(QWidget *parent = 0); - ~WidgetPlot2D(); +class WidgetPlot2D : public QWidget { + Q_OBJECT - void initGraphName(QStringList name); + public: + explicit WidgetPlot2D(QWidget *parent = 0); + ~WidgetPlot2D(); -private: - Ui::WidgetPlot2D *ui; + void initGraphName(QStringList name); - QVector isShowCheckBoxVector; // 是否显示勾选框 - QVector valueLabelVector; // label数值显示 - QVector GraphColorPushButtonVector; // 曲线颜色选择按钮 - QMap nameToGraphMap; // 曲线名称对应曲线序号 - QVector valueVector; // 存储曲线的当前值 - QTime time; + private: + Ui::WidgetPlot2D *ui; - int pointNum = 0; + QVector isShowCheckBoxVector; // 是否显示勾选框 + QVector valueLabelVector; // label数值显示 + QVector GraphColorPushButtonVector; // 曲线颜色选择按钮 + QMap nameToGraphMap; // 曲线名称对应曲线序号 + QVector valueVector; // 存储曲线的当前值 - void initQCP(); - void initWidget(); - void setTheme(QColor axis, QColor background); - bool isDirExist(QString fullPath); - void savePlotPng(); + int pointCnt[100] = {0}; + void initQCP(); + void initWidget(); + void setTheme(QColor axis, QColor background); + bool isDirExist(QString fullPath); + void savePlotPng(); -private slots: - void changeGraphVisible(); - void changeGraphColor(); - void plotOperation(); - void horzScrollBarChanged(int value); - void changePlotTheme(); + private slots: + void changeGraphVisible(); + void changeGraphColor(); + void plotOperation(); + void horzScrollBarChanged(int value); + void changePlotTheme(); -public slots: - void addData(QString name, double value,int offms); + public slots: + void addData(QString name, double value, int offms); }; -#endif // WIDGETPLOT2D_H +#endif // WIDGETPLOT2D_H