HOW TO: Compare multiple years?

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
nisbus
Newbie
Newbie
Posts: 18
Joined: Wed Jan 18, 2006 12:00 am
Location: Iceland
Contact:

HOW TO: Compare multiple years?

Post by nisbus » Thu Aug 30, 2007 11:19 pm

Hi,

Let's say I have two monthly series for two years (2005-2006) and I want to do a month by month comparison so that I get 4 series each ranging from jan.-dec.

Is there a simple way to do this?

thanks,
nisbus

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

Post by Narcís » Fri Aug 31, 2007 8:59 am

Hi nisbus,

You can achieve what you request doing something like this:

Code: Select all

uses DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var i, j: Integer;
begin
  Chart1.Axes.Bottom.DateTimeFormat:='MMM-yyyy';
  Chart1.Axes.Bottom.Increment:=DateTimeStep[dtOneMonth];
  //Chart1.Axes.Bottom.LabelsAngle:=90;

  for i:=0 to Chart1.SeriesCount-1 do
  begin
    Chart1[i].XValues.DateTime:=true;

    for j:=1 to 24 do
    begin
      if j<=12 then
        Chart1[i].AddXY(EncodeDate(2005,j,1),random)
      else
        Chart1[i].AddXY(EncodeDate(2006,(j mod 13)+1,1),random);
    end;
  end;

end;

//Month to month comparision
procedure TForm1.Button1Click(Sender: TObject);
var i, j, tmpIndex: Integer;
    tmpYear1, tmpYear2, tmpMonth: Word;
begin
  Chart1.Axes.Bottom.DateTimeFormat:='MMMM';

  for i:=0 to Chart1.SeriesCount-1 do
  begin

    tmpYear1:=YearOf(Chart1[i].XValues[0]);
    tmpIndex:=-1;

    for j:=0 to Chart1[i].Count-1 do
    begin
      tmpYear2:=YearOf(Chart1[i].XValues[j]);

      if tmpYear1<>tmpYear2 then
      begin
        if tmpIndex=-1 then
        begin
          tmpIndex:=j;
          Chart1.AddSeries(TLineSeries.Create(self));
          Chart1[Chart1.SeriesCount-1].XValues.DateTime:=true;
        end;

        tmpMonth:=MonthOf(Chart1[i].XValue[j]);
        Chart1[Chart1.SeriesCount-1].AddXY(EncodeDate(2005,tmpMonth,1),Chart1[i].YValue[j]);
      end;
    end;

    Chart1[i].Delete(tmpIndex, Chart1[i].Count - tmpIndex);
  end;

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

xxxxxx
Advanced
Posts: 128
Joined: Tue Jun 19, 2007 12:00 am
Location: Russia
Contact:

Post by xxxxxx » Sun Sep 02, 2007 5:30 pm

Hi, nisbus!
Generally speaking, for such task I could also use alternative horizontal axes: TopAxis if I needed to compare two years or additional custom axes if I needed to compare more
Regards, Alexander
=================
TeeChart Pro v8.05, Delphi 5 & Turbo Delphi Professional for Win32., Delphi & C++Builder 2009

nisbus
Newbie
Newbie
Posts: 18
Joined: Wed Jan 18, 2006 12:00 am
Location: Iceland
Contact:

Post by nisbus » Tue Sep 04, 2007 11:43 pm

Thank you both,

I used a method similar to the one Narcis suggested and I am now able to slice series apart to compare on an hourly, weekly, monthly and annual basis.

Don't you love TeeChart :)

nisbus

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

Post by Narcís » Wed Sep 05, 2007 7:53 am

Hi nisbus,

You're very welcome! I'm glad to hear that helped.
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

Post Reply