Page 1 of 1

HOW TO: Compare multiple years?

Posted: Thu Aug 30, 2007 11:19 pm
by 9345006
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

Posted: Fri Aug 31, 2007 8:59 am
by narcis
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;

Posted: Sun Sep 02, 2007 5:30 pm
by 9047589
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

Posted: Tue Sep 04, 2007 11:43 pm
by 9345006
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

Posted: Wed Sep 05, 2007 7:53 am
by narcis
Hi nisbus,

You're very welcome! I'm glad to hear that helped.