Page 1 of 1

Teechart Can't Handle Dates less than 1900???

Posted: Sun Mar 02, 2014 12:52 am
by 13049545
DrawingBackwards.png
Years in the 1800's dates print backwards.
DrawingBackwards.png (29.82 KiB) Viewed 6240 times
I sell forecasts online and use this chart -- everything's been fine.
I created a forecast and dates dip into the 1800's and everything printed backwards on the chart.

Here's test code to show the problem:
vb.net

Dim dtTest as DateTime

dtTest = CDate("1/1/1809 2:00 am")
Me.TChart1.Series(0).Add(dtTest, 2)

dtTest = CDate("1/1/1809 3:00 am")
Me.TChart1.Series(0).Add(dtTest, 3)

dtTest = CDate("1/1/1809 4:00 am")
Me.TChart1.Series(0).Add(dtTest, 4)

Chart is defined with bottom axis as datetime and so is fastline series.

If there's a limitation on printing dates in 1800's I didn't see it in the documentation and this control would then be worthless to me.

I"m hoping it's some trick I didn't know or something simple.

thanks in advance anyone who can help me get past this challenge.

Joseph

Re: Teechart Can't Handle Dates less than 1900???

Posted: Mon Mar 03, 2014 9:43 am
by Christopher
jzarech wrote:If there's a limitation on printing dates in 1800's I didn't see it in the documentation and this control would then be worthless to me.
The explanation for this behaviour is as follows. CDate("1/1/1809 2:00 am") returns a negative double value, as can be seen in this simple test:

Code: Select all

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dtTest As DateTime
    dtTest = CDate("1/1/1809 2:00 am")

    MessageBox.Show(dtTest.ToOADate().ToString())
  End Sub


So when running code such as the following:

Code: Select all

  Private Sub InitializeChart()
    Dim dtTest As DateTime
    Dim series As Line

    series = New Line(tChart1.Chart)
    dtTest = CDate("1/1/1809 2:00 am")
    series.Add(dtTest, 2)

    dtTest = CDate("1/1/1809 3:00 am")
    series.Add(dtTest, 3)

    dtTest = CDate("1/1/1809 4:00 am")
    series.Add(dtTest, 4)

    series.XValues.DateTime = True
    tChart1.Axes.Bottom.Labels.Angle = 90
    tChart1.Axes.Bottom.Labels.DateTimeFormat = "dd/MM/yyyy hh:mm:ss"
  End Sub
where each consecutive XValue is a larger negative number, then the series plots 'correctly' with each larger negative number further to the left. There is a simple workaround to this that will need a little explanation. The zero point at which DateTime values cease to be negative can be seen using this code:

Code: Select all

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dtTest As DateTime
    dtTest = DateTime.FromOADate(0.0)

    MessageBox.Show(dtTest.ToShortDateString() + " " + dtTest.ToShortTimeString())
  End Sub
any DateTime value greater than "31/12/1899 0:00" will be positive and so more recent DateTime values than this will be plotted as larger positive numbers, that is to say, to the right of the previous ones. So, for plots involving DateTime values smaller than this we can use:

Code: Select all

tChart1.Axes.Bottom.Inverted = True
to invert the bottom axis and have larger negative values plot to the right of smaller negative values as if they were positive values, e.g.

Code: Select all

  Private Sub InitializeChart()
    Dim dtTest As DateTime
    Dim series As Line

    series = New Line(tChart1.Chart)
    dtTest = CDate("1/1/1809 2:00 am")
    series.Add(dtTest, 2)

    dtTest = CDate("1/1/1809 3:00 am")
    series.Add(dtTest, 3)

    dtTest = CDate("1/1/1809 4:00 am")
    series.Add(dtTest, 4)

    series.XValues.DateTime = True
    tChart1.Axes.Bottom.Labels.Angle = 90
    tChart1.Axes.Bottom.Labels.DateTimeFormat = "dd/MM/yyyy hh:mm:ss"

    tChart1.Axes.Bottom.Inverted = True
  End Sub