How to execute the animation when chart is first drawn?

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
WarmBooter
Newbie
Newbie
Posts: 33
Joined: Thu Mar 18, 2004 5:00 am
Location: Brasil
Contact:

How to execute the animation when chart is first drawn?

Post by WarmBooter » Thu Feb 12, 2015 5:16 pm

I have a TDBChart (Bar) configured with one TSeriesAnimationTool to make the animation of the bars growing. I would like that this animation to be executed when the DBChart is paiting for the first time.

Is this possible?

I tried several ways, but none worked as I expected. Looks like the chart is always fully drawn before the animation is started.

Am I missing something?

PS: Note that the form has 5 graphics linked to 5 different datasets that are opened in sequence. The simplest code is like this:

Top5.Close;
Top5.ParamByName('dia1').AsDate:=Inic.Date;
Top5.ParamByName('dia2').AsDate:=Fim.Date;
Top5.Open;
ChartAnimation1.Execute; <- Animation starts, but the chart was already drawn

Low5.Close;
Low5.ParamByName('dia1').AsDate:=Inic.Date;
Low5.ParamByName('dia2').AsDate:=Fim.Date;
Low5.Open;
etc...

Thanks!

Carlos

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

Re: How to execute the animation when chart is first drawn?

Post by Yeray » Fri Feb 13, 2015 10:44 am

Hi Carlos,

I've made a simple example using a TBarSeries with sample values and a TSeriesAnimationTool and it seems to work fine for me here:

Code: Select all

uses Series, TeeAnimations;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.AddSeries(TBarSeries).FillSampleValues;
  Chart1.Draw;

  with Chart1.Tools.Add(TSeriesAnimationTool) as TSeriesAnimationTool do
  begin
    Series:=Chart1[0];
    Play;
  end;
end;
Note I've added a chart repaint after populating the series. This is basically to force the axes to be scaled to fit the values in the series.
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

WarmBooter
Newbie
Newbie
Posts: 33
Joined: Thu Mar 18, 2004 5:00 am
Location: Brasil
Contact:

Re: How to execute the animation when chart is first drawn?

Post by WarmBooter » Fri Feb 13, 2015 11:44 am

Thanks for replying!

I found a way to do what I want (well, almost...). The problem is that I have 5 DBcharts attached to 5 different datasets. The datasets are opened in sequence, and each of them takes some time to fetch the records, and this mess with the animation (probably because animation runs in low thread priority?).

What I did to solve the problem was to use Disable/EnableControls, something like this:

query1.disablecontrols;
query2.disablecontrols;
...
query1.open;
query2.open;
...
query1.enablecontrols;
query2.enablecontrols;
...
DBChart1.draw;
Animation1.play;
DBChart2.draw;
Animation2.play;

BUT, if I don't call the draw method before playing the animations, it gets really weird. But it is a bit nonsense to have the full chart draw to the screen and after that get the bars animated. Isn't there a way to "prepare" the chart without drawing it, just before playing the animation?

Carlos

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

Re: How to execute the animation when chart is first drawn?

Post by Yeray » Fri Feb 13, 2015 11:59 am

Hello Carlos,

You could try to calculate the maximum and minimum values the axes will have when the animation will be completed and set them to the axes. Ie:

Code: Select all

uses Series, TeeAnimations;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.Legend.Visible:=false;

  Chart1.AddSeries(TBarSeries).FillSampleValues;
  Chart1.Axes.Left.SetMinMax(0,Chart1[0].YValues.MaxValue);
  Chart1.Axes.Bottom.SetMinMax(-0.5,Chart1[0].XValues.MaxValue+0.5);

  with Chart1.Tools.Add(TSeriesAnimationTool) as TSeriesAnimationTool do
  begin
    Series:=Chart1[0];
    Play;
  end;

  Chart1.Axes.Left.Automatic:=true;
  Chart1.Axes.Bottom.Automatic:=true;
end;
In your case this means you will probably have to calculate the min and max before disabling the datasets, before assigning them to the series.
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

WarmBooter
Newbie
Newbie
Posts: 33
Joined: Thu Mar 18, 2004 5:00 am
Location: Brasil
Contact:

Re: How to execute the animation when chart is first drawn?

Post by WarmBooter » Fri Feb 13, 2015 12:07 pm

This hack seems to have solved my problem:

Code: Select all

  SendMessage(parent.Handle, WM_SETREDRAW, WPARAM(False), 0);
  try
    DBChart1.Draw;
    DBChart2.Draw;
    DBChart3.Draw;
    DBChart5.Draw;
  finally
    SendMessage(parent.Handle, WM_SETREDRAW, WPARAM(true), 0);
  end;
  DBChart1.Animations[0].Play;
  DBChart2.Animations[0].Play;
  DBChart3.Animations[0].Play;
  DBChart5.Animations[0].Play;
Anyway, I still think animations could be improved. For example, you could add a property telling the chart to autoplay the animation when it is drawing for the first time, meaning the first draw would already be done by the animation (no flickering at all).

Carlos

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

Re: How to execute the animation when chart is first drawn?

Post by Yeray » Fri Feb 13, 2015 2:49 pm

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

Post Reply