### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: TAChart - add, but "inside" the other series  (Read 362 times)

#### Nicole

• Hero Member
• Posts: 507
##### TAChart - add, but "inside" the other series
« on: September 30, 2022, 03:26:52 pm »
We have a TAChart, which's series are added at runtime by

etc.

Those series have all in common: The x-axes, which contains the date.

It is added nicely, - on the right side of the existing series, with a double populated x-aches.

How can I say,
"add some y values to the existing(!) x-axes date"?

#### wp

• Hero Member
• Posts: 10297
##### Re: TAChart - add, but "inside" the other series
« Reply #1 on: September 30, 2022, 04:06:21 pm »

#### Nicole

• Hero Member
• Posts: 507
##### Re: TAChart - add, but "inside" the other series
« Reply #2 on: September 30, 2022, 04:38:28 pm »
What the code does (many bugs and typos to do, pls do not tell me, I know it):

Pls mind the fk1 or fk2 or fk3.
fk for "foreign key" are the keys to SQL-series.
The first query takes 3 SQL-series in one request.

In the second query takes only 1 SQL-seried for fk1 and shall draw the candlestick-chart-series.
The second series ("inside") is added after the German comment, which reads
Code: Pascal  [Select][+][-]
1. "  // zweite Query wird gestaltet, die den Kontrakt 1 ins Chart zeichnet"

Code: Pascal  [Select][+][-]
1. procedure TFrame_oi.Zeichne_oi(fk1, fk2, fk3: integer);
2. Var s: string;
3.     openinterest, volumen: integer;
4.     Baranzahl: integer;
5. //    kontrak1, kontrak2, kontrakt3: TEinKontrakt;
6.     dat, dat_alt, dat2, dat1: TDateTime;
7.     d: double;
8.
9. begin
10.   Baranzahl:=StrToInt(LabeledEdit_oiBaranzahl.Caption);
11.   dat2:=DB_Kontrakte.ReadSQL_tbKontrakteLetztesDatum(now); // dat2 ist das letzte verfügbare Datum
12.   dat1:=IncDay(dat2, - Baranzahl);   // 20 Tage sind ca 1 Monat, dat 1 = erstes einzulesendes Datum
13.   dat_alt:=dat1;
14.
15.
16.   if fk1 <> -1 then
18.   Chart_oi.Title.Text.Text:='Liquidität der Kontrkate um ' + s + ' // Volumen ► blau, oi ► rot, ratio ► fett (linke Skala)';  // fett ging nicht mit <b>ratio</b>
19.   if fk3 = -1   // mit den anderen beiden müsste ich die Ware abfragen
20.   then showMessage('Frame_oi.Zeichne_oi damit habe ich noch wenig Erfahrung, hier fehlt jedenfalls der 3. Fremdschüssel');
21.
22.   s:='Select FK_JDATUM, VOLUME, openinterest From TBKURSZEILEN A ' +
23.       'where FK_JDATUM > :datum '+
24.       'and (FK_KONTRAKT = :fk1 or FK_KONTRAKT = :fk2 or FK_KONTRAKT = :fk3) ' +
25.        'order by fk_JDatum ';
26.
27.  // wenn eines davon -1 wäre Abbruch
28.   IBQuery_oi.SQL.Text:=s;            // die id der 3 Kontrakte und das Datum
29.   IBQuery_oi.ParamByName('datum').AsDate:=dat1;
30.   IBQuery_oi.ParamByName('fk1').AsInteger:=fk1;
31.   IBQuery_oi.ParamByName('fk2').AsInteger:=fk2;
32.   IBQuery_oi.ParamByName('fk3').AsInteger:=fk3;
33.
34.   IBQuery_oi.Active:=true;
35.
36.   Chart_oiLineSeries_oi.Clear;
37.   Chart_oiLineSeries_vol.Clear;
38.   Chart_oiLineSeries_ratio.Clear;
39.   openinterest:=0;
40.   volumen:=0;
41.   while not IBQuery_oi.EOF do begin // openinterest und volumen werden aus allen 3 Kontrakten addiert
42.
43.     dat:=IBQuery_oi.FieldByName('FK_JDATUM').AsDateTime;
44.     if dat=dat_alt then begin
45.         openinterest:=openinterest + IBQuery_oi.FieldByName('openinterest').AsInteger;
46.         volumen:=volumen + IBQuery_oi.FieldByName('VOLUME').AsInteger;
47.     end
48.     else begin
49.         //  die vorigen Werte werden gezeichnet
50.         if volumen <> 0 then begin
54.           d:=openinterest / volumen;
56.         end;
57.
58.         dat_alt:=dat;  // es wird neu gerechnet und neu zugewiesen
59.         openinterest:=IBQuery_oi.FieldByName('openinterest').AsInteger;
60.         volumen:=IBQuery_oi.FieldByName('VOLUME').AsInteger;
61.     end;
62.
63.     IBQuery_oi.Next;
64.   end;
65.
66.   // zweite Query wird gestaltet, die den Kontrakt 1 ins Chart zeichnet
67.   IBQuery_oiEinKontrakt.SQL.Text:='Select first 50 ID_KURSZEILEN, FK_JDATUM, FK_KONTRAKT, '+
68.      'O, H, L, C From TBKURSZEILEN where FK_KONTRAKT = :fk order by FK_JDATUM asc';
69.   IBQuery_oiEinKontrakt.ParamByName('fk').AsInteger:=fk1;
70.   IBQuery_oiEinKontrakt.Active:=true;
71.
72.   while not IBQuery_oiEinKontrakt.EOF do begin
74.          IBQuery_oiEinKontrakt.FieldByName('FK_JDATUM').AsDateTime,
75.          IBQuery_oiEinKontrakt.FieldByName('O').AsFloat,
76.          IBQuery_oiEinKontrakt.FieldByName('H').AsFloat,
77.          IBQuery_oiEinKontrakt.FieldByName('L').AsFloat,
78.          IBQuery_oiEinKontrakt.FieldByName('C').AsFloat
79.          );
80.    IBQuery_oiEinKontrakt.Next;
81.
82.   end;
83.
84. end;

If you ask yourself, why I do it this way and not simpler:
Those SQL-series have different length. Where fk1 has data, fk3 has none.
We do not know, which day exists for which foreign key.

#### wp

• Hero Member
• Posts: 10297
##### Re: TAChart - add, but "inside" the other series
« Reply #3 on: September 30, 2022, 05:32:16 pm »
I am attaching a test project showing that there is no bug in TAChart: A lineseries and an ohlc series with common date values for x are populated by random y values one after each other, but they are displayed correctly on the same axis.

So, I don't know what's wrong in your case. The one thing that I noticed is that you do not clear the Chart_oi_Candles series unlike the others before adding the new data (but this would not append the newly added data to the x axis).
« Last Edit: September 30, 2022, 06:26:54 pm by wp »

#### Nicole

• Hero Member
• Posts: 507
##### [solved] Re: TAChart - add, but "inside" the other series
« Reply #4 on: September 30, 2022, 06:22:39 pm »
"clear" was a very useful hint and I added it.
Nevertheless it did not cause the problem.

I am afraid, TAChart may have drawn., -  what I keyed in. Those query may have reached too far into the past. So the problem is solved by SQL in that way

select.... from... where ... datum > dat1