QtCharts is it posible to have gradient lines?



  • Hi,

    Could I achieve this kind of spline chart with QTCharts, where color changes according to y chart value?

    example:
    alt text


  • Qt Champions 2017

    Hi
    Maybe you can use
    https://doc.qt.io/qt-5.10/qtcharts-customchart-example.html
    and draw the gradient as background and let graph be transparent on the actual
    plot values.

    alt text



  • Ok, I will try to do that, thanks



  • How to make line be transparent like in that picture? Because if I set it like this:

    QPen green(QColor(0, 0, 0, 0x00));
    green.setWidth(2);
    m_series->setPen(green);

    line just becomes invisible. If I set chart background like this:
    QBrush brush (QImage(":image/image.png"));
    chart->setPlotAreaBackgroundBrush(brush);
    chart->setPlotAreaBackgroundVisible(true);
    whole chart area then has that image and line is still invisible. if I setPlotAreaBackgroundVisible(false) background disappears, but line is still invisible. How to have, that only chart line shows that background?



  • So it means it's impossible to achieve this with QT charts?



  • Starting from the aforementioned example, replace

    // Customize series
    QPen pen(QRgb(0xfdb157));
        pen.setWidth(5);
        series->setPen(pen);
    

    with

    QLinearGradient lineGradient;
        lineGradient.setStart(QPointF(0, 1));
        lineGradient.setFinalStop(QPointF(1, 0));
        lineGradient.setColorAt(0.0, Qt::blue);
        lineGradient.setColorAt(1.0, Qt::red);
        lineGradient.setCoordinateMode(QGradient::StretchToDeviceMode);
        series->setPen(QPen(QBrush(lineGradient),5));
    


  • Hi VRonin,

    Thank you a lot, that works perfectly!!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.