Jump to content
Software FX Community

X axis as a date


Recommended Posts

I'm staring with data from SQL server and displaying a chart that has SQL DateTime as the x axis. 


string dataQueryString = "Select * from dbo.Data;";

dataCommand = new SqlCommand(dataQueryString, connection);

dataAdapter = new SqlDataAdapter(dataCommand); 

System.Data.DataSet dataDS = new System.Data.DataSet();

dataAdapter.Fill(dataDS, "Data");


Chart1.DataSourceSettings.Fields.Add(new FieldMap("date", FieldUsage.XValue));

Chart1.DataSourceSettings.Fields.Add(new FieldMap("rValue", FieldUsage.Value));


Chart1.DataSource = dataDS.Tables[0];


This works fine and the x axis has the correct date values (e.g. 1/1/2000, 1/2/2000, etc).


I then do some processing based on user input, create a DataTable with the new values and then re-display the chart with the new DataTable.


At this point I can no longer get the x axis to understand it is a date. If I just copy the date values from the old table to the new I get the following error.


Input string was not in a correct format.   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info,

 Boolean parseDecimal)

at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)

at System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info)

at ChartFX.WinForms.DataSourceSettings.a(TypeCode A_0, Object A_1)

at ChartFX.WinForms.DataSourceSettings.a(IDataEx A_0, FieldMapCollection A_1, PropertyDescriptorCollection A_2, b A_3, Boolean A_4)

at ChartFX.WinForms.DataSourceSettings.a()

at ChartFX.WinForms.DataSourceSettings.e()

at ChartFX.WinForms.DataSourceSettings.set_DataSource(Object value)

at ChartFX.WinForms.Chart.set_DataSource(Object value)


I can fix this by converting the date into a double value serial date like excel uses (e.g. 12/15/2005 = 38701.00) but that is of course just a numeric value and the x axis no longer displays as a date.


Any ideas?


Link to comment
Share on other sites

When you say "and then re-display the chart with the new DataTable" are you doing this in a new chart? Are you creating a date-time column in your new DataTable? If the new table's column is not named "date" you may have to clear the fields collection and setup new FieldMaps.

Also I would recommend you try setting the X axis format as follows

chart1.AxisX.LabelsFormat.Format = AxisFormat.DateTime

This might help if you try using double numbers (note that we currently use .ToOADate and but this might change in future versions).



Link to comment
Share on other sites

I was using the same chart. It works both ways you suggested

Changing either

benchmarkTable.Columns[0].DataType = typeof(DateTime); and use at DateTime object in the row


Chart1.AxisX.LabelsFormat.Format = AxisFormat.DateTime; and use the the .ToOADate on the DateTime object.

Thanks for your help!


Link to comment
Share on other sites

Internally we use ToOADate and FromOADate when handling dates so if you need to create a chart with precision at the millisecond level, you will have to

1) use doubles in the X axis, using a different conversion method, e.g. you could use .ToFileTimeUTC

2) turn on the Notify property in the X axis and process the chart's GetAxisLabel event, in this event you will return a string e.g. using .FromFileTimeUTC

3) By doing this you will have to manually set your steps, I am guessing that in a chart where milliseconds are important you will typically display less than a date worth of data so losing our "auto steps" calculation will not be a substantial loss in this scenario.



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...