Search code examples
c#winformszooming

How I can control the Zoom ability of a chart in winform C#?


I have a chart, in it there is one chartarea with x-axis y-axis. First of all, I have to set it to zoomable,

chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;

By default, if I select a rectangular area using the mouse, the chart will zoom to the selected area. But this is quite annoying because it is prone to false operation. But if I do this:

chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = false;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false;

The Axes won't zoom, even if I call

chart1.ChartAreas[0].AxisX.ScaleView.Zoom(a, b);

So, I want the chartarea to be zoomable, but I don't like the mouse selection ability.

I found a method,

void chart1_SelectionRangeChanged(object sender, CursorEventArgs e)

It seems that when I select a new area, this method will be called, but it is not meant to be override. What can I do? Thank you!


Solution

  • try this:

    var ca = chart1.ChartAreas["ChartArea1"];
    ca.CursorX.IsUserEnabled = false;
    ca.CursorX.IsUserSelectionEnabled = false;
    

    (and same for CursorY, and replacing "ChartArea1" with the name of your chart area if it's different).

    This will disable the mouse selection, so you won't risk accidental zooming anymore.