Zooming & selection not working for C# chart with x-axis of the type Time

AntonioJunior picture AntonioJunior · Jul 19, 2012 · Viewed 11.2k times · Source

I'm using the following code to allow my parts of chart to be selected and zoomed:

chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;

chart1.ChartAreas[0].CursorY.IsUserEnabled = true;
chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisY.ScrollBar.IsPositionedInside = true;

It works for a chart with both x and y axes of the type Integer.

enter image description here

But when I set the series value type to series1.XValueType = ChartValueType.Time;, I won't be able to zoom in the x-axis.

enter image description here

When I set both axes value types for auto and add points using, for example, chart1.Series[0].Points.AddXY(DateTime.Now.ToLongTimeString(), rand.Next(10, 20));, then the zoom works.

How can I have my x-axis to be a Time and still be able to zoom?

Answer

Donald picture Donald · Sep 20, 2012

You need to set the Interval on the Cursor by default it is 1.0, I had the problem with percentages so I set my interval to 0.01 e.g 1% as shown below. You want want to set IntervalType on the Cursor as this accepts datetime types.

chart1.ChartAreas[0].CursorX.Interval = 0.01

More info at http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.cursor.interval%28v=vs.100%29.aspx