QCustomPlot Discussion and Comments

Bug with drawing on a logarithmic graphReturn to overview

Hi -

I am trying to draw a intersection line across a graph for a given cursor position. In the image below I am getting the correct result. The black vertical line is the cursor position. The red horizontal line is the value of the graph data at the cursor. The red and black lines should intersect on the graph as it does in the image here: https://ibb.co/bys9iQ

The bug seems to be when I move the cursor to the left such that the left data point is off the graph but the right data point is on the graph. In this case the red and black lines do not intersect on the graph as drawn. The image is here: https://ibb.co/kpeN3Q

Anyone have any suggestions on what I might be doing wrong or hints on how I could fix it?

The drawing code snipped:

		double dependentIntersection = FindGraphDependentIntersection(targetGraph->data().data(), xAxisCoord);
		targetLine->start->setCoords(-1e20, dependentIntersection);
		targetLine->end->setCoords(1e20, dependentIntersection);

The code to compute the intersection point:

	QCPGraphDataContainer const*	inData,
	double							inIndependentValue)
	int	low = 0;
	int	high = inData->size();
	int mid;

	if(inIndependentValue <= inData->at(0)->mainKey())
		return inData->at(0)->mainValue();

	if(inIndependentValue >= inData->at(high - 1)->mainKey())
		return inData->at(high - 1)->mainValue();

	while(low < high)
		mid = (low + high) >> 1;

		double midVal = inData->at(mid)->mainKey();

		if(inIndependentValue < midVal)
			high = mid;
		else if(inIndependentValue > midVal)
			low = mid + 1;
			return inData->at(mid)->mainValue();

	double	lowValue = inData->at(low - 1)->mainValue();
	double	lowKey = inData->at(low - 1)->mainKey();
	double	highValue = inData->at(low)->mainValue();
	double	highKey = inData->at(low)->mainKey();

	double interp = (inIndependentValue - lowKey) / (highKey - lowKey);

	double result = lowValue + (highValue - lowValue) * interp;

	//MLog("inIndependentValue=%g lowKey=%g lowValue=%g highKey=%g highValue=%g interp=%g result=%g", inIndependentValue, lowKey, lowValue, highKey, highValue, interp, result);

	return result;


My first guess is that you're linearly interpolating in coordinate space, however the lines of QCPGraph are drawn in screen coordinates, which are not logarithmic. So essentially your interpolation follows an apparent exponential, when using logarithmic axes.