QCustomPlot Discussion and Comments

Is there a way for getting graph data for a specific coords?Return to overview

I have two graphs in one plot. I need a y-axis value for a specific x-axis value. Is there a function for that?

all of data for a graph is stored in QCPDataMap. so you can get this map by QCPGraph::data() method and find your value or key in this map. even though if your value is not exact and you want to find nearest value of specific key, you can easily use lowerbound method of QMap.

Okay, I'm a little stuck with this. I'm trying to select a plot and see the data for it.

So doing this

const QCPDataMap *dataMap = ui->customPlot->selectedGraphs().first()->data();

gives me a pointer to the data for the plot, which is stored in a QMap? Is the key the x axis value for each point?

QCPDataMap consists a key and a QCPData( which holds key and value).
for a horizontal plot the key is x axis value and the value is y axis value. for example for a real time plot (horizontal) the key is dateTime and value is your value for that dateTime for given graph.

Thank you for your help but I am still. My goal is to allow a user to select a plot and re-scale the axes based on the selected data. I was thinking I could iterate through and determine min and max. With a QVector this is easy to do.

    QVector<double>::iterator maxY = std::max_element(y.begin(), y.end());
    QVector<double>::iterator minY = std::min_element(y.begin(), y.end());

With a QMap how can I iterate through and get all the data if the keys are unknown?

I am new to Qt so maybe this is very obvious to someone more experienced. But I am lost.

First of all, please note that selectedGraphs() may return an empty list. So calling

ui->customPlot->selectedGraphs().first()->data();

as you do now is dangerous. Qt will stop your program with an assert, if you try to access first() of an empty list.

To your problem:
If you want to rescale the axes according to a single graph (view its entire data set), call theGraph->rescaleAxes() (see documentation, there are also the functions rescaleKeyAxis and rescaleValueAxis). As you've found out you can get the selected graph via selectedGraphs(), but please check that it's not empty before accessing .first().

If you still want to iterate the data points, have a look at the Qt documentation about QMap. It will explain how to iterate over maps in Qt.

Yes, I was making sure count != 0. I think was just getting tripped up with a QMap in a QMap and what the difference was between the key in QCPDataMap and in QCPData.

Anyway. Here is some example code to dump the plot data from a selected line.

if(ui->customPlot->selectedGraphs().count() != 0)
{
	const QCPDataMap *dataMap = ui->customPlot->selectedGraphs().first()->data();
	QMap<double, QCPData>::const_iterator i = dataMap->constBegin();
	while (i != dataMap->constEnd()) {
		qDebug() << i.key()<< ": " << i.value().key << ": " << i.value().value << endl;
		++i;
	}
}

Just for clarification: There is no QMap in a QMap here. QCPDataMap is merely a typedef (a declared synonym) of QMap<double, QCPData>.
And QCPData is just a package of variables that make a data point. This is also the reason why the x coordinate is somewhat redundantly saved – once in the key of the QMap, and once as "key" variable in QCPData. QCPData must be self-consistent and thus contain both key and value coordinates. At the same time, QMap must know by what value to sort/search/access the data points.

However, your confusion made me notice that the documentation of the typedef QCPDataMap isn't properly processed by doxygen (the documentation generator). With correct documentation you'd have figured it out easier, I guess... sorry.

Use QCPItemTracer.

QCPItemTracer *tracer = new QCPItemTracer;
tracer->setGraph(graph);

double getValueByKey(double key)
{
    tracer->setGraphKey(key);
    return tracer->position->value();
}