Page 1 of 1

Programmatically move to any date in the graph

Posted: Wed Mar 02, 2011 12:36 am
by 16557879
Hello,

I am using TeeChart Pro 2011 and Delphi XE. I have a bar chart that switches between dtOneDay, dtOneWeek, and dtOneMonth and Axes.Bottom.ExactDateTime is set to true.

I want to enable the user to move programmatically to any date within the graph. For example, if the graph start date is March 1, they may want to jump to June 15 without scrolling through the graph.

Is there a way to do this?

Thanks for your help,

Dave

Re: Programmatically move to any date in the graph

Posted: Wed Mar 02, 2011 2:32 pm
by yeray
Hi Dave,

You only need to call the SetMinMax function with the according parameters.
In the following example I take the initial distance in the bottom axis (max-min) and I set the same axis to start at the given point and end at the given point plus the distance calculated. And everything corrected with an also calculated offset.

Code: Select all

uses Series, DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
    date: TDateTime;
begin
  with Chart1.AddSeries(TBarSeries) as TBarSeries do
  begin
    XValues.DateTime:=true;
    date:=DateOf(Now);
    for i:=0 to 5 do
    begin
      date:=date+15;
      AddXY(date,random*100);
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var tmp: double;
    offset: double;
    ValueIndex: Integer;
begin
  ValueIndex:=3;

  offset:=(Chart1[0].XValue[1]-Chart1[0].XValue[0])/4*2.5;
  tmp:=Chart1.Axes.Bottom.Maximum-Chart1.Axes.Bottom.Minimum;
  Chart1.Axes.Bottom.SetMinMax(Chart1[0].XValue[ValueIndex] - offset, Chart1[0].XValue[ValueIndex] + tmp + offset);
end;
If that's not what you were looking for, don't hesitate to let us know.

Re: Programmatically move to any date in the graph

Posted: Thu Mar 03, 2011 9:17 pm
by 16557879
Thanks Yeray.

I was hoping for a simple function along the line of

Chart1.Axes.Bottom.ShowDate(TDateTime)

I will take your suggested code and work with it. I think it should do what I want.

Dave

Re: Programmatically move to any date in the graph

Posted: Fri Mar 04, 2011 10:30 am
by yeray
Hi Dave,

Logically, each situation might be different, each chart can have a different distance between points (or an irregular distance). That's why I'm not sure how this ShowDate (or ShowValue because in general we talk about doubles) would exactly behave. Furthermore, note that what we are really doing here, in fact, is a scroll to a point, showing the values from a given point to an empty space maintaining the initial scale. Why not a function to show from the given point to the last point, changing the scale?
I mean, there are many variants we should consider here. Of course, we'll be glad to hear and consider your comments and suggestions.

Re: Programmatically move to any date in the graph

Posted: Fri Mar 04, 2011 3:07 pm
by 16557879
Hi Yeray,

I understand what you are saying. I was focused on dates rather than what the graph is really doing.

I have implemented your suggestion in my code making adjustments for time scales (days, weeks, months etc.) and it works that way we want it to. Thank you!

The one part of your code I do not fully understand is the offset values you are calculating (offset:=(Chart1[0].XValue[1]-Chart1[0].XValue[0])/4*2.5;). How did you decide to use 4*2.5? Is there a place in the documentation I could read up on using offset values?

Thanks for your help.

Dave

Re: Programmatically move to any date in the graph

Posted: Fri Mar 04, 2011 6:04 pm
by yeray
Hi Dave,

Excuse me. I've hardcoded what I was visually seeing. When you draw a bar series, by default, an offset is calculated internally. It is calculated considering the margins, the axis range,... a little bit too complex to extract the essential parts we need here easily.
The following is probably more elegant than my initial "4*2.5":

Code: Select all

  offset:=Chart1[0].XValue[0]- Chart1.Axes.Bottom.CalcPosPoint(Chart1.Axes.Bottom.IStartPos);