【问题标题】:MS Chart Control: prevent zoom when clickingMS 图表控件:单击时防止缩放
【发布时间】:2018-06-11 00:56:02
【问题描述】:

我正在使用一个 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;

【问题讨论】:

  • @HansPassant 抱歉,如果我不够精确。我不想禁用缩放。我只是想防止意外缩放。
  • 只需提供一个菜单命令或工具栏按钮以允许用户重置缩放。这样一来,您和我们都不必猜测“事故”是什么样的。

标签: c# winforms mschart


【解决方案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);
}

【讨论】:

  • 非常感谢 - 这绝对是解决问题的关键。
【解决方案2】:

根据 @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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2012-08-29
    • 2019-03-30
    • 1970-01-01
    相关资源
    最近更新 更多