c++ - 如何选择从QCPCurve的哪一侧填充渐变?

标签 c++ qt graph curve qcustomplot

我正在使用 qcustomplot 并绘制一条曲线 (QCPCurve)。下面的代码只填充曲线上方的区域。但我想填充曲线另一侧的区域。以及如何使渐变填充区域一直延伸到图形的边界?

what i need to do

_myPlot->clearPlottables();

QVector<double> x;
QVector<double> y;

for(int point = 0; point < shadowZone.length() ; point++){
    x.push_back(shadowZone.at(point).longitude);
    y.push_back(shadowZone.at(point).latitude);
}

QBrush shadowBrush (QColor(0,0,0), Qt::Dense7Pattern);

QCPCurve *newCurve = new QCPCurve(_myPlot->xAxis, _myPlot->yAxis);
newCurve->setBrush(shadowBrush);
newCurve->addData(x,y);

_myPlot->replot();

最佳答案

一个可能的解决方案是使用QCPGraph而不是使用QCPCurve来使用setChannelFillGraph(),策略是创建另一个 QCPGraph 是图形的最低行,必要时必须使用轴的 rangeChanged 信号来更新图形。

QCPGraph *newCurve = new QCPGraph(_myPlot->xAxis , _myPlot->yAxis);
newCurve->addData(x,y);

QBrush shadowBrush(QColor(0,0,0), Qt::Dense7Pattern);

newCurve->setBrush(shadowBrush);

QCPGraph *minGraph = new QCPGraph(_myPlot->xAxis , _myPlot->yAxis);
newCurve->setChannelFillGraph(minGraph);

QObject::connect(_myPlot->xAxis, static_cast<void(QCPAxis::*)(const QCPRange &)>(&QCPAxis::rangeChanged), [_myPlot, minGraph](const QCPRange & newRange){
    minGraph->setData(QVector<double>{newRange.lower, newRange.upper},
                      QVector<double>{_myPlot->yAxis->range().lower,_myPlot->yAxis->range().lower});
});

QObject::connect(_myPlot->yAxis, static_cast<void(QCPAxis::*)(const QCPRange &)>(&QCPAxis::rangeChanged), [_myPlot, minGraph](const QCPRange & newRange){
    minGraph->setData(QVector<double>{_myPlot->xAxis->range().lower,_myPlot->xAxis->range().upper},
                      QVector<double>{newRange.lower, newRange.lower});
});

一个完整的例子可以在下面的link中找到

enter image description here

关于c++ - 如何选择从QCPCurve的哪一侧填充渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48365707/

相关文章:

reporting-services - SSRS 中的图表仅显示几周的数据

c++ - C 中不明确的定义指令

c++ - 如果代码中的任何地方都没有连接插槽,是否有理由发出 Qt 信号?

c++ - 永远不会调用 qgraphicsScene 的 drawBackground

python - 使小值在 python 中的 matplotlib 颜色条上可见

javascript - Fusioncharts 中的可滚动 X 轴

c++ - 可变参数被破坏/无效的原因

c++ - 为什么有些场景不需要 "std::"?

使用 PPL 不锁定临界区的 C++ 并行循环

qt - QML - 垂直滑动 View ?