Working with Date and Time Values
To use date and time values in the Chart control at design time, set the Series.XValueType or Series.YValueType property to DateTime. This enables you to set the X value or Y value(s) to a string format like "11/21/2008" in the Properties window. You can also preview the formatting of the axis labels in the design area.
After you set the Series.XValueType or Series.YValueType property to DateTime in the Properties window, you can also set the respective axis' Axis.Minimum and Axis.Maximum properties to date and time formatted values.
To adjust the formatting of labels for date and time values, use the Series.LabelFormat or DataPoint.LabelFormat property. To adjust the formatting of the axis labels, use the Axis.LabelStyle.Format property. You can use all standard and custom date and time formatting strings supported by the DateTime structure. For example, the standard formatting string "D" corresponds to the long date pattern in your operating system's regional settings.
For more information on date and time formatting strings, see Date and Time Format Strings.
Working with Date and Time Values at Run Time
In the Chart control, all date and time values are stored as double. To convert between DateTime and double values, use the FromOADate and ToOADate methods in the DateTime structure.
The following code demonstrates how to convert between double and DateTime values in the Chart control.
Imports System.Web.UI.DataVisualization.Charting
…
Chart1.Series(0).XValueType = ChartValueType.DateTime;
Dim x As New System.DateTime(2008, 11, 21)
Chart1.Series(0).Points.AddXY(x.ToOADate(), 34)
…
Dim dt As System.DateTime = System.DateTime.FromOADate(Chart1.Series(0).Points(0).XValue)
using System.Web.UI.DataVisualization.Charting;
…
Chart1.Series[0].XValueType = ChartValueType.DateTime;
System.DateTime x = new System.DateTime(2008, 11, 21);
Chart1.Series[0].Points.AddXY(x.ToOADate(), 34);
…
System.DateTime dt = System.DateTime.FromOADate(Chart1.Series[0].Points[0].XValue);
Aligning Grid Lines, Tick Marks, and Labels
When plotting data with date and time values, the Chart control may not align the axis' grid lines, tick marks, and labels with their respective data points. For example, the first data point on the chart may have an X value of "November 21", but its corresponding grid line, tick mark, and label may be "January 1". To align these items correctly, use the Axis.IntervalOffset and Axis.InterOffsetType properties.
See Also
Reference
System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting
DateTime
Other Resources
Build Date:
2012-08-02