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
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;