c# - 如何使用鼠标滚轮在 Microsoft 图表控件中启用缩放

标签 c# microsoft-chart-controls

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

但用户不必点击图表,这应该就像鼠标位置在我的图表上,而不是从那个点开始通过鼠标滚轮滚动它可以放大/缩小

最佳答案

您需要使用 MouseWheel 事件。

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

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

并分配事件:

chart1.MouseWheel += chart1_MouseWheel;

然后在事件处理程序中:

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 属性告诉您轮子“滚动”了多少次,这很有用。
向外滚动会完全缩小。

可能有更简洁的方法来执行此操作,但就是这样。 希望这对您有所帮助!

关于c# - 如何使用鼠标滚轮在 Microsoft 图表控件中启用缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13584061/

相关文章:

c# - 具有通用数据库上下文的存储库模式

.net - 错误 : Chart cannot save an item after 10 attempts

.net - 如何在 asp.net 中为 Microsoft Chart Control 生成的图像设置分辨率 (DPI)

c# - 如何将 C# Control 和额外文本保存到磁盘?

c# - 文本框 : Disable the 'Paste' option whilst allowing 'Cut' and 'Copy' on Right-click

c# - 哪种 IEnumerable To a List 方式更好?

c# - openfiledialog 的过滤属性不起作用

c# - 创建动态图表工具提示

asp.net-mvc - 任何支持具有不同切片半径的饼图的 ASP.NET MVC 友好图表控件?

c# - 合并两个有条件的列表