Customized axis labels

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
ibauer
Newbie
Newbie
Posts: 35
Joined: Thu Mar 31, 2005 5:00 am
Location: Czech Republic

Customized axis labels

Post by ibauer » Sun Oct 21, 2007 10:46 pm

Hi all,

Background: Bottom axis displays values as a datetime (series' datetime property is set to true for that axis), Increment property equals to one hour and ExactDateTime is set to False because I didn't like that alternating label "effect" when panning the chart with mouse.

My problem is that when I keep zooming the chart in, there will be several labels showing the same date (e.g. 22.10.2007) at some point. To solve this problem, I modified the axis DateTimeFormat property to include the time portion as well. But this is not what I want, at least not always. I'd like to be able to display just a date portion in the label as long as there are only one label per day. When there are more than one label per day, I'd like to be able to display both date and time portions in the label.

Has anyone done something similar before?
Thanks!
Ivo Bauer [OZM Research]

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

Post by Narcís » Mon Oct 22, 2007 1:47 pm

Hi Ivo,

Maybe you could use the OnGetAxisLabel event for that.

Hope this helps!
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

ibauer
Newbie
Newbie
Posts: 35
Joined: Thu Mar 31, 2005 5:00 am
Location: Czech Republic

Post by ibauer » Mon Oct 22, 2007 2:23 pm

Hi Narcís,
narcis wrote:Maybe you could use the OnGetAxisLabel event for that.
Thanks for your suggestion. This does tell me where I'm supposed to insert my code, but it does not tell me how do I determine if there are more than one label for the same day in month, at that time. Do you have any idea?

Thanks, again.
Ivo Bauer [OZM Research]

ibauer
Newbie
Newbie
Posts: 35
Joined: Thu Mar 31, 2005 5:00 am
Location: Czech Republic

Post by ibauer » Mon Oct 22, 2007 9:31 pm

Just talking to myself. One idea would be to determine the actual value of axis increment (not the desired value as held by the Increment property). Should this value be less than one, I would consider that as an indication that there are at least two or more labels for the same day.

I discovered that the (bottom) axis has a method called CalcIncrement, but it is of no practical use to me because I'm apparently not supposed to call this method from within the OnGetAxisLabel event, because at the time this event is triggered, the CalcIncrement method is already being called by the TeeChart code, which results in a stack overflow and complete application crash (tested).

Is there any other method how to obtain the actual value of (bottom) axis increment from within the OnGetAxisLabel event handler, other than by calling CalcIncrement method?

Thanks, again.
Ivo Bauer [OZM Research]

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

Post by Yeray » Wed Oct 24, 2007 8:54 am

Hi ibauer,

As you suspected, the problem here is that OnGetAxisLabel event is executed before than the series is drawn, so this doesn't seem to be the appropriate event to acquire points positions.

Why don't you try to compare partial strings (at labels) to identify the same day points? I can't think an easier way actually.
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

ibauer
Newbie
Newbie
Posts: 35
Joined: Thu Mar 31, 2005 5:00 am
Location: Czech Republic

Post by ibauer » Wed Oct 24, 2007 10:26 am

Hi Yeray,
9348257 wrote:Why don't you try to compare partial strings (at labels) to identify the same day points?
That might be possible (using TChartAxis.LabelValue method), but even then, how can I possibly know upfront at which data points the labels will appear? Note that we are still within the OnGetAxisLabel event handler.

Could you please provide a simplistic, but working example?
Ivo Bauer [OZM Research]

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

Post by Yeray » Tue Oct 30, 2007 10:03 am

Hi ibauer,

If I understood well, you're trying to achieve something similar to the following. Note that we compare the maximum and minimum of the bottom axis and, if they are the same, we're showing only a day and we have to show only the 5 last characters of the Labels:

Code: Select all

procedure TForm1.Chart1GetAxisLabel(Sender: TChartAxis;
  Series: TChartSeries; ValueIndex: Integer; var LabelText: String);
begin
  if CompareDate(Chart1.Axes.Bottom.Minimum, Chart1.Axes.Bottom.Maximum) = 0 then
  begin
    LabelText := RightStr(LabelText,5);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i,j:Integer;
begin
  Series1.Clear;
  Series1.XValues.DateTime := true;

  Chart1.View3D := false;

  Chart1.Axes.Bottom.Increment := DateTimeStep[dtOneHour];
  Chart1.Axes.Bottom.ExactDateTime := false;
  Chart1.Axes.Bottom.DateTimeFormat := 'dd/mm/yyyy hh:mm';
  Chart1.Axes.Bottom.LabelsAlternate:=true;

  for i:=0 to 20 do
  begin
    for j:=0 to 3 do
      Series1.AddXY(EncodeDateTime(2007,11,i+1,j+1,0,0,0),random*100);
  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

ibauer
Newbie
Newbie
Posts: 35
Joined: Thu Mar 31, 2005 5:00 am
Location: Czech Republic

Post by ibauer » Tue Oct 30, 2007 1:06 pm

Hi Yeray,
9348257 wrote:If I understood well, you're trying to achieve something similar to the following. Note that we compare the maximum and minimum of the bottom axis and, if they are the same, we're showing only a day and we have to show only the 5 last characters of the Labels:
Thanks, although this is not what I wanted, you made me experimenting and I now almost have a solution I'm after. This is the code that does not display the time portion of TDateTime unless there are more than one label for the same day (note that BottomAxis.RoundFirstLabel is set to True):

Code: Select all

procedure TMyFrame.ChartGetAxisLabel(Sender: TChartAxis;
  Series: TChartSeries; ValueIndex: Integer; var LabelText: string);
// BottomAxis.DateTimeformat := 'd.m. h:mm';
const
  CSubString = #32 + '0:00';
var
  LPos: Integer;
begin
  LPos := Pos(CSubString, LabelText);
  if LPos > 0 then
  begin
    Delete(LabelText, LPos, Succ(Length(LabelText) - LPos));
  end;
end;
Now my (only minor) problem is that the above code cuts the zero time portion of the label always, but I would like to keep it in there when more than one label is displayed for the same day. Is it possible?
Ivo Bauer [OZM Research]

Pep
Site Admin
Site Admin
Posts: 3299
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Post by Pep » Wed Nov 07, 2007 5:59 pm

Hi Ivo,

you should be able to customize it saving each time the axis label that has been assigned in the OnGetAxisLabel event and before to customize it check if the saved label is the same day or not.

ibauer
Newbie
Newbie
Posts: 35
Joined: Thu Mar 31, 2005 5:00 am
Location: Czech Republic

Post by ibauer » Sun Nov 11, 2007 7:40 pm

Hi Pep,

Thanks for your suggestion. I'll give it a go.

Ivo
Ivo Bauer [OZM Research]

Post Reply