1

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

尝试单击时如何防止放大?是否有类似缩放的最小矩形大小?

这是我处理点击的方式:

_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) 
    { 

    }
}

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

_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;
4

2 回答 2

1

您可以自己手动处理缩放。您可以使用MouseDown事件来捕获开始 X 和开始 Y。然后使用MouseUp事件来捕获结束 X 和结束 Y。一旦有了开始点和结束点,您就可以确定是否要缩放。如果要缩放,可以使用下面的辅助功能手动缩放。

private void set_chart_zoom(ChartArea c, double xStart, double xEnd, double yStart, double yEnd)
{
    c.AxisX.ScaleView.Zoom(xStart, xEnd);
    c.AxisY.ScaleView.Zoom(yStart, yEnd);
}
于 2018-05-10T14:59:25.133 回答
1

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

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

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);
}
于 2018-05-10T15:50:13.950 回答