Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QtCharts recalculate/update axis for new series values



  • Hi

    I have a slot function updatePlot(...) that receives a QVector<double> and iterates through the vector data
    and fills a series which has been cleared prior.

    {
    ...
        ls = new QLineSeries();
        chart = new QChart();
        xAxis = new QValueAxis;
        yAxis = new QValueAxis;
        cv = new QChartView(chart);
    
        chart->addSeries(ls);
        chart->addAxis(xAxis, Qt::AlignBottom);
        chart->addAxis(yAxis, Qt::AlignLeft);
        ls->attachAxis(xAxis);
        ls->attachAxis(yAxis);
    ...
    }
    ...
    void PlotWindow::updatePlot(const QVector<double> &dat)
    {
        ls->clear();
        for(int i=0;i<dat.size();i++){
    	ls->append(i,dat.at(i));
        }
    }
    

    The problem of course is now that the data changes x and y ranges every time, but the axes
    stay at the same range. What is the proper way to recalculate the x/y axes ranges?

    Do I have to take care of it and calculate the high/low values in the for loop or is there a function in the qtcharts that does this for me?



  • so for now I use this which seems to work...

    xAxis->setRange(0,dat.size());
    yAxis->setRange(*std::min_element(dat.constBegin(),dat.constEnd()),*std::max_element(dat.constBegin(),dat.constEnd()));