I have a SQL database that stores some data that I would like to chart. The problem is, I inherited this database and they store the datetime values as Ticks. When I set my chart datasource to this table, it doesn't seem to understand ticks.
How do I get my chart to convert the ticks back to a DateTime format that my chart understands?
My SQL query and code:
static public DataTable get_I1(RunningTests rt)
{
DataTable dt = new DataTable();
using (SqlConnection cs = new SqlConnection(connString))
{
string query = string.Format("SELECT Time_Stamp, I1 FROM Test WHERE Unit_ID = '{0}' AND Time_Stamp >= '{1}' AND Time_Stamp <= '{2}'", rt.Unit_ID, rt.StartTime.Ticks, rt.StopTime.Ticks);
Console.WriteLine(query);
SqlCommand cmd = new SqlCommand(query, cs);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
dt.DefaultView.Sort = "Time_Stamp DESC";
dt = dt.DefaultView.ToTable();
return dt;
}
My code to set my chart datasource:
private void do_chart_I1(RunningTests rt)
{
muCalGUI1.chartI1.Series.Clear();
DataTable dt = SQL.get_I1(rt);
muCalGUI1.chartI1.DataSource = dt;
Series s = new Series("I1");
s.XValueMember = "Time_Stamp";
s.YValueMembers = "I1";
s.ChartType = SeriesChartType.Line;
s.BorderWidth = 2;
s.MarkerSize = 5;
s.MarkerStyle = MarkerStyle.Circle;
muCalGUI1.chartI1.ChartAreas[0].AxisY.IsStartedFromZero = false;
muCalGUI1.chartI1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd\nHH:mm:ss";
muCalGUI1.chartI1.ChartAreas[0].AxisY.LabelStyle.Format = "0";
muCalGUI1.chartI1.ChartAreas[0].RecalculateAxesScale();
muCalGUI1.chartI1.Series.Add(s);
muCalGUI1.chartI1.Legends.Clear();
}