how to enable zooming in Microsoft chart control by using Mouse wheel

后端 未结 4 1335
长情又很酷
长情又很酷 2020-12-29 23:11

I am using Microsoft Chart control in my project and I want to enable zooming feature in Chart Control by using Mouse Wheel, how can I achieve this?

but user don\'t

4条回答
  •  [愿得一人]
    2020-12-29 23:24

    I modificated code from above and added a reverse zooming using Stack.

    private class ZoomFrame
    {
        public double XStart { get; set; }
        public double XFinish { get; set; }
        public double YStart { get; set; }
        public double YFinish { get; set; }
    }
    
    private readonly Stack _zoomFrames = new Stack();
    private void chart1_MouseWheel(object sender, MouseEventArgs e)
    {
        var chart = (Chart)sender;
        var xAxis = chart.ChartAreas[0].AxisX;
        var yAxis = chart.ChartAreas[0].AxisY;
    
        try
        {
            if (e.Delta < 0)
            {
                if (0 < _zoomFrames.Count)
                {
                    var frame = _zoomFrames.Pop();
                    if (_zoomFrames.Count == 0)
                    {
                        xAxis.ScaleView.ZoomReset();
                        yAxis.ScaleView.ZoomReset();
                    }
                    else
                    {
                        xAxis.ScaleView.Zoom(frame.XStart, frame.XFinish);
                        yAxis.ScaleView.Zoom(frame.YStart, frame.YFinish);
                    }
                }
            }
            else if (e.Delta > 0)
            {
                var xMin = xAxis.ScaleView.ViewMinimum;
                var xMax = xAxis.ScaleView.ViewMaximum;
                var yMin = yAxis.ScaleView.ViewMinimum;
                var yMax = yAxis.ScaleView.ViewMaximum;
    
                _zoomFrames.Push(new ZoomFrame { XStart = xMin, XFinish = xMax, YStart = yMin, YFinish = yMax });
    
                var posXStart = xAxis.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 4;
                var posXFinish = xAxis.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 4;
                var posYStart = yAxis.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 4;
                var posYFinish = yAxis.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 4;
    
                xAxis.ScaleView.Zoom(posXStart, posXFinish);
                yAxis.ScaleView.Zoom(posYStart, posYFinish);
            }
        }
        catch { }         
    }
    

    Hope this helps!

提交回复
热议问题