我有以下Winforms代码:
void chart1_MouseWheel(object sender, MouseEventArgs e)
{
double xMin = chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum;
double xMax = chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum;
if (e.Delta < 0)
{ //chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
//chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset();
}
if (e.Delta > 0)
{
double posXStart = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) - (xMax - xMin)/2 ;
double posXFinish = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) + (xMax - xMin)/2;
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(posXStart, posXFinish);
}
}
放大功能有效,但是当e.Delta< 0,我需要基于上述代码的缩小功能.
解决方法:
尝试
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(1);
chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset(1);
如果在缩放时将saveState设置为true,则ZoomReset(1)将返回到上一个缩放状态.或者,如果将saveState设置为false,则ZoomReset(1)会一直缩小.这是我的代码,我通过单击鼠标进行操作,但是我敢肯定您可以通过滚轮使用它:
private void chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
double startX, endX, startY, endY;
if (chart1.ChartAreas[0].CursorX.SelectionStart > chart1.ChartAreas[0].CursorX.SelectionEnd)
{
startX = chart1.ChartAreas[0].CursorX.SelectionEnd;
endX = chart1.ChartAreas[0].CursorX.SelectionStart;
}
else
{
startX = chart1.ChartAreas[0].CursorX.SelectionStart;
endX = chart1.ChartAreas[0].CursorX.SelectionEnd;
}
if (chart1.ChartAreas[0].CursorY.SelectionStart > chart1.ChartAreas[0].CursorY.SelectionEnd)
{
endY = chart1.ChartAreas[0].CursorY.SelectionStart;
startY = chart1.ChartAreas[0].CursorY.SelectionEnd;
}
else
{
startY = chart1.ChartAreas[0].CursorY.SelectionStart;
endY = chart1.ChartAreas[0].CursorY.SelectionEnd;
}
if (startX == endX && startY == endY)
{
return;
}
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(startX, (endX - startX), DateTimeIntervalType.Auto, true);
chart1.ChartAreas[0].AxisY.ScaleView.Zoom(startY, (endY - startY), DateTimeIntervalType.Auto, true);
}