开发者

MS-Chart Zooming of x-axis with scroll bar in c#

开发者 https://www.devze.com 2023-04-06 20:59 出处:网络
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. But now my requirement is, I need zooming for both

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. 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.

private void chart1_开发者_JAVA技巧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#..


Try using 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, i.e. if you have zoomed more than one time, it returns just to the previous zoomed view.

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.

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...).


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. 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!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号