Sunday, April 23, 2006

ZedGraph and using time for the X axis.

The article regarding ZedGraph is

A flexible charting library for .NET


ZedGraph uses a variation of the C# DateTime type called XDate.
The current scheam is to leave all dates in C# DateTime type and
the convert them to XDate for graphing.

In mysql the date/time field is
of type datetime and has a default value of 0000-00-00 00:00:00

In C# use the type DateTime.

DateTime timeStamp;

To format it for the mysql query use.

timeStamp.ToString("yyyy-MM-dd HH:mm:ss")
see Parsing Dates and Times in .NET
The code to get the timeStamp back into a C# DateTime type is

MyCommand.CommandText = "SELECT * FROM Temperature ORDER BY idx";
OdbcDataReader MyDataReader;
MyDataReader = MyCommand.ExecuteReader();
while (MyDataReader.Read())
{
string a = MyDataReader.GetString(4); // where the datetime value is in the 5th position
System.IFormatProvider frmt = new System.Globalization.CultureInfo("en-US", true);
DateTime dt = DateTime.ParseExact(a, "yyyy-MM-dd HH:mm:ss", frmt);
....
}

To get ZedGraph setup to use time on the x axis.

// get a reference to the GraphPane
GraphPane myPane = zg1.GraphPane;

// Set the Titles
myPane.Title = "Temperature Data";
myPane.XAxis.Title = "Time";
myPane.YAxis.Title = "Temperature";
myPane.XAxis.Type = AxisType.Date;

There are other options regarding what one can set for x-axis and time but the one line is all you should need if you are feeding it time in the XDate format.

0 Comments:

Post a Comment

<< Home