Page 1 of 1

Scrolling one date at a time

Posted: Wed Feb 04, 2009 3:55 pm
by 10551622
Hello,

I am new to TChart so this may be a simple question to answer. I have a bar graph with dates along the x axis and a TChartScrollBar. When I scroll the graph using the arrows on the scroll bar the graph moves one page at a time. I would like to move just one date (data point) at a time. How can I do this?

Thanks,

Dave.

Posted: Wed Feb 04, 2009 4:15 pm
by yeray
Hi Dave,

Here there is an example doing it, I hope will help:

Code: Select all

const PointsPerPage = 10;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  with Chart1.Axes.Bottom do
  begin
    Automatic := False;
    Minimum := 0;
    Maximum := PointsPerPage - 1;
  end;

  Chart1[0].FillSampleValues(50);

  Series1.XValues.DateTime := true;

  ScrollBar1.Max := Chart1[0].Count - PointsPerPage;
end;

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
  with Chart1.Axes.Bottom do
  begin
    Minimum := ScrollBar1.Position;
    Maximum := ScrollBar1.Position + PointsPerPage - 1;
  end;
end;

Posted: Wed Feb 04, 2009 6:52 pm
by 10551622
Thank you for the quick response. That was what I was looking for.

I noticed that when you turn off “Automatic”, bars that are added using Series1.Add (x,’’,clr) are not in proportion and the more bars you add the wider each bar gets. Is there another setting such as Automatic that will keep the correct proportion of the bars? That is, the bars do not automatically become wider with each add?

Thanks again,

Dave.

Posted: Thu Feb 05, 2009 8:48 am
by yeray
Hi Dave,

I'm not sure to understand you. I've added a button with the following code to the example above and it seems to work fine here.

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
begin
  Chart1[0].Add(random*Chart1[0].MaxYValue,'',clTeeColor);
  ScrollBar1.Max := ScrollBar1.Max + 1;
end;
If you still have problems with this, please, could you send us a simple example project we can run "as-is" to reproduce the problem here?
You can either post your files at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.

Posted: Thu Feb 05, 2009 2:29 pm
by 10551622
Yeray,

Thanks for your help. I am sure it is something I am missing in a property somewhere.

If Chart1.Axes.Bottom.Automatic is true, then the bars draw correctly side by side. If I set Automatic to false so that I can scroll, the bars are incorrect – they overlap and stack onto one another. I have removed the scroll bar from the example below to simplify it.

I have added two series to the chart; both are Standard bar, 3D and smooth. No other changes are made through the Edit Chart settings.

Code: Select all

procedure TfrmChartExample.FormShow(Sender: TObject);
begin
  Chart1.MaxPointsPerPage:=10;

  with Chart1.Axes.Bottom do
  begin
    Automatic := true;  //If true, bars draw correctly.
                        //If false, bars are incorrect.
    Maximum := Date + Chart1.MaxPointsPerPage-1;
    Minimum := Date;
  end;

  Series1.XValues.DateTime := true;
  Series2.XValues.DateTime := true;
end;

procedure TfrmChartExample.Button1Click(Sender: TObject);
var
  i :integer;
  x :double;
  y :double;
begin
  for i := 0 to 50 do
  begin
    x:= IncDay(Date, i);
    y:= i+1;
    if y>10 then y:=8;
    series1.AddXY(x, y, '', clBlue);
    series2.AddXY(x, y-1, '', clSkyBlue);
  end;
end;
I am sure it is a simple setting I am missing, but I don’t know where to look. If you would like me to zip up the project and send it to you let me know.

I am using Delphi 2006, win 32.

Thanks,

Dave.

Posted: Thu Feb 05, 2009 3:49 pm
by yeray
Hi Dave,

Yes it would be better if you send us this example and, if possible, with a picture showing your result and maybe another showing the result you expected.

You can either post your files at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.

Posted: Thu Feb 05, 2009 4:05 pm
by 10551622
Hi Yeray,

I have posted ChartExample.zip to your Upload Page. I included two BMPs of the graph when Automatic is set to true (correct graph) and when Automatic is set to false (incorrect graph).

Thanks,
Dave.

Posted: Fri Feb 06, 2009 4:55 pm
by yeray
Hi Dave,

The Chart property MaxPointsPerPage is used when one wants to use the Chart pagination feature. On the other hand, you are scrolling your chart with a scrollbar. That's why setting this property your chart is confusing.

The following code works now for us here:

Code: Select all

procedure TfrmChartExample.FormShow(Sender: TObject);
var MaxPointsPerPage: Integer;
begin
  MaxPointsPerPage:=10;

  with Chart1.Axes.Bottom do
  begin
    Automatic := false; 
    Maximum := Date + MaxPointsPerPage-1;
    Minimum := Date;
  end;

  Series1.XValues.DateTime := true;
  Series2.XValues.DateTime := true;
end;

Posted: Mon Feb 09, 2009 7:00 pm
by 10551622
Yeray,

Thanks! I have corrected the problem with the MaxPointsPerPage and the bars are now drawing correctly.

I gave up trying to use TChartScrollBar as I couldn’t get it to work with the dates. Using your ideas above, I added a regular scrollbar and added:

Code: Select all

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
  with Graph.Axes.Bottom do
  begin
    SetMinMax(StartDate + ScrollBar1.Position, StartDate + ScrollBar1.Position + PointsPerPage - 1);
  end;
end;
That works just the way I want it to!

Best regards,

Dave.

[/code]

Posted: Tue Feb 10, 2009 8:55 am
by yeray
Hi Dave,

Yes, we recommend to use the standard ScrollBar instead of TChartScrollBar because it presents some problems.

I'm happy to see it works fine!