Page 1 of 1

zoom with mousewheel

Posted: Sat Oct 29, 2011 1:36 pm
by 10546313
Hi,
I use teechart v8.07 and want to implement a zoom while mousewheel.

This example don't work as I expected:

Code: Select all

procedure TChroDataResultFrame.ChroDataChartMouseWheel(Sender: TObject;
  Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean); 
 with ChroDataChart do begin
          if (WheelDelta > 0) then ChroDataChart.ZoomRect(110)
                          else ChroDataChart.ZoomPercent(90);
          Handled:=True;
     end;
I want the following behaviour :

Point on the chart with the mouse
with the wheel button the series are zoomend in or out, while the center is now at the mouseposition.
This scould be similar as the normal zoomrect with left mousebutton

Zoom in and out must result in the same zoom factor. That means if I zoom in and out the same window should be displayed

How can I do that ?

with best regards
Werner

Re: zoom with mousewheel

Posted: Mon Oct 31, 2011 12:44 pm
by yeray
Hello Werner,

Have you tried using TeeMouseWheel as discussed here?
http://www.teechart.net/support/viewtop ... 66&p=23983

Re: zoom with mousewheel

Posted: Mon Oct 31, 2011 2:41 pm
by 10546313
Dear Yeray,

I have tried the sample, but as I described the ZoomPercent does not fit the problem.
I suppose it has to be realized with ZoomRect, but don't no how.

with best regards
Werner

Re: zoom with mousewheel

Posted: Mon Oct 31, 2011 4:12 pm
by narcis
Hello Werner,

In that case you could call ZoomRect with a rectangle centered at your mouse X, Y position and with your desired dimensions. Notice the original drawing area is Chart1.ChartRect so you could increase or decrease the rectangle you send to ZoomRect depending on the movement of your mouse wheel.

Hope this helps!

Re: zoom with mousewheel

Posted: Mon Oct 31, 2011 4:35 pm
by 10546313
Thank you for the info,
do you have a sample project or a code snippet for it ?

with best regards

Werner

Re: zoom with mousewheel

Posted: Wed Nov 02, 2011 10:27 am
by yeray
Hello Werner,

I found some problems in using the MousePos given in the MouseWheel event, but I can use the mouse positions from the OnMouseMove event:

Code: Select all

uses Series;

var MouseX, MouseY: Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;
  Chart1.Panning.MouseWheel:=pmwNone;
  Chart1.AddSeries(TPointSeries).FillSampleValues();
end;

procedure TForm1.Chart1MouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
const radius=50;
begin
  if WheelDelta > 0 then
    Chart1.ZoomRect(Rect(MouseX-radius, MouseY-radius, MouseX+radius, MouseY+radius));
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  MouseX:=X;
  MouseY:=Y;
end;

Re: zoom with mousewheel

Posted: Wed Nov 02, 2011 12:19 pm
by 10546313
Dear Yeray,
thank you for the code, this is for zoom in.
I have tried this for zoom out, but the behaviour was always zoom in.
Whats wrong ?

BTW : ChroDataChart.ZoomRect(Rect(IMouseX-radius, IMouseY-radius, IMouseX+radius, IMouseY+radius));
results in a compiler error.

Code: Select all

 if WheelDelta > 0 then begin
      Rect.Bottom:= IMouseX-radius;
             Rect.Left:=IMouseY-radius;
             Rect.Right:=IMouseX+radius;
             Rect.Top:=IMouseY+radius;
     ChroDataChart.ZoomRect(Rect); 
  end else begin
     Rect.Bottom:= IMouseX+radius;
             Rect.Left:=IMouseY+radius;
             Rect.Right:=IMouseX-radius;
             Rect.Top:=IMouseY-radius;
             ChroDataChart.ZoomRect(Rect);
  end;

with best regards
Werner

Re: zoom with mousewheel

Posted: Thu Nov 03, 2011 3:00 pm
by yeray
Hello Werner,

With the actual v2011, there is a Zoom.History, that would help you to unZoom through the zoom states you've passed. This works fine for me here:

Code: Select all

uses Series;

var MouseX, MouseY: Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;
  Chart1.Panning.MouseWheel:=pmwNone;
  Chart1.AddSeries(TPointSeries).FillSampleValues();
  Chart1.Zoom.History:=true;
end;

procedure TForm1.Chart1MouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
const radius=50;
begin
  if WheelDelta > 0 then
    Chart1.ZoomRect(Rect(MouseX-radius, MouseY-radius, MouseX+radius, MouseY+radius))
  else
    Chart1.UndoZoom;
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  MouseX:=X;
  MouseY:=Y;
end;
However, in v8, without the Zoom.History, the UndoZoom call will bring you to the initial step, without any zoom at all, even if you have zoomed several times before the first unzooming.
If you want a Zoom History in v8, you'll have to implement your own history. You could have a two dimension array for each axis. When you zoom in, you add the min and max of each axis before zooming (before the ZoomRect call) so you will be able to restore them in the zoom out procedure.