Page 1 of 2

ZoomIn ZoomOut with MouseWheel ?

Posted: Fri Jul 06, 2007 9:06 am
by 9349911
Hi !

Is it possible to Zoom in and Zoom out with the mousewheel ?
How can I realize this?

Posted: Fri Jul 06, 2007 9:26 am
by narcis
Hi Dominik,

Yes, this is possible using this code:

Code: Select all

  Chart1.TabStop:=true;
  TeeUseMouseWheel:=true;

Posted: Fri Jul 06, 2007 9:48 am
by 9349911
Hi !

Well I added

Code: Select all

  TeeUseMouseWheel    := true;
  Chart1.TabStop      := true;
in FormCreate

but nothing happend. Are ther some other properties which have to be set (or not)?

Something is interesting:

Code: Select all

procedure TForm1.Chart1MouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
  beep;
end;
This procedure is never fired up when use the wheel from my mouse.

Any suggestions ?

Posted: Fri Jul 06, 2007 9:53 am
by narcis
Hi Dominik,

Code below works fine for me here using latest v8 release available. Which TeeChart version are you using? Can you please test if this works fine at your end?

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  TeeUseMouseWheel:=true;
  Chart1.TabStop:=true;
end;

procedure TForm1.Chart1MouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
  Chart1.Title.Text.Add('Hi');
end;
Thanks in advance.

Posted: Fri Jul 06, 2007 10:00 am
by 9349911
I did a test with a really small sample application. The mousewheel works.

But it only scrolls the y-axis.

Is it possible to scroll the x-Axis, too?
And how can I use the mouse wheel to zoom in / out (what is the interesting part for me)?

Posted: Fri Jul 06, 2007 10:07 am
by narcis
Hi Dominik,
Is it possible to scroll the x-Axis, too?
This may work provided you have a mouse that supports horizontal wheel scrolling.
And how can I use the mouse wheel to zoom in / out (what is the interesting part for me)?
Sorry for overlooking the original message. In that case you can do something like in this C++ Builder example:

Code: Select all

void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift,
	  int WheelDelta, TPoint &MousePos, bool &Handled) {
	Chart1->Zoom->Animated = false;

	if(WheelDelta > 0)
	  Chart1->ZoomPercent(110);

	if(WheelDelta < 0)
	  Chart1->ZoomPercent(90);
}

Posted: Fri Jul 06, 2007 10:13 am
by 9349911
Ok thx !

But what the hell can cause Chart1MouseWheel to be never fired up?
In my simple demo it works. My bigger demo don´t bring this event.

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  TeeUseMouseWheel    := true;
  Chart1.TabStop      := true;
This is the start from FormCreate. So in general it should work ?! :?

Posted: Fri Jul 06, 2007 10:20 am
by narcis
Hi Dominik,

Is there any other mouse event on your project that may be overriding the OnMouseWheel event? If so you could try commenting in those events and check if it makes a difference.

Posted: Fri Jul 06, 2007 10:27 am
by 9349911
Hi !

No there is only a ChartListBox2MouseDown. But this shouldn´t be a problem.

I tried to put another Chart to the mainform. Set

Code: Select all

  TeeUseMouseWheel    := true;
  Chart1.TabStop      := true;
  Chart2.TabStop      := true;
and added some sample data to the series 1. In my simple demo this works fine. In my bigger dimo this won´t work, too.

So could it be possible that there is something worng in the mainform? There are no other mousewheel events.

Strange thing ....

Posted: Fri Jul 06, 2007 10:31 am
by 9349911
This works:

Code: Select all

procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
  Chart1.Title.Text.Add('Hi'); 
end;
:?

Posted: Fri Jul 06, 2007 1:29 pm
by 9047589
From my point of view:
1. First, make sure you Chart in your big demo is the active control, otherwise it won’t fire OnMouseWheel event.

2. It’s useful to add in OnChartClick event handler something like

Code: Select all

procedure TForm1.Chart1Click(Sender: TObject);
begin
  ActiveControl:=Chart1;
…
end;
or

Code: Select all

procedure TForm1.Chart1Click(Sender: TObject);
begin
  Chart1.SetFocus;
…
end;
to allow user to set focus to chart without tabbing, just clicking it.

3. And finally in OnMouseWheel event handler write something like that

Code: Select all

