Search code examples
c#datetimetimespanteechart

How to add negative timestamps to TeeChart X axis with C#?


I have data with relative timestamp (as TimeSpan). Example:

RelativeTimestamp (hh:mm) | Data1 | Data2
-00:03                      2.2     1.3
-00:01                      2.5     1.5
 00:00                      2.4     1.6
 00:02                      2.7     1.7
 00:08                      2.1     1.9

I want to make a TeeChart with C# that draws these series of data. However when I try

series.Add(row["RelativeTimestamp"], row["Data1"]);

It complains that I cannot use TimeStamp on horizontal axis. So I also tried converting it to DateTime with

DateTime RelativeTimestamp_DT = row["RelativeTimestamp"] + (new DateTime(1970,1,1));

but, of course, this makes the series of timestamps to become 23:57, -23:59 etc instead of anything negative.

So, how can I make negative timestamp labels on X axis?

We can assume that relative timestamp is no larger than 24 hours positive or negative.


Solution

  • I ended up using Series.Add(DateTime X, double Y, string Label), which existence I didn't notice at first. This is nice because I can put the data to right position on X axis, but give it a custom label, even when timestamps are unequally distributed:

    foreach (DataRow in data.Rows) {
        DateTime RelativeTimestamp_DT = DateTime.MinValue + row["RelativeTimestamp"];
    
        string label = row["RelativeTimestamp"].ToString(@"hh\:mm");
        if (row["RelativeTimestamp"] < new TimeSpan(0)) {
            label = "-" + label;
        }
    
        series.Add(row["RelativeTimestamp"], row["Data1"], label);
        series.Add(row["RelativeTimestamp"], row["Data2"], label);
    }