Annotation - form fails to paint correctly

Hello all,

May I ask for some help please.

I have a
DBAdvGDIPChartView that displays a product price over a period of time.
The x-axis of the chart is a date. I would like to add an annotation to
mark todays date, however, I am having 2 problems.

Problem 1.
This is the code I use to add the annotation. This works great if I call
it from a button on the form, but I would rather that chart just added
the annotation after it loaded the series.

  AdvChartView, AdvChartViewGDIP, DBAdvChartViewGDIP, AdvChart

  with DBAdvGDIPChartView1.Panes[0].Series[0].Annotations.Add do
    Text := 'Today';
    PointIndex := 10;
    OffsetY := -50;
    OffsetX := 10;
    Color :=  clred;
    //ColorTo := Lighter(Color, 40);
    //GradientType := gtForwardDiagonal;
    Bordercolor := clBlack;
    OpacityTo := 180;
    OpacityTo := 180;
    Shape := TAnnotationShape(4);
    LineColor := clred;//Color;
    Arrow := arDoubleArrow;
    ArrowColor := Color;

I add the above code to the 'OnAfterDrawSeries' event the form that the
chart resides on fails to paint correctly. When using the
OnAfterDrawSeries event (and many others too) the event is called
multiple times and perhaps this has something to do with the painting of
the form.

May I ask, which events can I use to draw a single annotation correctly?

2. My x-axis is a date (the date is pulled from a query). May I ask how
I find the appropriate 'PointIndex' for a date on x-axis that
represents today?

Thank you for your time.


You should avoid adding code that adds an annotation or any other item to a collection in a draw event. The OnAfterDrawSeries is called multiple times as this event is called after a series is drawn. You can simply move the code in the constructor of the form and only call it once after setting the dataset to active programmatically, which should be sufficient to show an annotation. The chart itself is responsible for drawing it.

Pieter Scheldeman2017-06-29 12:38:10

Thank you Pieter. I will check that now.

Also, I just figured out Delphi has a DayOfTheYear function for my second issue.


Thanks again Pieter,

Problem fixed. I added that code to OnFormShow. Everything works well now.