在地震剖面中,我们常需要绘制如下的波形图,如何使用Qt来实现呢?下面是一种尝试,可能帮你解决问题。如果你有更好的方案,欢迎评论,帮助更多人解决问题。
单道波形图
QCustomPlot *plot = new QCustomPlot();
customPlot->setMinimumSize(600, 400);
// Generate datasets to plot
QVector<double> x(1000), y(1000);
for (int i = 0; i < 1000; ++i) {
x[i] = i / 10.0;
y[i] = sin(x[i]) * exp(-x[i] / 10.0) + 0.1 * (rand() / (double)RAND_MAX - 0.5);
}
// create and configure the graph
QCPGraph *graph = customPlot->addGraph();
graph->setData(x, y);
graph->setPen(QPen(Qt::black));
graph->setBrush(QBrush(Qt::black));
// find the minimum and maximum y values
double ymin = INFINITY, ymax = -INFINITY;
for (int i=0; i<y.size(); ++i)
{
if (y[i] < ymin)
ymin = y[i];
if (y[i] > ymax)
ymax = y[i];
}
// create and configure the rect item to cover the area below the x axis
QCPItemRect *rect = new QCPItemRect(customPlot);
rect->setBrush(QBrush(Qt::white));
// set QCPItemRect only display inside of the axis
rect->setClipToAxisRect(true);
rect->setPen(QPen(Qt::NoPen));
rect->topLeft->setCoords(x.first(), ymin);
rect->bottomRight->setCoords(x.last(), 0);
QCPGraph *graph1 = customPlot->addGraph();
graph1->setData(x, y);
graph1->setPen(QPen(Qt::black));
graph1->setBrush(Qt::NoBrush);
// set blank axis lines:
customPlot->rescaleAxes();
customPlot->xAxis->grid()->setVisible(false);
customPlot->yAxis->grid()->setVisible(false);
// make top right axes clones of bottom left axes:
customPlot->axisRect()->setupFullAxesBox();
// set the range and labels for the axes
customPlot->xAxis->setRange(x.first(), x.last());
customPlot->xAxis->setLabel("X Axis");
customPlot->yAxis->setRange(ymin, ymax);
customPlot->yAxis->setLabel("Y Axis");
// show the plot
customPlot->replot();
customPlot->show();
网友评论