C# MS图表控件:单击时防止缩放
我使用的是MS图表控件,它在单击图表时设置光标,并允许用户放大和缩小。当用户尝试单击图表时,意外地会拖动一个非常小的缩放矩形,图表会放大而不是处理单击 在尝试单击时,如何防止放大?是否有类似于缩放的最小矩形大小 以下是我如何处理点击:C# MS图表控件:单击时防止缩放,c#,winforms,mschart,C#,Winforms,Mschart,我使用的是MS图表控件,它在单击图表时设置光标,并允许用户放大和缩小。当用户尝试单击图表时,意外地会拖动一个非常小的缩放矩形,图表会放大而不是处理单击 在尝试单击时,如何防止放大?是否有类似于缩放的最小矩形大小 以下是我如何处理点击: _area = new ChartArea(); private void chart1_MouseClick(object sender, MouseEventArgs e) { try { _area.CursorX.Se
_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;
您可以自己手动处理缩放。您可以使用
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);
}
根据巴达克的回答,这里有一个完整的解决方案。关键是禁用图表的缩放功能,并使用
MouseUp/MouseDown
事件手动缩放(如Baddack建议的)。图表的用户选择功能保持启用状态,以使用选择矩形设置缩放间隔
此示例代码检查缩放重新角度的宽度和高度是否至少为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);
}
@汉帕桑对不起,如果我不够准确的话。我不想禁用缩放。我只是想防止意外缩放。只需提供一个菜单命令或工具栏按钮,允许用户重置缩放。这样,你和我们都不必猜测“事故”是什么样子。非常感谢——这绝对是解决问题的关键。