c++qtgraphtrendlineqcustomplot

How to add a trendline to my graph in Qt?


I want to add a trendline to my graph in Qt.

How to plot a trendline on a line graph?


Solution

  • Here's how to compute a trendline mathematically:

    Slope:

    enter image description here

    Offset:

    enter image description here

    Trendline formula:

    enter image description here

    where

    a is slope
    x is the horizontal axis value
    b is the Y-intercept
    

    Normally you have sccattered data that makes up your graph. No matter if you use QtWidget or QML you can follow these steps.

    Then you have everything you want to apply your trendline equation

    Using your equation, you could now draw your line trend on your QCustomPLot using two point:

    QCPItemStraightLine *trendLine = new QCPItemStraightLine(customPlot);
    trendLine->point1->setCoords(x, y);  // location of point 1 in plot coordinate
    trendLine->point2->setCoords(xx, yy);  // location of point 2 in plot coordinate