更新代码

This commit is contained in:
feiyangqingyun
2022-05-18 17:39:04 +08:00
parent 96938b94f9
commit a155ad45d3
32 changed files with 145 additions and 322 deletions

View File

@@ -13,21 +13,29 @@ frmAxisTag::~frmAxisTag()
delete ui;
}
void frmAxisTag::showEvent(QShowEvent *)
{
dataTimer.start(50);
}
void frmAxisTag::hideEvent(QHideEvent *)
{
dataTimer.stop();
}
void frmAxisTag::initForm()
{
mPlot = ui->customPlot;
// configure plot to have two right axes:
mPlot->yAxis->setTickLabels(false);
connect(mPlot->yAxis2, SIGNAL(rangeChanged(QCPRange)), mPlot->yAxis, SLOT(setRange(QCPRange))); // left axis only mirrors inner right axis
mPlot->yAxis2->setVisible(true);
mPlot->axisRect()->addAxis(QCPAxis::atRight);
mPlot->axisRect()->axis(QCPAxis::atRight, 0)->setPadding(30); // add some padding to have space for tags
mPlot->axisRect()->axis(QCPAxis::atRight, 1)->setPadding(30); // add some padding to have space for tags
ui->customPlot->yAxis->setTickLabels(false);
connect(ui->customPlot->yAxis2, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis, SLOT(setRange(QCPRange))); // left axis only mirrors inner right axis
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->axisRect()->addAxis(QCPAxis::atRight);
ui->customPlot->axisRect()->axis(QCPAxis::atRight, 0)->setPadding(30); // add some padding to have space for tags
ui->customPlot->axisRect()->axis(QCPAxis::atRight, 1)->setPadding(30); // add some padding to have space for tags
// create graphs:
mGraph1 = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 0));
mGraph2 = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 1));
mGraph1 = ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->axisRect()->axis(QCPAxis::atRight, 0));
mGraph2 = ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->axisRect()->axis(QCPAxis::atRight, 1));
mGraph1->setPen(QPen(QColor(250, 120, 0)));
mGraph2->setPen(QPen(QColor(0, 180, 60)));
@@ -37,8 +45,7 @@ void frmAxisTag::initForm()
mTag2 = new AxisTag(mGraph2->valueAxis());
mTag2->setPen(mGraph2->pen());
connect(&mDataTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
mDataTimer.start(40);
connect(&dataTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
}
void frmAxisTag::timerSlot()
@@ -50,10 +57,10 @@ void frmAxisTag::timerSlot()
mGraph2->addData(count2, qCos(count2 / 50.0) + qSin(count2 / 50.0 / 0.4364) * 0.15);
// make key axis range scroll with the data:
mPlot->xAxis->rescale();
ui->customPlot->xAxis->rescale();
mGraph1->rescaleValueAxis(false, true);
mGraph2->rescaleValueAxis(false, true);
mPlot->xAxis->setRange(mPlot->xAxis->range().upper, 100, Qt::AlignRight);
ui->customPlot->xAxis->setRange(ui->customPlot->xAxis->range().upper, 100, Qt::AlignRight);
// update the vertical axis tag positions and texts to match the rightmost data point of the graphs:
double graph1Value = mGraph1->dataMainValue(mGraph1->dataCount() - 1);
@@ -63,5 +70,5 @@ void frmAxisTag::timerSlot()
mTag1->setText(QString::number(graph1Value, 'f', 2));
mTag2->setText(QString::number(graph2Value, 'f', 2));
mPlot->replot();
ui->customPlot->replot();
}

View File

@@ -17,14 +17,17 @@ public:
explicit frmAxisTag(QWidget *parent = 0);
~frmAxisTag();
protected:
void showEvent(QShowEvent *);
void hideEvent(QHideEvent *);
private:
Ui::frmAxisTag *ui;
QCustomPlot *mPlot;
QPointer<QCPGraph> mGraph1;
QPointer<QCPGraph> mGraph2;
AxisTag *mTag1;
AxisTag *mTag2;
QTimer mDataTimer;
QTimer dataTimer;
private slots:
void initForm();

View File

