Draw custom value on y axis

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Gucci
Newbie
Newbie
Posts: 22
Joined: Mon Jul 27, 2015 12:00 am

Draw custom value on y axis

Post by Gucci » Wed Aug 05, 2015 11:36 am

Good day,

I am trying to draw my closing value from by candle stick on the Y axis, but with no luck I am getting it right.

I am trying to accomplish the following.
1) By default when my chart is loaded I want to highlight on the Y axis the highest closing value in different color.

2) Secondly if possible can I draw also on the y axis the current closing value if the mouse move over a series.

I already have the closing value on mouse move.

Here is a screen shot with my idea.

I hope this can help.
draw y axis.png
Draw custom value on Y axis.
draw y axis.png (236.9 KiB) Viewed 17301 times

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Draw custom value on y axis

Post by Narcís » Wed Aug 05, 2015 12:11 pm

Hi Gucci,

You can achieve what you request using Annotation tools set to a custom position. You can drop a TChart component, add a Candle series and two Annotation tools to it and use the following code:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=False;

  Series1.FillSampleValues();

  ChartTool1.Text:=FloatToStr(Series1.CloseValues.MaxValue);
  ChartTool1.Shape.Transparent:=True;
  ChartTool1.Shape.Font.Color:=clRed;

  ChartTool2.Active:=False;
  ChartTool2.Shape.Transparent:=True;
  ChartTool2.Shape.Font.Color:=clBlue;

  Chart1.Draw;
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
  SetAnnotationPosition(ChartTool1);
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var Index : Integer;
begin
  Index:=Series1.Clicked(X, Y);
  ChartTool2.Active:=Index<>-1;

  If ChartTool2.Active then
  begin
    ChartTool2.Text:=FloatToStr(Series1.CloseValues[Index]);
    SetAnnotationPosition(ChartTool2);
  end;
end;

procedure TForm1.SetAnnotationPosition(Annotation: TAnnotationTool);
var Value : Double;
    XPos  : Integer;
    YPos  : Integer;
begin
  Value:=StrToFloat(Annotation.Text);

  XPos:=Chart1.Axes.Bottom.IStartPos - Annotation.Width -
        Chart1.Axes.Left.TickLength - 2;
  YPos:=Chart1.Axes.Left.CalcPosValue(Value) - (Annotation.Height div 2);

  Annotation.Shape.CustomPosition:=True;
  Annotation.Shape.Left:=XPos;
  Annotation.Shape.Top:=YPos;
end;
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Gucci
Newbie
Newbie
Posts: 22
Joined: Mon Jul 27, 2015 12:00 am

Re: Draw custom value on y axis

Post by Gucci » Wed Aug 05, 2015 1:06 pm

Hi,

I dont know what to say, but it is beautiful.

Thanx you so much.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Draw custom value on y axis

Post by Narcís » Wed Aug 05, 2015 1:10 pm

Hi Gucci,

You're very welcome! :D
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Gucci
Newbie
Newbie
Posts: 22
Joined: Mon Jul 27, 2015 12:00 am

Re: Draw custom value on y axis

Post by Gucci » Tue Aug 25, 2015 5:42 am

Good day support,

Sorry to keep coming back to this, but I am starting to completely loose it with myself. my problem is as follows :

I dynamically create a new function each time and a TAnnotationTool. I am trying on my SetAnnotationPosition to set each closing value to each custom axis created, but this is were I am completely loosing the plot.

I have tried all different ways but cant get it right. Can you please see if you can determine if this is possible.
sample.png
sample.png (113.83 KiB) Viewed 17190 times
Attachments
chart dynamic.zip
(12.34 KiB) Downloaded 752 times

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Draw custom value on y axis

Post by Yeray » Tue Aug 25, 2015 3:39 pm

Hello,

First of all note you start creating a TCandleSeries with a CustomAxis assigned to it. This is fine if you want but later you are trying to synchronize tools with custom axes and this would be cleaner if you have as many custom axes as annotation tools. So I start removing the initial custom axis assigned to the TCandleSeries.

I also removed the SetAnnotationPosition method and I'm recalculating the positions of the annotations at the calculateaxis. I'm doing this way because the new positions of the axes should also alter the positions of all the annotations.

Here how it looks:
2015-08-25_1735.png
2015-08-25_1735.png (72.67 KiB) Viewed 17161 times
And the project modified:
chart dynamic.zip
(2.93 KiB) Downloaded 813 times
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Gucci
Newbie
Newbie
Posts: 22
Joined: Mon Jul 27, 2015 12:00 am

Re: Draw custom value on y axis

Post by Gucci » Tue Aug 25, 2015 4:48 pm

Capture.JPG
Capture.JPG (84.81 KiB) Viewed 17160 times
Hi Yeray,

Thank you much for the nice and clean solution, what do you suggest I must do with the CandleSeries closing value. Must I create a seperate calculation or do you suggest I use your current calculateaxis to add that value in.

thank you again.

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Draw custom value on y axis

Post by Yeray » Wed Aug 26, 2015 8:20 am

Hello,

I see Narcís suggested you to have two extra TAnnotationTools, one always visible (showing the maximum close value) and one visible when the mouse is over the candle series (showing the close value of the candle under the mouse).
That code wasn't present on the project you attached so I skipped that part, but of course you can re-add it. Just note these two extra annotation tools will also be in Chart1.Tools array, so you'll have to take care with the indexes at calculateaxis (use Chart1.Tools.Items[i+2] instead of Chart1.Tools.Items).

Don't hesitate to let us know if you find problems with it.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Gucci
Newbie
Newbie
Posts: 22
Joined: Mon Jul 27, 2015 12:00 am

Re: Draw custom value on y axis

Post by Gucci » Wed Aug 26, 2015 10:23 am

Hi Yeray

I am not 100% sure If I understand your solution, is it possible you can alter your last example attached.

Thank you so much

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Draw custom value on y axis

Post by Yeray » Wed Aug 26, 2015 10:40 am

Hi,

Take a look at it:
chart dynamic.zip
(3.22 KiB) Downloaded 792 times
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Gucci
Newbie
Newbie
Posts: 22
Joined: Mon Jul 27, 2015 12:00 am

Re: Draw custom value on y axis

Post by Gucci » Wed Aug 26, 2015 11:53 am

Thank you so much. Money well spend on a great tool and support.

Post Reply