【QCustomPlot实战系列】QCPGraph渐变堆叠面积图

渐变图很简单,在Qt中线性渐变使用QLinearGradient即可实现,曲线图可以参考
QCustomPlot之平滑曲线下(九)
QCPGraph渐变堆叠面积图

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值