Customized axis labels
Customized axis labels
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!
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]
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi Ivo,
Maybe you could use the OnGetAxisLabel event for that.
Hope this helps!
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 |
Instructions - How to post in this forum |
Hi Narcís,
Thanks, again.
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?narcis wrote:Maybe you could use the OnGetAxisLabel event for that.
Thanks, again.
Ivo Bauer [OZM Research]
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.
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]
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.
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,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |
Hi Yeray,
Could you please provide a simplistic, but working example?
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.9348257 wrote:Why don't you try to compare partial strings (at labels) to identify the same day points?
Could you please provide a simplistic, but working example?
Ivo Bauer [OZM Research]
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:
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,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |
Hi Yeray,
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?
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):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:
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;
Ivo Bauer [OZM Research]
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.
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.
Pep Jorge
http://support.steema.com
http://support.steema.com