Recent

Author Topic: [SOLVED] AxisToGraph, GraphToImage and pixels: confused  (Read 272 times)

kegge13

  • New Member
  • *
  • Posts: 34
[SOLVED] AxisToGraph, GraphToImage and pixels: confused
« on: September 27, 2020, 01:02:01 pm »
I have a graph with one X-axis and two Y-axes. Therefore the Y-axes have an autoscale transform.
In the first stage several curves are plotted.
In the second stage some extra series are filled to overlay a part of the data with a model. Mainly because of historical reasons I chose to do that by filling points in that overlay series. The resolution in the overlay series can be higher than the original data. There is of course no reason at all to plot on a sub-pixel level. Having "steep" curves creates a line with more pixels so this is not trivial. In the attached graph I show the points of the overlay series XXL size.
So I need the dataseries.AxisToGraphX and dataseries.AxisToGraphY function to calibrate pixels_cm and pixels_percent and then decide on a nice data density.

1) When I use dataseries.AxisToGraphX(Xvalue2)-dataseries.AxisToGraphX(Xvalue1) I get just Xvalue2-Xvalue1, which clearly is not a distance in pixels. So I used parentchart.XGraphToImage: same result. What part do I not understand?

2) Using  dataseries.AxisToGraphY(Yvalue2)-dataseries.AxisToGraphY(Yvalue1) produces a sigsev error on the autoscale data not being found (?). Also, this took me by surprise.
As there are multiple Y-axis a parentchart.YGraphToImage would be unresolved I guess.

3) Is it better leave this path and create user-defined functions to fill the overlay series and let the chart decide on the data density.

4) There is also the variant of userdatasources.

Any hints or opinions on this?

Thanks in advance

« Last Edit: September 27, 2020, 03:49:01 pm by kegge13 »

wp

  • Hero Member
  • *****
  • Posts: 7640
Re: AxisToGraph, GraphToImage and pixels: confused
« Reply #1 on: September 27, 2020, 02:52:50 pm »
Three coordinate systems are involved when drawing a chart on the screen:
  • The inner-most coordinates are the pixels on the screen, TAChart calls them image coordinates.
  • The outer-most coordinates are the values plotted on the axes; these are the units of the data values provided by the chart source. In TAChart speak these are called axis coordinates.
  • In between these two systems are the graph coordinates. This is an inivisible linear coordinate system layed over the chart. The connection between graph and image coordinates is made by the chart methods GraphToImage and ImageToGraph. The connection between graph and axis coordinates is defined by axis transformations. The axis transformations provide methods GraphToAxis and AxisToGraph. Normally, where there is no transformation, TAChart uses an identity transformation, i.e. assumes graph and axis coorindates to be equal. When the transformations object has been assigned to an axis you can also call the axis' GraphToAxis and AxisToGraph methods. And when a series has been assigned to an axis (by setting its AxisIndexX and/or AxisIndexY) you can also use the series' methods GraphToAxis and AxisToGraph.
Suppose you have a chart with two y axes, the right one displaying values between 0 and 100, and you want to determine the y pixel coordinates of some y axis value, say 27. Knowing the axis coordinates (27) you must determine the graph coordinate. Since the right axis is subject to an axis transformation you can call Chart.AxisList[2].GetTransform in order to dermine the TAChartTransformations applicable to this axis; this also takes care of the identity transform when no external transformations object is used. Then call the transformations method AxisToGraph to get the graph y value. Finally apply Chart.YGraphToImage to determine the pixel value:

Code: Pascal  [Select][+][-]
  1. uses
  2.   TAChartAxis, TATransformations;
  3.  
  4. function YAxisToPixels(AChart: TChart; AxisIndex: Integer; y: Double):  Integer;
  5. var
  6.   axis: TChartAxis;
  7.   grY: Double;
  8. begin
  9.   axis := AChart.AxisList[AxisIndex];
  10.   grY := axis.GetTransform.AxisToGraph(Y);
  11.   Result := AChart.YGraphToImage(grY);
  12. end;
  13.  
  14. procedure TForm1.Button1Click(Sender: TObject);
  15. begin
  16.   ShowMessage(YAxisToPixels(Chart1, 1, 0.2).ToString);
  17. end;

If this general information does not help you enough to solve the issue you should create a small demo project showing the issue. Pack ,pas, .lfm, lpr, .lpi files to a common zip which you can upload under "Attachments and other options".
« Last Edit: September 27, 2020, 04:36:37 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

kegge13

  • New Member
  • *
  • Posts: 34
Re: AxisToGraph, GraphToImage and pixels: confused
« Reply #2 on: September 27, 2020, 03:48:26 pm »
Three coordinate systems are involved when drawing a chart on the screen:
  • The inner-most coordinates are the pixels on the screen, TAChart calls them image coordinates.
  • The outer-most coordinates are the values plotted on the axes; these are the units of the data values provided by the chart source. In TAChart speak these are called axis coordinates.
  • In between these two systems are the graph coordinates. This is an inivisible linear coordinate system layed over the chart. The connection between graph and image coordinates is made by the chart methods GraphToImage and ImageToGraph. The connection between graph and axis coordinates is defined by axis transformations. The axis transformations provide methods GraphToAxis and AxisToGraph. Normally, where there is no transformation, TAChart uses an identity transformation, i.e. assumes graph and axis coorindates to be equal. When the transformations object has been assigned to an axis you can also call the axis' GraphToAxis and AxisToGraph methods. And when a series has been assigned to an axis (by setting its AxisIndexX and/or AxisIndexY) you can also use the series' methods GraphToAxis and AxisToGraph.

Thanks a lot @WP for explaining this topic, it helps a lot. Maybe I can try to insert this explanation in the TAchart wiki somewhere.

wp

  • Hero Member
  • *****
  • Posts: 7640
Re: [SOLVED] AxisToGraph, GraphToImage and pixels: confused
« Reply #3 on: September 27, 2020, 04:35:42 pm »
I added the explanation to the Runtime FAQ (https://wiki.lazarus.freepascal.org/TAChart_Runtime_FAQ#How_to_find_the_pixel_coordinates_on_the_point_of_the_y_axis.3F).

Please note that the code pasted originally was not 100% exact. I updated the sample code as tested for the wiki.
« Last Edit: September 27, 2020, 04:37:22 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018