QCustomPlot Discussion and Comments

finding a min and max in a GraphReturn to overview

Hi there,
I am trying to find the min and max values in the Graph.
To do that I use a Lambda:

QCPData min = *std::min_element(ui->plot->graph(0)->data()->begin(), ui->plot->graph(0)->data()->end(),[](QCPData const & s1, QCPData const & s2) {return s1.value < s2.value;});

//QCPData max = *std::max_element(ui->plot->graph(0)->data()->begin(), ui->plot->graph(0)->data()->end(),[](QCPData const & s1, QCPData const & s2) {return s1.value > s2.value);});

But then min.value and max.value have exactly the same value, wich cannot be. Am I using it correctly? I am using 1.3.2 version.
Also can I find the documentation for this version somewhere? Because in the Documentation link in this website I cannot find the QCPDataMap class anylonger - as I see it has been replaced in beta 2 . Right?

Yes the data container have change !

If you still use the version 1.3.2, you can use thoses functions :

For the first value in the xAxis :
ui->plot->graph(0)->data()->firstKey();

For the last value in the xAxis :
ui->plot->graph(0)->data()->lastKey();

but if you want the min/max value in the yAxis

QCPData max = *std::max_element(ui->plot->graph(0)->data()->values()->begin(), ui->plot->graph(0)->data()->values()->end())

(in your code, you forget about the values() !!!)

Hi there. thank you for your response.

QCPData max = *std::max_element(ui->plot->graph(0)->data()->values()->begin(), ui->plot->graph(0)->data()->values()->end())

doesn ot seem to work. The error is

formgraph.cpp:233: error: conversion from 'QList<QCPData>::iterator' to non-scalar type 'QCPData' requested
QCPData min = std::min_element(ui->plot->graph(0)->data()->values().begin(), ui->plot->graph(0)->data()->values().end(),[](QCPData const & s1, QCPData const & s2) {return s1.value < s2.value;});

If I try to dereference the obtained value by adding an asterisk in from of min_element as: QCPData min = *std::min_element(...) , I manage to compile , but then the application segfaults (probably because I am trying to access a QList<QCPData> object as a QCPData ovject...

As you can see I have also implemented a comparator as Lambda because without it I would also get the error:

/usr/include/c++/4.8/bits/stl_algo.h:6240: error: no match for 'operator<' (operand types are 'QCPData' and 'QCPData')
if (*__first < *__result)

Any ideas (or access to the old documentation?)

Thank you for your help

Instead of QCPData, have you try to put the result in a QVector<double> ?!

If you want the old documentation, I guess you can just download the old archive of the QCustomPlot and the documentation will be inside (the archive)

Hi,
This worked for me:

bool mycom(QCPData const & s1, QCPData const & s2)
{
return s1.value < s2.value;
}


void MainWindow::on_pushButton_2_clicked()
{
QCPData min = *std::min_element(ui->plot->graph(0)->data()->begin(), ui->plot->graph(0)->data()->end(), mycom);

QString s;

s.sprintf("minimum is at: %lf, value of %lf", min.key, min.value);
ui->label->setText(s);
}