Search code examples
c#winformsmschart

Enable scrolling in x-axis for chart control


I can't seem to enable a scroll bar on x-axis. I'm using the code below to generate the chart.

List<int> xVal = new List<int>();  
List<int> yVal = new List<int>();  
for (int i = 0; i <= maxQueuetime ; i++)  
{  
    xVal.Add(i);  
    yVal.Add(graph2Yaxis[i]);  
}  
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;  
chart1.Series[0]["PointWidth"] = "1";  
chart1.Series[0].Points.DataBindXY(xVal, yVal);

enter image description here


Solution

  • This will let the user drag over a portion of the chart he want to see and then a scrollbar will show up:

    ChartArea CA = chart1.ChartAreas[0];
    CA.CursorX.IsUserSelectionEnabled = true;
    

    To make the scrollbar show up by code use at least this line:

    CA.AxisX.ScaleView.Zoom(firstDataPoint, lastDataPointInView);
    

    Depending on your data you may want to set the ScaleView.SizeType

    CA.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
    

    I suggest adding a little help to tell the user how to zoom in..

    To prevent zooming you can change the default:

    CA.AxisX.ScaleView.Zoomable = false;