Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Axis.LabelFormat doesn't convert double to DateTime correctly

I just want the time of my samples to appear on the x axis of plots I am creating with Measurement Studio 7.0 and C#.Net.

I am creating a ScatterPlot and filling it with the PlotXY(double[] xData, double[] yData) method. The xaxis is a set of Dates brought in from a database as strings and converted to standard DateTime objects:

DateTime rtime = Convert.ToDateTime(dRow["result_time"]);

I then fill the xData with this DataTime converted to a double, as that is all PlotXY will accept:

xData[i] = rtime.ToOADate();

Note: This worked fine using the ActiveX components of Measurement Studio 6.0, but fails when using the new .Net components in MStudio 7.0.

I go into the XAxis Properties and
change my MajorDivisions.LabelFormat to a DateTime format such as "M/d/yy h:mm" and all I get are bogus dates such as 1/1/00 10:31 when it should be 9/23/03 12:05

Please advise on how to convert DateTime to a double such that the Axis formatting will be correct.
0 Kudos
Message 1 of 3
(4,252 Views)
The Measurement Studio .NET graphs do not use OLE Automation (the OA in From/ToOADate) dates for the conversion factor between DateTime and double. OA date was natural for the ActiveX controls in Measurement Studio 6.0 since they are COM components, but there is not a precedent in .NET for what the double representation of a DateTime should be other than From/ToOADate, which are really convenience methods for interop with COM interfaces.

The DateTime/double conversion factor that the Measurement Studio graphs use is a double value of 0.0 is equivalent to DateTime.MinValue and a double interval of 1.0 is equivalent to 1 second. The NationalInstruments.DataConverter class will convert from/to double and DateTime and TimeSpan with this conversion factor. F
or example, to convert a DateTime to a double as in your example above, you could do this:

DateTime rtime = Convert.ToDateTime(dRow["result_time"]);
xData[i] = (double)DataConverter.Convert(rtTime, typeof(double));

DataConverter will also convert arrays of DateTime to double, like this:

DataTime[] rtimes;
// Initialize rtimes with an array of DateTime values.
xData = (double[])DataConverter.Convert(rtimes, typeof(double[]));

I will file a suggestion to improve the documentation regarding this conversion and its use with the graph in a future release.

Also, you may want to look at using the WaveformGraph instead of the ScatterGraph. The WaveformGraph and WaveformPlot plot methods have overloads for specifying DateTime and TimeSpan values for the start and increment.

Hope this helps.

- Elton
Message 2 of 3
(4,252 Views)
Great answer. Super quick. Thanks!!!
0 Kudos
Message 3 of 3
(4,252 Views)