### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [Solved] 2 Tchart same x range  (Read 3097 times)

#### coimbras

• New Member
• Posts: 14
##### [Solved] 2 Tchart same x range
« on: June 07, 2018, 06:45:23 pm »
Hello.
I just searching in google, cant find solution...

Is there a way to put the maximum xx in a tchart is now display,in a variable, for use in a second tchart?

I haver two tchar in same window. But the values in the second are a little "shorter" than in the first.
My program reads from disk a table, and display it in a tchart. In other step the user reads other table from disk, and it display in the second chart.

in  the folowing code the maxgrafico1 and mingrafico1 return 0 in the showmessage.

Many thanks.

Code: Pascal  [Select][+][-]
1.   for x := 0 to Length(Tabela1) - 1 do
3.
4.   maxgrafico1:= Chart1.AxisList[1].Range.Max;
5.   mingrafico1:= Chart1.AxisList[1].Range.Min;
6.
7.
8.
9.   ShowMessage( floattostr(mingrafico1) + ' ' + floattostr(maxgrafico1));
« Last Edit: June 08, 2018, 06:17:24 pm by coimbras »

#### wp

• Hero Member
• Posts: 7978
##### Re: 2 Tchart same x range
« Reply #1 on: June 08, 2018, 12:19:43 am »
If I understand correctly you want to know the range of the axes in a chart. The most flexible way, I think, is to use the function LogicalExtent of the Chart. This returns a rectangle defined by corner points a and b - a is the bottom/left corner, b the top/right corner of the area covered by the data. There is also a CurrentExtent which is very similar but includes also the margins around the data areas (property Chart.Margin).  So, in otherwords

- Chart.LogicalExtent.a.x is the minimum x value of all series
- Chart.LogicalExtent.a.y is the minimum y value of all series
- Chart.LogicalExtent.b.x is the maximum x value of all series
- Chart.LogicalExtent.b.y is the maximum y value of all series.

- Chart.CurrentExtent.a.x is the minimum x value of all series minus the left chart margin (converted to "world" coordinates)
- etc.

You can also write to this property, and this way you force an axis to a specific limit. But the Extent is a record, and the chart does not provide a setter for the record elements; therefore you must set the entire Extent record using an intermediate variable. And note: Maximum must always be larger than Minimum!

Example
Code: Pascal  [Select][+][-]
1. uses
2.   TAChartUtils;
3.
4. { TForm1 }
5.
6. procedure TForm1.Button1Click(Sender: TObject);
7. var
8.   ex: TDoubleRect;
9. begin
10.   ex := Chart2.LogicalExtent;
11.   ex.b.y := Chart1.LogicalExtent.b.y;
12.   Chart2.LogicalExtent := ex;
13. end;

Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### coimbras

• New Member
• Posts: 14
##### Re: 2 Tchart same x range
« Reply #2 on: June 08, 2018, 01:07:51 pm »
Well... This issue is problematic.

I have 2 procedures, first drwas chart1, second draws chart2.

In some "codes" i´ve tried if pass the LogicalExtend to double variables, they return -1 1 -1 1 ...
It works as i want in the following code:

Code: Pascal  [Select][+][-]
1.   //Reads Values from Chart1 on the other procedure
2.   //Minx:=ExtensaoGrafico1.a.x;                 //These 2 lines here return -1 1
3.   //Maxx:=ExtensaoGrafico1.b.x;
4.   if (Chart1.LogicalExtent.a.x>-10) and (Chart1.LogicalExtent.b.x<10) then             //Check if "exists" Chart1->Default
5.     begin
6.       ShowMessage('Aplicados valores xx do Gráfico 2');
7.       Minx:=Chart2.LogicalExtent.a.x;
8.       Maxx:=Chart2.LogicalExtent.b.x;
9.     end
10.   else
11.     begin
12.       Minx:=Chart1.LogicalExtent.a.x;
13.       Maxx:=Chart1.LogicalExtent.b.x;
14.     end;
15.
16.   //Miny:=Chart2.LogicalExtent.a.y;              //These 2 lines return -1 1
17.   //Maxy:=Chart2.LogicalExtent.b.y;
18.
19.   ExtensaoGrafico2.a.x:= Minx;
20.   ExtensaoGrafico2.a.y:= Chart2.LogicalExtent.a.y;
21.   ExtensaoGrafico2.b.x:= Maxx;
22.   ExtensaoGrafico2.b.y:= Chart2.LogicalExtent.b.y;
23.   Chart2.LogicalExtent:= ExtensaoGrafico2;

