Mapping real world to canvas coordinates

I'm using a TadvGDIPChartView with a line chart to plot scientific data. I need to be able to add non data lines to these charts, so I'm coding this in the OnDrawChart method. But I'm having great difficulty finding how to map a real world value to a value that can be plotted on the canvas.

What I need is to be able to take an XY value, where X is a data point index, and Y is any value on the chart, not necessarily an actual data point on the chart line, and return the canvas XY value so that I can plot my own lines. Something like MapPointToCanvasPoint(X, Y : double): TPoint  Obviously, this should take into acount the displayed range and any scaling, etc.
Also, I need the reverse of this. Given an XY point on the canvas, I need to return the data index and the real world value for Y - again, this may not be on the chart line, it could easily be above or below, and again, display ranges and scaling etc. need to be accounted for.
I'm sure this is already provided, but the manuals don't provide any information, and the terminology used for function names isn't particularly clear to enable me to find these functions.


To map real points to canvas points you need to use the function ValueToY and to reverse YToValue. for example:

AdvChartView1.Panes[0].Series[0].ValueToY(100, AdvChartView1.Panes[0].Series.SeriesRectangle) gives you to based on the settings for the first serie. You can also access the XScale and YScale properties separately. ValueToX and XToValue does not exist since the X-Axis is based on the seriesrectangle.Width divided by the XScale and you can automatically assume the first point is drawn at XScale or XScale/2 depending on the type of chart.

Kind Regards, 
Scheldeman Pieter

Thanks for the quick reply.  I think I've got the Y points sorted out - when I resize the chart and scale the Y axis, my line in the Y direction draws correctly.  However, I must be doing something wrong in the X as when I reduce the width of the chart, my line doesn't get drawn at the correct X position. Making the chart narrower causes the X points to be drawn at too small a value.

For example, I'm drawing a test line that has X1 at 25% of the data points and X2 at 50%  (18,000 data points so X1 = 4500, X2 = 9000). With a large chart, this appears to draw correctly, but as I shrink the chart down in the X direction, X1 is drawn at approximately 3000 and X2 at approximately 7000. This is just resizing the chart, not scaling or scrolling the axis.

I'm calculating X1 and x2 in the OnDrawChart method using

x1 := Round(4500 * chart.XScale);
x2 := Round(9000 * chart.XScale);

Am I missing an offset or something here?


OK, figured it out. Just in case anybody else is searching for this. The canvas X and Y values are calculated from

DR : TRect;

DR := AdvChartView.Panes[0].Series.SeriesRectangle;;
canvasX := Round(realXValue * AdvChartView.Panes[0].XScale) + DR .Left;
canvasY := AdvChartView.Panes[0].Series[0.ValueToY(realYValue, DR);