Restricting zooming and scrolling

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Metman
Advanced
Posts: 113
Joined: Fri Dec 21, 2007 12:00 am

Restricting zooming and scrolling

Post by Metman » Wed May 22, 2013 4:15 pm

Hi

Whilst I would like to allow my user to be able to zoom and scroll within a series of a chart, is there anyway that I can restrict a user from scrolling or zooming beyond/outside the extents of a series maximum and minimum xvalues??
I know this must be obvious but I just wondered if you could do it without writing a lot of additional code.

Bruce.

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

Re: Restricting zooming and scrolling

Post by Yeray » Thu May 23, 2013 10:00 am

Hi Bruce,

You can use the OnZoom and OnScroll events to limit it. Ie:

Code: Select all

uses Series;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  Chart1.AddSeries(TPointSeries).FillSampleValues;
end;

procedure TForm1.Chart1Zoom(Sender: TObject);
begin
  if Chart1.Axes.Bottom.Maximum-Chart1.Axes.Bottom.Minimum<5 then
    Chart1.Zoom.Allow:=false;
end;
However, this would disable the zoom and you won't be able to unzoom.

An alternative would be to use the zoom history. When the condition is reached you can restore the last axes values. This way you won't zoom more than desired:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  Chart1.AddSeries(TPointSeries).FillSampleValues;

  Chart1.Zoom.History:=true;
end;

procedure TForm1.Chart1Zoom(Sender: TObject);
begin
  if Chart1.Axes.Bottom.Maximum-Chart1.Axes.Bottom.Minimum<5 then
    with Chart1.Zoom.AxesHistory.Items[Chart1.Zoom.AxesHistory.Count-1] do
      Chart1.Axes.Bottom.SetMinMax(AxesMinMax[6], AxesMinMax[7]);
end;
Or you can do it manually, if you want to avoid saving the history and directly jump to the initial state when you unzoom after several zooms:

Code: Select all

uses Series;

var bottomMin, bottomMax: Double;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  Chart1.AddSeries(TPointSeries).FillSampleValues;

  bottomMin:=-1;
  bottomMax:=-1;
end;

procedure TForm1.Chart1Zoom(Sender: TObject);
begin
  if Chart1.Axes.Bottom.Maximum-Chart1.Axes.Bottom.Minimum<5 then
    if (bottomMin<>-1) and (bottomMax<>-1) then
      Chart1.Axes.Bottom.SetMinMax(bottomMin, bottomMax)
    else
      Chart1.Axes.Bottom.SetMinMax(Chart1.Axes.Bottom.Minimum, Chart1.Axes.Bottom.Minimum+5);

  bottomMin:=Chart1.Axes.Bottom.Minimum;
  bottomMax:=Chart1.Axes.Bottom.Maximum;
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

Metman
Advanced
Posts: 113
Joined: Fri Dec 21, 2007 12:00 am

Re: Restricting zooming and scrolling

Post by Metman » Thu May 23, 2013 10:16 am

Thanks Yeray I'll give that a go!

Post Reply