1. If you want to use DateTime values as X, Y or Y2 data for your chart you do that the same way as you would add numeric values. Charting for WPF supports DateTime values given in any of these formats:
C# Copy Code |
---|
DateTime myDateTime = new DateTime(2011, 12, 1); |
VB.NET Copy Code |
---|
Dim myDateTime As New DateTime(2011, 12, 1) |
C# Copy Code |
---|
DateTime myDateTime = new DateTime(2011, 12, 1); |
VB.NET Copy Code |
---|
Dim myDateTime As New DateTime(2011, 12, 1) |
C# Copy Code |
---|
lineSeries[0].XData.Add(myDateTime.Ticks); |
VB.NET Copy Code |
---|
lineSeries(0).XData.Add(myDateTime.Ticks) |
2. You must set the ValueFormat property of the Axis object for the respective X, Y, X2 or Y2 axis at which DateTime data will be shown to ValueFormat.DateTime:
C# Copy Code |
---|
Axis xAxis = new Axis(); |
VB.NET Copy Code |
---|
lineChart1.XAxisSettings.ValueFormat = ValueFormat.DateTime |
3. The start and end values of the respective axis must be set to the desired DateTime value. The properties for that - Axis.MinValue and Axis.MaxValue accept only double values. When the ValueFormat is ValueFormat.DateTime the control expects OLE Automation representation of the values for the properties:
C# Copy Code |
---|
DateTime start = new DateTime(2011, 3, 1); |
VB.NET Copy Code |
---|
Dim start As New DateTime(2011, 3, 1) |
The size of the interval among the scale divisions of the axis depends on the number of divisions we want to see:
C# Copy Code |
---|
xAxis.Interval = (end.ToOADate() - start.ToOADate()) / 8; |
VB.NET Copy Code |
---|
lineChart1.XAxisSettings.Interval = ([end].ToOADate() - start.ToOADate()) / 8 |
4. Then we select the type of labels to appear at the axis and rotate them at 35 degrees because formatted as DateTime values they are too long. We use the LabelRotationAngle property for that:
C# Copy Code |
---|
xAxis.Interval = (end.ToOADate() - start.ToOADate())/8; |
VB.NET Copy Code |
---|
lineChart1.XAxisSettings.Interval = ([end].ToOADate() - start.ToOADate()) / 8 |
5. Finally, don't forget to add the axis to the respective X/Y/X2/Y2 collection:
C# Copy Code |
---|
myLineChart.XAxes.Add(xAxis); |
Using DateTime values in a chart is shown in detail in the DateTimeValues sample that is installed with the control.