I have a MsSql database which calculates the timespan between two dates in seconds. That works fine. I use this column afterwards in C# and write them in an array.
This
UPDATE: This answer may be quite useful for other readers, but it pretty much misses the OP's issues. I'll leave it as it stands, but it will not help in creating specially formatted y-axis labels..
Most Chart problems stem from invalid or useless x-values. The following discussion tries to help avoiding or getting around them..
A number is a number and you can't simply display it as a DateTime
, or for that matter a TimeSpan
.
So you need to add the X-Values
as either DateTime
or as double
that contain values that can be converted to DateTime
. The fomer is what I prefer..
So instead of adding the seconds directly add them as offsets from a given DateTime
:
Change something like this
series.Points.AddXY(sec, yValues);
To this:
var dt = new DateTime(0).AddSeconds(sec);
series.Points.AddXY(dt, yValues);
Now you can use the date and time formatting strings as needed..:
chartArea.AxisX.LabelStyle.Format = "{mm:ss}";
You could also add them as doubles
that actually are calculated from DateTimes
via the ToOADate
:
series.Points.AddXY(dt.ToOADate(), yValues);
But now you will have to set the ChartValueType.DateTime
and probably also AxisX.IntervalType
and AxisX.Interval
to make sure the chart gets the formatting right..:
s.XValueType = ChartValueType.DateTime;
ca.AxisX.Interval = 5;
ca.AxisX.IntervalType = DateTimeIntervalType.Seconds;
ca.AxisX.LabelStyle.Format = "{mm:ss}";
Pick values that suit your data!
Note that the problem with your original code is that the X-Values
internally always are doubles
, but the seconds are not integer values in them but fractional parts; so you need some kind of calculation. That's what ToOADate
does. Here is a short test that shows what one second actually does amount to as a OADate double
:
Best add the X-Values
as DateTimes
so all further processing can rely on the type..
Update I just saw that you have finally added the real code to your question and that is uses Points.DataBindY
. This will not create meaningful X-Values, I'm afraid. Try to switch to Points.DataBindXY
! Of course the X-Values you bind to also need to follow the rules I have explained above..!
You can do a loop over your array and convert the numbers like I shown above; here is a simple example:
int[] seconds = new int[5] { 1, 3, 88, 123, 3333 };
double[] oaSeconds = seconds.Select(x => new DateTime(0).AddSeconds(x).ToOADate())
.ToArray();