Page 1 of 1

time on x-axis

Posted: Thu Jun 27, 2019 4:27 pm
by 16586368
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

Re: time on x-axis

Posted: Fri Jun 28, 2019 2:28 pm
by yeray
Hello,

You could intercept the labels at OnGetAxisLabel event and modify them to fit your needs.

Re: time on x-axis

Posted: Mon Jul 01, 2019 3:39 pm
by 16586368
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

Re: time on x-axis

Posted: Wed Jul 03, 2019 9:13 am
by yeray
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;