[英]MS-Chart Zooming of x-axis with scroll bar in c#
I have added the scroll bar to the x-axis of my mschart control using this link Adding a scroll bar to MS Chart control C# and it worked as expected. 我使用此链接将滚动条添加到了 mschart控件的x轴上。 将滚动条添加到MS Chart控件C# ,它按预期工作。 But now my requirement is, I need zooming for both the axis.
但是现在我的要求是,我需要同时缩放两个轴。 But since I removed Zoom reset button for x-axis, I have used the following to reset it by forcing.
但是,由于我删除了x轴的“缩放重置”按钮,因此已使用以下命令通过强制对其进行重置。
private void chart1_AxisScrollBarClicked(object sender, ScrollBarEventArgs e)
{
// Handle zoom reset button
if(e.ButtonType == ScrollBarButtonType.ZoomReset)
{
// Event is handled, no more processing required
e.IsHandled = true;
// Reset zoom on X and Y axis
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset();
}
}
But it is not working properly. 但是它不能正常工作。 Please help me in fixing this in c#..
请帮助我在C#中修复此问题。
Try using ZoomReset(0)
. 尝试使用
ZoomReset(0)
。
private void zeroZoom_Click(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(0);
chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset(0);
}
The first thing coming in mind, is that your problem is related to multiple zooming. 首先想到的是,您的问题与多重缩放有关。
As you had noticed, by default the zoom-reset button (exactly like the ZoomReset method) doesn't reset the zoom completely, but restore the previous view-status, ie if you have zoomed more than one time, it returns just to the previous zoomed view. 如您所知,默认情况下,缩放重置按钮(与ZoomReset方法完全一样)不会完全重置缩放,而是会还原以前的视图状态,即,如果缩放超过一次,它将仅返回到以前的缩放视图。
To completely reset the zoom, you can use this code: 要完全重置缩放,可以使用以下代码:
while (chart1.ChartAreas[0].AxisX.ScaleView.IsZoomed)
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
while (chart1.ChartAreas[0].AxisY.ScaleView.IsZoomed)
chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset();
Conversely, if you like the default zoom-reset behaviour, you should have two buttons for the two axis because it's possible to have different number of view-statex for the different axis. 相反,如果您喜欢默认的缩放重置行为,则对于两个轴应该有两个按钮,因为对于不同的轴可能有不同数量的view-statex。
Another possibility, is that you are zooming a secondary axis, like AxisX2
or AxisY2
(not sure, but I think that is depending on the chart type), so you should reset those (or, to be safe, just reset all axis...). 另一种可能性是,您正在缩放辅助轴,例如
AxisX2
或AxisY2
(不确定,但是我认为这取决于图表类型),因此您应该重置这些轴(或者,为了安全起见,只需重置所有轴。) )。
I tried with the below code today and it seems like working fine. 我今天尝试使用下面的代码,看起来工作正常。 Here the for loop handles the X axis with scroll and the next if block handles the ordinary X-axis.
这里的for循环使用滚动处理X轴,下一个if块处理普通的X轴。 Could you please have a glance at it and let me know your views about it?
您能看一眼,让我知道您对此的看法吗?
private void chart1_AxisScrollBarClicked(object sender, ScrollBarEventArgs e)
{
Boolean blnIsXaxisReset = false;
try
{
// Handle zoom reset button
if(e.ButtonType == ScrollBarButtonType.ZoomReset)
{
// Event is handled, no more processing required
e.IsHandled = true;
// Reset zoom on Y axis
while (chart1.ChartAreas[0].AxisY.ScaleView.IsZoomed)
chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset();
//Handles Zoom reset on X axis with scroll bar
foreach (Series series in chart1.Series)
{
if (series.YAxisType == AxisType.Secondary)
{
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(-10, 10);
blnIsXaxisReset = true;
break;
}
}
//Handles Zoom reset on ordinary X axis
if (blnIsXaxisReset == false)
{
while (chart1.ChartAreas[0].AxisX.ScaleView.IsZoomed)
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
}
}
}
catch (Exception ex)
{
BuildException buildException = new BuildException();
buildException.SystemException = ex;
buildException.CustomMessage = "Error in zooming the Chart";
ExceptionHandler.HandleException(buildException);
}
}
Thanks for your effort!! 谢谢你的努力!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.