For now it is enough ... i see that some posts have the [SOLVED] how i put it?

Thanks.

#### wp

• Hero Member
• Posts: 7978
##### Re: 2 Tchart same x range
« Reply #3 on: June 08, 2018, 01:25:45 pm »
Can you strip down your project into a simple demo so that I can better understand and play with it? Pack .pas, .lfm, .lpr and .lpi file into a common zip and upload it under "Attachments and other options".

To mark an issue as "solved" edit the title of the first post (you always can edit your own posts).
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### coimbras

• New Member
• Posts: 14
##### Re: [Solved] 2 Tchart same x range
« Reply #4 on: June 08, 2018, 06:39:44 pm »
This is the screen, still not working 100%...

#### wp

• Hero Member
• Posts: 7978
##### Re: [Solved] 2 Tchart same x range
« Reply #5 on: June 08, 2018, 06:51:11 pm »
This is the screen, still not working 100%...
I was thinking of a compilable project. It it still not clear to me what you want to achieve.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### coimbras

• New Member
• Posts: 14
##### Re: [Solved] 2 Tchart same x range
« Reply #6 on: June 11, 2018, 11:17:14 am »
WP:

In the xx axis, i want that two graphics have the same 0 - 1020 numbers.

First i load the upper graphic, from a file that have the values for xx 0- 1020.
Second i load the bottom graphic, from a file tha have the values for xx from 0 - 1250.

But i want that the bottom graphic have the same values in xx that i have in upper. Even for achieve that it is need to cut, or in some cases have "empty values".
The upper is mandatory. The user need to compare directly imagining a vertical line, the curves.

Thats it.

UPDATE:

There is some kind of delay needed... in the following code, similar to that i have posted befor, if not include a ShowMessage('Aplicados valores xx do Gráfico 1') befere read the minimum and maximum yy from chart 1 it return -1 1 for those values. If show the message, the program assume correct values...

Is it too fast for the code?

if (Chart1.LogicalExtent.a.x>-10) and (Chart1.LogicalExtent.b.x<10) then             //Check if "exists" Chart1->Default
begin
ShowMessage('Aplicados valores xx do Gráfico 2');
Minx:=Chart2.LogicalExtent.a.x;
Maxx:=Chart2.LogicalExtent.b.x;
end
else
begin
Minx:=Chart1.LogicalExtent.a.x;
Maxx:=Chart1.LogicalExtent.b.x;
end;

ShowMessage('Aplicados valores xx do Gráfico 1');
Miny:=Chart2.LogicalExtent.a.y;              //These 2 lines return -1 1 if not show message...
Maxy:=Chart2.LogicalExtent.b.y;

ShowMessage('YY' + floattostr(Miny) + floattostr(Maxy));

ExtensaoGrafico2.a.x:= Minx;
ExtensaoGrafico2.a.y:= Miny;
ExtensaoGrafico2.b.x:= Maxx;
ExtensaoGrafico2.b.y:= Maxy;
Chart2.LogicalExtent:= ExtensaoGrafico2;

Thanks.
« Last Edit: June 11, 2018, 11:50:16 am by coimbras »

#### wp

• Hero Member
• Posts: 7978
##### Re: [Solved] 2 Tchart same x range
« Reply #7 on: June 11, 2018, 12:16:47 pm »
I think the easiest way to link the axes of two (or more) charts is to add a TChartLink component to the form. Click on the "..." next to "LinkedCharts". Click "Add" twice, then click each of the two items and select each chart in the object inspector. Then set "Mode" to "elmOnlyX" to link only the x axis.

When you load the data the charts' x axes will automatically synchronize. Even if you zoom (drag a zoom rect with the left mouse button) or drag (with the right mouse button) the axes will stay linked.

Since the labels on the y axis can have different lengths you should set the LeftAxis.LabelSize to the same value for each chart (the default 0 means autocalculation).

Have a look at my attached demo.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### coimbras

• New Member
• Posts: 14
##### Re: [Solved] 2 Tchart same x range
« Reply #8 on: June 11, 2018, 12:25:23 pm »
WP thats awesome.
The zoom is a property of the object, not "coded" also the link.
Great.
Im mya project i dont need zoom or drag but is great idea for future projects.

Thnaks.

#### wp

• Hero Member
• Posts: 7978
##### Re: [Solved] 2 Tchart same x range
« Reply #9 on: June 11, 2018, 02:21:19 pm »
If zooming and panning are undesirable you can turn them off by setting the chart's "AllowZoom" to false (although only "zoom" is mentioned in the name of the property it holds for "drag" as well).
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...