Weeknum functionality

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
peters
Newbie
Newbie
Posts: 4
Joined: Tue Sep 13, 2011 12:00 am

Weeknum functionality

Post by peters » Thu Nov 10, 2011 12:38 pm

Hi,

Does TeeChart support weeknum date format in chart settings? I have weekly collected data and I want to label my X-axis as

01/2011, 02/2011 etc.

where first number is a week number in the year.

Thanks
Peters

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

Re: Weeknum functionality

Post by Yeray » Thu Nov 10, 2011 3:52 pm

Hi Peters,

I'm afraid it's not supported automatically. But you can always format the axis labels at OnDrawLabel event as in the example at "All features\Welcome !\Axes\First and Last Labels".
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

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

Re: Weeknum functionality

Post by Narcís » Thu Nov 10, 2011 3:59 pm

Hi Peters,

For completeness, you may use DateUtils.WeekOfTheYear method for that.
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

peters
Newbie
Newbie
Posts: 4
Joined: Tue Sep 13, 2011 12:00 am

Re: Weeknum functionality

Post by peters » Thu Nov 10, 2011 9:00 pm

Thanks for suggestions.

Maybe in the next releases you will consider something like ''ww/yyyy" in date formating options. It would be useful improvement :-).

Peters

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

Re: Weeknum functionality

Post by Yeray » Fri Nov 11, 2011 1:32 pm

Hi Peters,

I've added it to the wish list to be implemented in future releases (TV52015832).
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

peters
Newbie
Newbie
Posts: 4
Joined: Tue Sep 13, 2011 12:00 am

Re: Weeknum functionality

Post by peters » Sat Nov 12, 2011 10:00 am

Hi,

I have almost found a solution which looks like this:

Code: Select all

procedure TMyChart.ChartGetAxisLabel(Sender: TChartAxis;
  Series: TChartSeries; ValueIndex: Integer; var LabelText: String);
const
  WW = 'ww';
var
  WeekNum : string;
begin
  if Sender.IsDateTime and (pos(WW, Sender.DateTimeFormat) > 0) then
     try
       WeekNum := IntToStr(WeekOfTheYear(StrToDate(LabelText)));
     except
       WeekNum := 'ERR';
     end;
     LabelText := StringReplace(LabelText, WW, WeekNum, [rfReplaceAll]);
end;
The problem is that instead of LableText I need original X-axis value, because LabelText is ALREADY formated and StrToDate returns error during convertion. So my question is how to get here oryginal bottom axis value BEFORE formating.

Thanks
Peters

peters
Newbie
Newbie
Posts: 4
Joined: Tue Sep 13, 2011 12:00 am

Re: Weeknum functionality

Post by peters » Sun Nov 13, 2011 8:16 pm

Hi,

I have dug a little in the source code and I think I have a solution, but it would require changes in source code of TeEngine. Date formatting is done in LabelValue function. It gets its parameter (date value for x-label tick) from DoDeaultLabels procedure, but actual x-value is stored only in temporary tmpValue variable which is not published. So now I can see 3 solutions:

1) publish tmpValue as a property and than us it in OnGetAxisLabel
2) overwrite LabelValue function with code from my previous post
3) overwrite DateTimeToString standard VCL function and implement there the interpretation of "ww" formatting

Because of compatibility with next releases all of them are not safe. Maybe anyone has better idea? :-).

Thanks
Peters

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

Re: Weeknum functionality

Post by Yeray » Tue Nov 15, 2011 4:39 pm

Hello Peters,
peters wrote:1) publish tmpValue as a property and than us it in OnGetAxisLabel
2) overwrite LabelValue function with code from my previous post
3) overwrite DateTimeToString standard VCL function and implement there the interpretation of "ww" formatting
The second option is the one I think could be interesting. It will be considered when TV52015832 will be deeply studied.
In the meanwhile, what about using Custom Labels?

Code: Select all

uses Series, DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var i, last: Integer;
    tmpDate: TDateTime;
    diff: Double;
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;
  Chart1.Axes.Bottom.LabelsAngle:=90;
  Chart1.Axes.Bottom.DateTimeFormat:='ww/yyyy';

  with Chart1.AddSeries(TFastLineSeries) as TFastLineSeries do
  begin
    XValues.DateTime:=true;

    tmpDate:=StrToDateTime('01/01/2011');
    AddXY(tmpDate, random*100);
    for i:=0 to 20 do
    begin
      tmpDate:=IncDay(tmpDate,7);
      AddXY(tmpDate, YValue[Count-1] + random*10-5);
    end;

    Chart1.Axes.Bottom.Increment:=DateTimeStep[dtOneWeek];
    Chart1.Axes.Bottom.Items.Clear;
    Chart1.Axes.Bottom.Items.Add(XValue[0],StringReplace(FormatDateTime(Chart1.Axes.Bottom.DateTimeFormat,XValue[0]), 'ww', IntToStr(WeekOfTheYear(XValue[0])), [rfReplaceAll]));
    last:=0;
    for i:=1 to Count-1 do
    begin
      if (XValue[i]-XValue[last] >= Chart1.Axes.Bottom.Increment) then
      begin
        Chart1.Axes.Bottom.Items.Add(XValue[i],StringReplace(FormatDateTime(Chart1.Axes.Bottom.DateTimeFormat,XValue[i]), 'ww', IntToStr(WeekOfTheYear(XValue[i])), [rfReplaceAll]));
        last:=i;
      end;
    end;
  end;
end;
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