MS 图表控件:单击时防止缩放 [英] MS Chart Control: prevent zoom when clicking

查看:55
本文介绍了MS 图表控件:单击时防止缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MS 图表控件,该控件在单击图表时设置一个光标,并使用户能够放大和缩小.当用户尝试点击图表时,他意外地拖动了一个非常小的缩放矩形,图表会放大而不是处理点击.

I'm using a MS Chart Control that sets a cursor when the chart is clicked and that enables the user to zoom in and out. When the user tries to click into the chart it accidentally happens that he drags a very small zoom rectangle and the chart zooms in instead of handling the click.

如何防止在尝试点击时放大?是否有用于缩放的最小矩形大小之类的东西?

What can be done to prevent zooming in when trying to click? Is there something like a minimum rectangle size for zooming?

以下是我处理点击的方式:

Here's how I handle the click:

_area = new ChartArea();

private void chart1_MouseClick(object sender, MouseEventArgs e) 
{
    try 
    {
        _area.CursorX.SetCursorPixelPosition(new Point(e.X, e.Y), true);
    }
    catch (Exception ex) 
    { 

    }
}

这就是我设置缩放和光标设置的方式:

And this is how I setup the zoom and cursor settings:

_area.AxisX.ScaleView.Zoomable = true;
_area.CursorX.IsUserSelectionEnabled = true;
_area.CursorX.IntervalType = DateTimeIntervalType.Seconds;
_area.CursorX.Interval = 1D;
_area.CursorY.IsUserSelectionEnabled = true;
_area.CursorY.Interval = 0;

推荐答案

基于 @Baddack 的回答,这里有一个完整的解决方案.关键是禁用图表的缩放功能并使用 MouseUp/MouseDown 事件手动缩放(如 Baddack 建议的那样).图表的用户选择功能保持启用,以使用选择矩形设置缩放间隔.

Based on @Baddack's answer here's a complete solution. The key is to disable the zoom feature of the chart and zoom manually (like Baddack suggested) by using MouseUp/MouseDown events. The user selection feature of the chart is kept enabled to use the selection rectangle for setting the zoom interval.

此示例代码检查缩放重角的宽度和高度是否至少为 10 像素.只有在这种情况下才会启动缩放:

This sample code checks if the zoom retangle is at least 10 pixels wide and high. Only if that's the case the zoom is initiated:

private ChartArea _area;
private Point _chartMouseDownLocation;
...

private void MainForm_Load(object sender, EventArgs e)
{
    ...
    // Disable zooming by chart control because zoom is initiated by MouseUp event
    _area.AxisX.ScaleView.Zoomable = false;
    _area.AxisY.ScaleView.Zoomable = false;

    // Enable user selection to get the interval/rectangle of the selection for 
    // determining the interval for zooming
    _area.CursorX.IsUserSelectionEnabled = true;
    _area.CursorX.IntervalType = DateTimeIntervalType.Seconds;
    _area.CursorX.Interval = 1D;
    _area.CursorY.IsUserSelectionEnabled = true;
    _area.CursorY.Interval = 0;        
}

private void chart1_MouseDown(object sender, MouseEventArgs e)
{
    _chartMouseDownLocation = e.Location;
}

private void chart1_MouseUp(object sender, MouseEventArgs e)
{
    // Check if rectangle has at least 10 pixels with and hright
    if (Math.Abs(e.Location.X - _chartMouseDownLocation.X) > 10 && 
        Math.Abs(e.Location.Y - _chartMouseDownLocation.Y) > 10)
    {
        // Zoom to the Selection rectangle
        _area.AxisX.ScaleView.Zoom(
            Math.Min(_area.CursorX.SelectionStart, _area.CursorX.SelectionEnd),
            Math.Max(_area.CursorX.SelectionStart, _area.CursorX.SelectionEnd)
        );
        _area.AxisY.ScaleView.Zoom(
            Math.Min(_area.CursorY.SelectionStart, _area.CursorY.SelectionEnd),
            Math.Max(_area.CursorY.SelectionStart, _area.CursorY.SelectionEnd)
        );
    }
    // Reset/hide the selection rectangle
    _area.CursorX.SetSelectionPosition(0D, 0D);
    _area.CursorY.SetSelectionPosition(0D, 0D);
}

这篇关于MS 图表控件:单击时防止缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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