Search code examples
qtqt6qtcharts

How to automatically set range based on maximum value of visible points in QChart?


I'm trying to plot a chart where it sets Y axis range from 0.0 to maximum value in plot. However I do not want to set the range maximum to be maximum value of all data, because I'm not plotting all data, but the last 10 seconds of the incoming data. How can I achieve that? I know how to set range maximum of all data inserted to series, but that is not what I want to achieve. In this example the maximum is set taking in all the incoming data -- this is not what I want. I want my plot to automatically adjust to whatever is the maximum plotted Y value of all visible points, so points in chart area.

I set the X axis range to show only last 10 seconds. I want to set the range of Y axis to stop at maximum of actually plotted points. Is there a way to get maximum value of only plotted points that are in QLineSeries?


Solution

  • got it working by looping over all data from the series

    QDateTime maxX = QDateTime::currentTime();
    int seconds = QTime(0, 0, 0).secsTo(10);
    QDateTime minX = maxX.addSecs(0-seconds);
    
    qreal minY = std::numeric_limits<qreal>::max();
    qreal maxY = std::numeric_limits<qreal>::min();
    
    for(int i=0;i<mySeries->points().size();i++)
    {
        if(mySeries->points().at(i).x() > minX.toMSecsSinceEpoch()
            && mySeries->points().at(i).x() < maxX.toMSecsSinceEpoch())
        {
            if(mySeries->points().at(i).y() < minY)
                minY = mySeries->points().at(i).y();
            if(tmySeries->points().at(i).y() > maxY)
                maxY = mySeries->points().at(i).y();
        }    
    }