在地震剖面中,我们常需要绘制如下的波形图,如何使用Qt来实现呢?下面是一种尝试,可能帮你解决问题。如果你有更好的方案,欢迎评论,帮助更多人解决问题。
方法1: 将点集划分为x轴上方和下方的点,对于x轴交点位置,需要求解。使用直线方程求解。
customPlot->addGraph();
customPlot->graph(0)->setPen(Qt::NoPen); // no line
customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 0, 255))); // first graph will be filled with black
customPlot->addGraph();
customPlot->graph(1)->setPen(QPen(Qt::black)); // only draw line under x-axis
// customPlot->graph(1)->setBrush(QBrush(QColor(0, 255, 0, 255)));
// generate some points of data (y0 for first, y1 for second graph):
QVector<double> x(1000), y0(1000);
for (int i = 0; i < 1000; ++i) {
x[i] = i / 10.0;
y0[i] = sin(x[i]) * exp(-x[i] / 10.0) + 0.1 * (rand() / (double)RAND_MAX - 0.5);
}
QVector<double> xPositive, yPositive, xNegative, yNegative;
for (int i = 0; i < x.size() - 1; ++i) {
if(std::abs(y0[i]) < 1e-9){
xPositive.push_back(x[i]);
xNegative.push_back(x[i]);
yPositive.push_back(y0[i]);
yNegative.push_back(y0[i]);
continue;
}
bool isCrossing = y0[i] * y0[i + 1] <= 0;
if (y0[i] > 0) {
xPositive.push_back(x[i]);
yPositive.push_back(y0[i]);
} else if(y0[i] < 0){
xNegative.push_back(x[i]);
yNegative.push_back(y0[i]);
}
if (isCrossing) {
double xCross = -1.0 * (x[i] * y0[i + 1] - x[i + 1] * y0[i]) / (y0[i] - y0[i + 1]);
xPositive.push_back(xCross);
yPositive.push_back(0);
xNegative.push_back(xCross);
yNegative.push_back(0);
}
}
if(y0.back() >= 0){
xPositive.push_back(x.back());
yPositive.push_back(y0.back());
}
if(y0.back() <= 0){
xNegative.push_back(x.back());
yNegative.push_back(y0.back());
}
customPlot->xAxis2->setVisible(true);
customPlot->xAxis2->setTickLabels(false);
customPlot->yAxis2->setVisible(true);
customPlot->yAxis2->setTickLabels(false);
// make left and bottom axes always transfer their ranges to right and top axes:
connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
// pass data points to graphs:
customPlot->graph(0)->setData(xPositive, yPositive);
// customPlot->graph(0)->setData(x, y0);
customPlot->graph(1)->setData(xNegative, yNegative);
// customPlot->graph(2)->setData({0, 251}, {0, 0});
// let the ranges scale themselves so graph 0 fits perfectly in the visible area:
customPlot->graph(0)->rescaleAxes();
// same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
customPlot->graph(1)->rescaleAxes(true);
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
customPlot->replot();
customPlot->show();
方法2:将低于x轴的遮盖,再绘制一次曲线
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();