Page 1 of 1

Draw custom value on y axis

Posted: Wed Aug 05, 2015 11:36 am
by 16475102
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 17110 times

Re: Draw custom value on y axis

Posted: Wed Aug 05, 2015 12:11 pm
by narcis
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;

Re: Draw custom value on y axis

Posted: Wed Aug 05, 2015 1:06 pm
by 16475102
Hi,

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

Thanx you so much.

Re: Draw custom value on y axis

Posted: Wed Aug 05, 2015 1:10 pm
by narcis
Hi Gucci,

You're very welcome! :D

Re: Draw custom value on y axis

Posted: Tue Aug 25, 2015 5:42 am
by 16475102
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 16999 times

Re: Draw custom value on y axis

Posted: Tue Aug 25, 2015 3:39 pm
by yeray
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 16970 times
And the project modified:
chart dynamic.zip
(2.93 KiB) Downloaded 801 times

Re: Draw custom value on y axis

Posted: Tue Aug 25, 2015 4:48 pm
by 16475102
Capture.JPG
Capture.JPG (84.81 KiB) Viewed 16969 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.

Re: Draw custom value on y axis

Posted: Wed Aug 26, 2015 8:20 am
by yeray
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.

Re: Draw custom value on y axis

Posted: Wed Aug 26, 2015 10:23 am
by 16475102
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

Re: Draw custom value on y axis

Posted: Wed Aug 26, 2015 10:40 am
by yeray
Hi,

Take a look at it:
chart dynamic.zip
(3.22 KiB) Downloaded 781 times

Re: Draw custom value on y axis

Posted: Wed Aug 26, 2015 11:53 am
by 16475102
Thank you so much. Money well spend on a great tool and support.