Search code examples
delphiteechart

TeeChart Different between LineSeries XY value and Cursor value on Series Double Click


I'm using basic TeeChart version arrived with Delphi 10.1. A double click on a LineSeries toggle the point and the Mark of this series. I'm also present the cursor value. However, there is a shift between the two, they are not identical.

procedure TfrmMain.SeriesDblClick(Sender: TChartSeries; ValueIndex: Integer;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  tmpX1,tmpY1,tmpX2,tmpY2:Double;
begin
  // First values
  tmpX1:=Chart.Axes.Bottom.CalcPosPoint(X);
  tmpY1:=Chart.Axes.Left.CalcPosPoint(Y);

  // The prevous values are not identical to:
  tmpX2:=(Sender as tLineSeries).XValue[ValueIndex];
  tmpY2:=(Sender as tLineSeries).YValue[ValueIndex];
end;

How to fix it?


Solution

  • It's not going to be possible to resolve this. Information is lost when you map from the real-valued space to integer screen space, and back again.

    CalcPosPoint takes the integer screen coordinate and maps that to the real-valued axis space. On the other hand XValue[] and YValue[] return the original data.

    The chart code starts with the real values in XValue[] and YValue[]. These are mapped to integer values. That mapping by necessity rounds to the nearest integer, after the transformation to real axis values. At that point information is lost, that cannot be retrieved.

    As a thought experiment, consider two real X values that were very close. Let's suppose that XValue[0] = 0.0 and XValue[1] = 1e-6, for the sake of argument. The X axis ranges from 0 to 1 say. Now, in this scenario, you would need 1e6 pixels on the screen in order for these two points to have different integer coordinates when mapped onto the screen. Your screen doesn't have that many pixels and so these two points, with different real X values, map to the same X coordinate on the screen.

    Hopefully this example shows the imposibility of what you are trying to do. Perhaps the best you can achieve is to search the raw coordinates in XValue[] and YValue[] to find the closes to (tmpX1, tmpY1), and report those closest raw coordinate values.