QCustomPlot Discussion and Comments

How to get the maximum and minimum value of a graph?Return to overview

Hello I need to get the maximum value of the visible part of a graph. I'm deleting and adding new data constantly to the graph so I want to know every time I modified the graph array the maximum value. How can I do that?

Kind regards.

Try something like that for max/min/mean/stddev

for(int k =0;k<m_plot->graphCount();k++)
        {
            QCPGraphDataContainer::const_iterator begin;
            QCPGraphDataContainer::const_iterator end;
            QCPDataRange range = m_plot->graph(k)->data()->dataRange();
            
            m_plot->graph(k)->getVisibleDataBounds(begin,end,range);            
                       
            const QCPGraphData* maxData = std::max_element(begin,
                                                           end,
                                                           [](QCPGraphData a, QCPGraphData b) { return a.value < b.value; });
            
            
            
            const QCPGraphData* minData = std::min_element(begin,
                                                           end,
                                                           [](QCPGraphData a, QCPGraphData b) { return a.value < b.value; });
            
            double max = maxData->value;
            double min = minData->value;
            const double mean = std::accumulate(begin,
                                                end,
                                                .0,
                                                [](double a, QCPGraphData b) { return a + b.value;}) / std::distance(begin, end);
            
            std::vector<double> diff(std::distance(begin, end));
            std::transform(begin, end, diff.begin(), [mean](QCPGraphData x) { return x.value - mean; });
            double var = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0) /  std::distance(begin, end);
            double stdev = std::sqrt(var);
        }