I am developing a GUI which I use to display some values with a TGraph in a Canvas, and I want to retrieve these values (x,y) when I put the mouse pointer over a specific point. Then, display it using a status bar (although a tooltip may be possible too).
The problem I find is that I can only retrieve the pixel coordinates transformed to canvas coordinates of my pointer, but not the actual value stored in the TGraph I am pointing.
For a histogram I would know roughly how to do it. Just get X coordinate
using :
Double_t x = gPad->PadtoX(gPad->AbsPixeltoX(px));
h->GetBinCenter(h-> FindBin(x))
I have taken a look at :
THistPainter:GetObjectInfo
And looked to a related one for TGraph…
Could you please educate me if there is a way to do it?
Thank you!
Yes, it is close, as close as the precision of your hand , but there is some margin, so I was wondering if there exists a built-in method for it.
A workaround i guess, would be to loop for the closest TGraph point of every pixel, whenever a TGraph is pointed
I have found something similar at:
TGraphPainter::ExecuteEventHelper
case kMouseMotion:
middle = kTRUE;
for (i=0;i<theNpoints;i++) {
pxp = gPad->XtoAbsPixel(gPad->XtoPad(theX[i]));
pyp = gPad->YtoAbsPixel(gPad->YtoPad(theY[i]));
d = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
if (d < kMaxDiff) middle = kFALSE;
}
As you already have the X and U value close to the point, the simplest is surely to loop on the graph’s points and compare them with the X Y value you have within some error box.