渐变图很简单,在Qt中线性渐变使用QLinearGradient即可实现,曲线图可以参考
QCustomPlot之平滑曲线下(九)
static QBrush GenerateLinearBrush(QColor startColor, QColor endColor)
{
startColor.setAlpha(0.8*255);
endColor.setAlpha(0.8*255);
QLinearGradient gradient(0, 0, 0, 1);
gradient.setCoordinateMode(QLinearGradient::ObjectMode);
gradient.setColorAt(0, startColor);
gradient.setColorAt(1, endColor);
return QBrush(gradient);
}
static QCPGraph* AddGraph(
QCustomPlot* parentPlot,
const QVector<double>& keys,
const QVector<double>& values,
const QString& name)
{
auto graph = new QCPSmoothGraph(parentPlot->xAxis, parentPlot->yAxis);
graph->setData(keys, values);
graph->setPen(Qt::NoPen);
graph->setName(name);
return graph;
}
void GradientStackedAreaChart::initCustomPlot(QCustomPlot *parentPlot)
{
QVector<double> x = {
0, 1, 2, 3, 4, 5, 6,
};
QVector<double> y = {
150, 230, 224, 218, 135, 147, 260
};
QVector<QString> labels = {
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
};
auto ticker = QSharedPointer<QCPAxisTickerText>::create();
ticker->setTicks(x, labels);
auto graph1 = AddGraph(parentPlot, x, {140, 232, 101, 264, 90, 340, 250},
"Line 1");
auto graph2 = AddGraph(parentPlot, x, {120, 282, 111, 234, 220, 340, 310},
"Line 2");
auto graph3 = AddGraph(parentPlot, x, {320, 132, 201, 334, 190, 130, 220},
"Line 3");
auto graph4 = AddGraph(parentPlot, x, {220, 402, 231, 134, 190, 230, 120},
"Line 4");
auto graph5 = AddGraph(parentPlot, x, {220, 302, 181, 234, 210, 290, 150},
"Line 5");
graph5->moveAbove(graph4);
graph4->moveAbove(graph3);
graph3->moveAbove(graph2);
graph2->moveAbove(graph1);
graph5->setBrush(GenerateLinearBrush(QColor(255, 191, 0), QColor(224, 62, 76)));
graph5->setChannelFillGraph(graph4);
graph4->setBrush(GenerateLinearBrush(QColor(255, 0, 135), QColor(135, 0, 157)));
graph4->setChannelFillGraph(graph3);
graph3->setBrush(GenerateLinearBrush(QColor(55, 162, 255), QColor(116, 21, 219)));
graph3->setChannelFillGraph(graph2);
graph2->setBrush(GenerateLinearBrush(QColor(0, 221, 255), QColor(77, 119, 255)));
graph2->setChannelFillGraph(graph1);
graph1->setBrush(GenerateLinearBrush(QColor(128, 255, 165), QColor(1, 191, 236)));
parentPlot->xAxis->setTicker(ticker);
parentPlot->xAxis->grid()->setVisible(false);
parentPlot->xAxis->setRange(0, 6);
parentPlot->yAxis->setRange(0, 1500);
parentPlot->yAxis->setBasePen(Qt::NoPen);
parentPlot->yAxis->setSubTicks(false);
parentPlot->yAxis->setTickPen(Qt::NoPen);
parentPlot->yAxis->grid()->setPen(QPen(QColor(224, 230, 241), 1));
parentPlot->legend->setVisible(true);
parentPlot->legend->setBorderPen(Qt::NoPen);
parentPlot->legend->setFillOrder(QCPLayoutGrid::foColumnsFirst);
parentPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignHCenter | Qt::AlignTop);
}