Page 1 of 1

Restricting zooming and scrolling

Posted: Wed May 22, 2013 4:15 pm
by 10047857
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.

Re: Restricting zooming and scrolling

Posted: Thu May 23, 2013 10:00 am
by yeray
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;

Re: Restricting zooming and scrolling

Posted: Thu May 23, 2013 10:16 am
by 10047857
Thanks Yeray I'll give that a go!