This is a migrated thread and some comments may be shown as answers.

How to Change the handler of Zooming

1 Answer 54 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Hassan
Top achievements
Rank 1
Hassan asked on 25 Mar 2015, 11:22 AM
Dear Telerik, 


how to change the handler of the zooming instead of CTRL & mouse scroll.

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 30 Mar 2015, 08:34 AM
Hello Hassan,

Thank you for writing.

To change the modifier key of the pan and zoom controller you should create a class that inherits the default controller and override the OnMouseWheel method. For example you can use the following code to zoom with the shift key:
class MyPanZoomControler : ChartPanZoomController
{
    protected override ActionResult OnMouseWheel(MouseEventArgs e)
    {
        if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift && this.Area is CartesianArea)
        {
            IChartView chartView = this.Area.View;
 
            double zoomWidth = chartView.ZoomWidth;
 
            if (this.PanZoomMode == ChartPanZoomMode.Horizontal || this.PanZoomMode == ChartPanZoomMode.Both)
            {
                zoomWidth += ((double)e.Delta / 1200d);
            }
 
            double zoomHeight = chartView.ZoomHeight;
 
            if (this.PanZoomMode == ChartPanZoomMode.Vertical || this.PanZoomMode == ChartPanZoomMode.Both)
            {
                zoomHeight += ((double)e.Delta / 1200d);
            }
 
            double virtualHorizontalPosition = e.X - this.Area.AreaModel.LayoutSlot.X - chartView.PlotOriginX;
            double plotAreaVirtualWidth = this.Area.AreaModel.LayoutSlot.Width * chartView.ZoomWidth;
            double relativeHorizontalPosition = virtualHorizontalPosition / plotAreaVirtualWidth;
            double newPlotAreaVirtualWidth = this.Area.AreaModel.LayoutSlot.Width * zoomWidth;
            double newPanOffsetX = (newPlotAreaVirtualWidth * relativeHorizontalPosition) - (e.X - this.Area.AreaModel.LayoutSlot.X);
            newPanOffsetX = this.ClampValue(newPanOffsetX, 0, this.Area.AreaModel.LayoutSlot.Width * (zoomWidth - 1d));
 
            double virtualVerticalPosition = e.Y - this.Area.AreaModel.LayoutSlot.Y - chartView.PlotOriginY;
            double plotAreaVirtualHeight = this.Area.AreaModel.LayoutSlot.Height * chartView.ZoomHeight;
            double relativeVerticalPosition = virtualVerticalPosition / plotAreaVirtualHeight;
            double newPlotAreaVirtualHeight = this.Area.AreaModel.LayoutSlot.Height * zoomHeight;
            double newPanOffsetY = (newPlotAreaVirtualHeight * relativeVerticalPosition) - (e.Y - this.Area.AreaModel.LayoutSlot.Y);
            newPanOffsetY = this.ClampValue(newPanOffsetY, 0, this.Area.AreaModel.LayoutSlot.Height * (zoomHeight - 1d));
 
            zoomWidth = this.ClampValue(zoomWidth, 1d, 100d);
            zoomHeight = this.ClampValue(zoomHeight, 1d, 100d);
 
            this.Area.View.Zoom(zoomWidth, zoomHeight);
            this.Area.View.Pan(-newPanOffsetX, -newPanOffsetY);
        }
 
        return base.OnMouseWheel(e);
    }
 
    private double ClampValue(double value, double minValue, double maxValue)
    {
        return Math.Max(minValue, Math.Min(value, maxValue));
    }
}

Please let me know if there is something else I can help you with. 
 
Regards,
Dimitar
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
ChartView
Asked by
Hassan
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or