@@ -16,21 +16,23 @@ frmScrollBar::~frmScrollBar()
void frmScrollBar::initForm()
{
// The following plot setup is mostly taken from the plot demos:
ui->plot->addGraph();
ui->plot->graph()->setPen(QPen(Qt::blue));
ui->plot->graph()->setBrush(QBrush(QColor(0, 0, 255, 20)));
ui->plot->addGraph();
ui->plot->graph()->setPen(QPen(Qt::red));
ui->customPlot->addGraph();
ui->customPlot->graph()->setPen(QPen(Qt::blue));
ui->customPlot->graph()->setBrush(QBrush(QColor(0, 0, 255, 20)));
ui->customPlot->addGraph();
ui->customPlot->graph()->setPen(QPen(Qt::red));
QVector<double> x(500), y0(500), y1(500);
for (int i = 0; i < 500; ++i) {
x[i] = (i / 499.0 - 0.5) * 10;
y0[i] = qExp(-x[i] * x[i] * 0.25) * qSin(x[i] * 5) * 5;
y1[i] = qExp(-x[i] * x[i] * 0.25) * 5;
}
ui->plot->graph(0)->setData(x, y0);
ui->plot->graph(1)->setData(x, y1);
ui->plot->axisRect()->setupFullAxesBox(true);
ui->plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
ui->customPlot->graph(0)->setData(x, y0);
ui->customPlot->graph(1)->setData(x, y1);
ui->customPlot->axisRect()->setupFullAxesBox(true);
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
// configure scroll bars:
// Since scroll bars only support integer values, we'll set a high default range of -500..500 and
@@ -43,27 +45,27 @@ void frmScrollBar::initForm()
// create connection between axes and scroll bars:
connect(ui->horizontalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(horzScrollBarChanged(int)));
connect(ui->verticalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(vertScrollBarChanged(int)));
connect(ui->plot->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(xAxisChanged(QCPRange)));
connect(ui->plot->yAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(yAxisChanged(QCPRange)));
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(xAxisChanged(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(yAxisChanged(QCPRange)));
// initialize axis range (and scroll bar positions via signals we just connected):
ui->plot->xAxis->setRange(0, 6, Qt::AlignCenter);
ui->plot->yAxis->setRange(0, 10, Qt::AlignCenter);
ui->customPlot->xAxis->setRange(0, 6, Qt::AlignCenter);
ui->customPlot->yAxis->setRange(0, 10, Qt::AlignCenter);
}
void frmScrollBar::horzScrollBarChanged(int value)
{
if (qAbs(ui->plot->xAxis->range().center() - value / 100.0) > 0.01) { // if user is dragging plot, we don't want to replot twice
ui->plot->xAxis->setRange(value / 100.0, ui->plot->xAxis->range().size(), Qt::AlignCenter);
ui->plot->replot();
if (qAbs(ui->customPlot->xAxis->range().center() - value / 100.0) > 0.01) { // if user is dragging plot, we don't want to replot twice
ui->customPlot->xAxis->setRange(value / 100.0, ui->customPlot->xAxis->range().size(), Qt::AlignCenter);
ui->customPlot->replot();
}
}
void frmScrollBar::vertScrollBarChanged(int value)
{
if (qAbs(ui->plot->yAxis->range().center() + value / 100.0) > 0.01) { // if user is dragging plot, we don't want to replot twice
ui->plot->yAxis->setRange(-value / 100.0, ui->plot->yAxis->range().size(), Qt::AlignCenter);
ui->plot->replot();
if (qAbs(ui->customPlot->yAxis->range().center() + value / 100.0) > 0.01) { // if user is dragging plot, we don't want to replot twice
ui->customPlot->yAxis->setRange(-value / 100.0, ui->customPlot->yAxis->range().size(), Qt::AlignCenter);
ui->customPlot->replot();
}
}

View File

@@ -15,7 +15,7 @@
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QCustomPlot" name="plot" native="true">
<widget class="QCustomPlot" name="customPlot" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
@@ -44,7 +44,7 @@
<customwidget>
<class>QCustomPlot</class>
<extends>QWidget</extends>
<header>qcustomplot.h</header>
<header location="global">qcustomplot.h</header>
<container>1</container>
</customwidget>
</customwidgets>