Recent

Author Topic: Mousewheel zooming starting point  (Read 2923 times)

Marq01

  • New Member
  • *
  • Posts: 13
Mousewheel zooming starting point
« on: February 10, 2016, 08:12:48 pm »
Hi,

When implementing mousewheel zooming in the TAchart it zooms with the mouse pointer as origin.
Is it possible to have the 0.0 coordinate as zoom origin regardless of the mouse position?
If so, can you give a code example to manage this?

Arnoud

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Mousewheel zooming starting point
« Reply #1 on: February 10, 2016, 10:56:02 pm »
There are two mousewheel zoom modes depending on the selection of to property "FixedPoint" of the MouseWheelZoomTool (http://wiki.lazarus.freepascal.org/TAChart_documentation#Extent_tools):
  • FixedPoint = true --> zooming uses the current mouse position as a stationary point
  • FixedPoint = false --> zooming uses the center of the visible chart window as a stationary point (This mode was not working correctly, I fixed it in r51858 of trunk and requested backporting to Laz 1.6 RC3)
Is FixedPoint = false ok for you? Or do you really need to zoom relative to the origin?

[EDIT]
If you really want to zoom relative to the origin you can add a TZoomMouseWheelTool, set its FixedPoint to false and ZoomFactor to a value slightly larger than 1 (maybe 1.1). Then add event handlers for OnBeforeMouseWheelUp and OnBeforeMouseWheelDown and the following code:
Code: Pascal  [Select][+][-]
  1. uses
  2.   TAChartUtils, TAGeometry;
  3.  
  4. { TForm1 }
  5.  
  6. procedure TForm1.DoZoomStep(ATool: TChartTool; const AFactor: TDoublePoint);
  7. var
  8.   sz, center: TDoublePoint;
  9.   ext: TDoubleRect;
  10. begin
  11.   ext := Chart1.LogicalExtent;
  12.   sz := ext.b - ext.a;
  13.   center := DoublePoint(ext.a.x, ext.a.y);
  14.   ext.a := center;
  15.   ext.b := center + sz * AFactor;
  16.   Chart1.LogicalExtent := ext;
  17. end;
  18.  
  19. procedure TForm1.ChartToolset1ZoomMouseWheelTool2BeforeMouseWheelDown(
  20.   ATool: TChartTool; APoint: TPoint);
  21. var
  22.   tool: TZoomMouseWheelTool;
  23. begin
  24.   tool := TZoomMouseWheelTool(ATool);
  25.   if (tool.ZoomFactor > 0) and (tool.ZoomRatio > 0) then
  26.     DoZoomStep(ATool, DoublePoint(
  27.       tool.ZoomFactor,
  28.       tool.ZoomFactor * tool.ZoomRatio)
  29.     );
  30.   ATool.Handled;
  31. end;
  32.  
  33. procedure TForm1.ChartToolset1ZoomMouseWheelTool2BeforeMouseWheelUp(
  34.   ATool: TChartTool; APoint: TPoint);
  35. var
  36.   tool: TZoomMouseWheelTool;
  37. begin
  38.   tool := TZoomMouseWheelTool(ATool);
  39.   if (tool.ZoomFactor > 0) and (tool.ZoomRatio > 0) then
  40.     DoZoomStep(ATool, DoublePoint(
  41.       1 / tool.ZoomFactor,
  42.       1 / tool.ZoomFactor / tool.ZoomRatio)
  43.     );
  44.   ATool.Handled;
  45. end;

The DoZoomStep procedure (which is adapted from the original code of the tool) sets the zoom origin at the lower left corner of the chart window (Chart.Logicalextent.a) and, from here, multiplies to Chart.Logicalextent by the zoom factor.
It would be a good exercise for you to modify this procedure such that zooming is relative to any other point  :-X
« Last Edit: February 10, 2016, 11:45:33 pm by wp »

 

TinyPortal © 2005-2018