Question
“How can I calculate the x,y position of the chart when clicked in the viewer?”
Solution
The can be accomplished by implementing the Chart.OnDrawCommandClick event to convert the mouse position to chart coordinate space.
Download: CalcChartPosClicked.zip
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
procedure TForm1.ehTeeChart_DrawCommandClick(Sender, aDrawCommand: TObject); var lViewer: TppViewer; lCursorPos: TPoint; lDrawCommandPos: TPoint; lChartPos: TPoint; begin // get reference to report viewer lViewer := TppViewer(ppReport1.PreviewForm.Viewer); // get mouse pos GetCursorPos(lCursorPos); // convert to viewer coords lCursorPos := lViewer.PaintBox.ScreenToClient(lCursorPos); // get pos of the chart image lDrawCommandPos := TppDrawCommand(aDrawCommand).DrawRect.TopLeft; // get chart pos clicked lChartPos.X := lCursorPos.X - lDrawCommandPos.X; lChartPos.Y := lCursorPos.Y - lDrawCommandPos.Y; // adjust for viewer scaling (i.e. viewer support zoom) lChartPos.X := Round(lChartPos.X / lViewer.ScreenDevice.ScaleX); lChartPos.Y := Round(lChartPos.Y / lViewer.ScreenDevice.ScaleY); ShowMessage('Chart clicked: ' + IntToStr(lChartPos.X) + ',' + IntToStr(lChartPos.Y)); end; |