3

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

Community
  • 1
  • 1
NewBie
  • 41
  • 1
  • 1
  • 3
  • "is not working properly" is too vague... how it doesn't work ? – digEmAll Sep 25 '11 at 14:28
  • I need to have zooming for both axis. So I tried resetting both the axis when the y-axis zoom reset is clicked in the "chart1_AxisScrollBarClicked(object sender, ScrollBarEventArgs e)" event (since x-axis zoom reset has been disabled). Now Y-axis is working fine but x-axis is not resetting back to the initial view. – NewBie Sep 26 '11 at 06:14
  • I have tried to give you an answer... let me know if you still have problems – digEmAll Sep 26 '11 at 07:10

3 Answers3

4

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);
}
Samuel Parkinson
  • 2,992
  • 1
  • 27
  • 38
3

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

digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • Thanks for responding immediately.The Y-axis should reset to the default view but the x-axis should reset to the initial view ie.chartArea.AxisX.ScaleView.Zoom(0,10); But if I use your suggested code, the X-axis is resetting to a view without scrollbar. – NewBie Sep 27 '11 at 05:30
0

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

NewBie
  • 41
  • 1
  • 1
  • 3