Enable scrolling in x-axis for chart control

2019-06-03 00:50发布

问题:

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

回答1:

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;