procedure TForm1.Chart1MouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  Percentage : double;
begin
  if ([ssShift,ssCtrl] <= Shift) then
    with Chart1 do begin
      if WheelDelta>0 then
        Percentage:=100+0.045*WheelDelta
      else
        Percentage:=100+0.05*WheelDelta;
      ZoomPercent(Percentage); 
      Handled:=True;
    end
  else
  if ([ssAlt,ssCtrl] <= Shift) then
    with Chart1.BottomAxis  do begin
      Scroll(WheelDelta*(Maximum-Minimum)/1000,False);
      Handled:=True;
    end;
end;
this enables user to use Ctrl+Shift together with mouse wheel for zooming in and out,
Ctrl+Alt together with mouse wheel for horizontal scrolling and still allow him to use plain MouseWheel for vertical scrolling.

4. Last bun not least, thank you for a good tip how to organize more clever Unzoom under control of user, I just work on this issue, because standard TeeChart Unzoom to the original 100% is not always what user expects.
Regards,
Alexander

Posted: Fri Jul 06, 2007 3:18 pm
by 9349911
I only have to say once:

THANK YOU VERY MUCH !!! :D

That´s absolutely great ... fantastic :shock: :D

Posted: Thu Jul 12, 2007 6:01 am
by 9349911
Hi !

There are still 2 small problems:
1) custom Y axis
I use only custom Y axis and I zoom these axis with this code:

Code: Select all

procedure TForm1.Chart1Zoom(Sender: TObject);
var Achse   : TChartAxis;
    i       : Byte;
begin
  if Chart1.Zoom.Direction in [tzdBoth, tzdVertical] then

  for i := 0 to Chart1.SeriesCount - 1 do begin
    Achse := Chart1.Series[i].GetVertAxis;
    if Achse.Visible then
      Achse.SetMinMax(Achse.CalcPosPoint(Chart1.Zoom.Y1),Achse.CalcPosPoint(Chart1.Zoom.Y0));
  end;    
end;
And this is my code for the MouseWheel:

Code: Select all

procedure TForm1.Chart1MouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin 
  if ([ssShift,ssCtrl] <= Shift) then
    with Chart1 do begin
      if (WheelDelta > 0) then Chart1.ZoomPercent(110)
                          else Chart1.ZoomPercent(90);
      Handled:=True; 
    end;
end;
If you try this and use the scrolling wheel for zooming (with pressed Shift, CTRL) the Y axis are zoomed dramatically. The axis are approximated zoomed with the factor 1000-2000. You can´t see any series any more.
If I don´t use the Chart1Zoom Event, then the Y axis won´t be zoomed. Only the x axis - what is ok.

2) Scrolling chart with the mouse wheel
I use this code:

Code: Select all

procedure TForm1.Chart1MouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin 
  if ([ssAlt] <= Shift) then
    with Chart1.BottomAxis  do begin
      Scroll(WheelDelta*(Maximum-Minimum)/1000,False); 
      Handled:=True; 
    end;
  Chart1.SetFocus;
end;
Works great in both directions. You can scroll vertical by pressing the Alt key and it works. If you want to scroll horizontal by releasing the Alt key you have to click into the chart first. If you don´t click you won´t be able to scroll the chart in horizontal direction.
How can I change this so that no additional click is necessary?

Posted: Thu Jul 12, 2007 10:03 am
by Pep
Hi,

have you tried changing the :
Chart1.SetFocus;
by
Chart1.TabStop := true;
?

Posted: Thu Jul 12, 2007 10:46 pm
by 9047589
9349911 wrote:If you try this and use the scrolling wheel for zooming (with pressed Shift, CTRL) the Y axis are zoomed dramatically.
Event fires each time you turn a wheel - 10% for each Wheel movement is too much!

9349911 wrote: If I don´t use the Chart1Zoom Event, then the Y axis won´t be zoomed.
It seems we work in parallel direction. My work is on progress, I can only mention I turned from ZoomPercent to ZoomRect and now it works with custom axis as well.

9349911 wrote: You can scroll vertical by pressing the Alt key and it works. If you want to scroll horizontal by releasing the Alt key you have to click into the chart first. If you don´t click you won´t be able to scroll the chart in horizontal direction.
I didn't catch the point. Your code and comment regarding Alt-key operation seems controversial. Anyhow, firing Wheel event is beyond TeeChart and it seems you have enable TChart to catch it yourself, i.g. providing handler for OnExit events of all other contols, or may be catch corresponding Window message.