Recent

Author Topic: clicking a chart, get x-axis value  (Read 4342 times)

mtanner

  • Sr. Member
  • ****
  • Posts: 287
clicking a chart, get x-axis value
« on: September 12, 2017, 12:26:23 pm »
What is the easiest way of allowing the user to click on a chart and getting the x-axis position for the point clicked. The clicked point may not coincide with a data point for ay of the series displayed.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: clicking a chart, get x-axis value
« Reply #1 on: September 12, 2017, 12:58:06 pm »
Use the DatapointClickTool: http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Chart_Tools#Using_a_TDataPointClickTool

If the click should be evaluated also if no datapoint is hit you can use the ordinary OnClick, or OnMouseDown event of the chart. Use Chart.ImageToGraph to convert the pixel coordinates to graph coordinates:

Code: Text  [Select][+][-]
  1. procedure TForm1.Chart1Click(Sender: TObject);
  2. var
  3.   P: TDoublePoint;
  4. begin
  5.   P := Chart1.ImageToGraph(Chart1.ScreenToClient(Mouse.CursorPos));
  6.   Statusbar1.SimpleText := Format('X=%.2f, Y=%.2f', [P.X, P.Y]);
  7. end;  

But note that this works only if you did not use ChartTransformations. In this case you must decide which axes will be used, then call axis.GetTransform.GraphToAxis to further convert the chart's graph coordinates to the units displayed on the axis. Here with the example of the x axis:

Code: Text  [Select][+][-]
  1. procedure TForm1.Chart1Click(Sender: TObject);
  2. var
  3.   P: TDoublePoint;
  4.   x: Double;
  5. begin
  6.   P := Chart1.ImageToGraph(Chart1.ScreenToClient(Mouse.CursorPos));
  7.   x := Chart1.BottomAxis.GetTransform.GraphToAxis(P.X);
  8.   StatusBar.SimpleText := Format('X = %.2f', [x]);
  9. end;

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: clicking a chart, get x-axis value
« Reply #2 on: September 13, 2017, 08:59:38 am »
Thanks, that works fine. I don't use transformations, so the simple approach does what I need.

 

TinyPortal © 2005-2018