time on x-axis

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
kgbaviation
Newbie
Newbie
Posts: 2
Joined: Mon Jun 10, 2019 12:00 am

time on x-axis

Post by kgbaviation » Thu Jun 27, 2019 4:27 pm

Hi,

I am looking to have time on a x axis I have tried datetime, but some data goes past 24 hrs which adds to the day and i would like to display as hours. 25:15:35 instead of 01:15:35. Is there anyway to do this with datetime or a custom label?

Any help would be appreciated
Thanks,
Paul

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

Re: time on x-axis

Post by Yeray » Fri Jun 28, 2019 2:28 pm

Hello,

You could intercept the labels at OnGetAxisLabel event and modify them to fit your needs.
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

kgbaviation
Newbie
Newbie
Posts: 2
Joined: Mon Jun 10, 2019 12:00 am

Re: time on x-axis

Post by kgbaviation » Mon Jul 01, 2019 3:39 pm

Thank you for your response, but I am unclear as to how to change the labels as each time it updates it changes the labels back to the original. Also the only useful parameter I see in the function is the label which is a string and not the value it represents. If you could clear any of this up for me it would be much appreciated.

Thank you,
Paul

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

Re: time on x-axis

Post by Yeray » Wed Jul 03, 2019 9:13 am

Hello Paul,

You can set the axis to show the complete datetime (dd/mm/yyy hh:mm) and then reformat the LabelText in the OnGetLabel event according to your needs.
Ie:

Code: Select all

uses DateUtils;

var tomorrow: TDateTime;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
var date: TDateTime;
begin
  Chart1.View3D:=False;
  Chart1.Legend.Hide;

  date:=Now;
  tomorrow:=IncDay(date);
  tomorrow:=EncodeDate(YearOf(tomorrow),MonthOf(tomorrow),DayOf(tomorrow));

  with Chart1.AddSeries(TPointSeries) do
  begin
    XValues.DateTime:=True;
    AddXY(date, 50+random*50);
    for i:=1 to 48 do
    begin
      date:=IncHour(date);
      AddXY(date, YValues[Count-1]+random*10-5);
    end;
  end;

  Chart1.Axes.Bottom.DateTimeFormat:='dd/mm/yyyy hh:mm';
end;

procedure TForm1.Chart1GetAxisLabel(Sender: TChartAxis; Series: TChartSeries;
  ValueIndex: Integer; var LabelText: string);
var date: TDateTime;
    hour, min, tmp: Integer;
    hourStr, minStr: string;
begin
  if (Sender=Chart1.Axes.Bottom) and (Sender.IsDateTime) then
  begin
    date:=StrToDateTime(LabelText);
    if (date>=tomorrow) then
    begin
      tmp:=DayOf(date-tomorrow)-DayOf(0)+1;
      hour:=HourOf(date)+tmp*24;
      min:=MinuteOf(date);
      hourStr:=IntToStr(hour);
      minStr:=IntToStr(min);

      if Length(hourStr)=1 then
        hourStr:='0'+hourStr;

      if Length(minStr)=1 then
        minStr:='0'+minStr;

      LabelText:=hourStr+':'+minStr;
    end
    else
      LabelText:=FormatDateTime('hh:mm', date);
  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