如何使用鼠标滚轮启用Microsoft图表控件中的缩放 [英] how to enable zooming in Microsoft chart control by using Mouse wheel

查看:62
本文介绍了如何使用鼠标滚轮启用Microsoft图表控件中的缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用Microsoft Chart控件,并且想使用鼠标滚轮在Chart Control中启用缩放功能,该如何实现?

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 have to click on chart, It should be like if mouse position is on my Chart than from that point onward by mouse wheel rolling it can zoom in / out

推荐答案

您将要使用 MouseWheel 事件.

首先使图表的两个轴都可缩放:

First make both axes of your chart zoomable:

chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;

并分配事件:

chart1.MouseWheel += chart1_MouseWheel;

然后在事件处理程序中:

Then in the event handler:

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) // Scrolled down.
        {
            xAxis.ScaleView.ZoomReset();
            yAxis.ScaleView.ZoomReset();
        }
        else if (e.Delta > 0) // Scrolled up.
        {
            var xMin = xAxis.ScaleView.ViewMinimum;
            var xMax = xAxis.ScaleView.ViewMaximum;
            var yMin = yAxis.ScaleView.ViewMinimum;
            var yMax = yAxis.ScaleView.ViewMaximum;

            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 { }            
}

e.Delta 属性可以告诉您您完成了多少轮滚动",这很有用.
完全缩小将整个缩小.

The e.Delta property tells you how many wheel "scrolls" you've done, and can be useful.
Scrolling out at all will zoom out the whole way.

可能有一种更清洁的方法,但是确实如此.希望这会有所帮助!

There's probably a cleaner way of doing this, but there it is. Hope this helps!

这篇关于如何使用鼠标滚轮启用Microsoft图表控件中的缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