Recent

Author Topic: [SOLVED] Plot datetime data from a file  (Read 7579 times)

madepabloh

  • Full Member
  • ***
  • Posts: 160
[SOLVED] Plot datetime data from a file
« on: September 23, 2012, 04:03:13 pm »
Hi folks,

Alfter a long time without to use lazarus, i am back again troing to plot a txt file what contains datetime data. The structure of each line of the file is this (for thousands of rows):
06/12/2011 12:05:01;34.5
what correspond to: dd/mm/yy hh:nn:ss;temperature. The data are saved into an ascii file (txt for example)

I load the data into a dbgrid component without problems, but now i can`t have it plotted into chart.
I followed all the recomendations from the wiki as well as in this forum topic:
http://www.lazarus.freepascal.org/index.php/topic,14186.15.html

It compiles correctly, but it returns an error what say: "06/12/2011 12:05:01" is not a valid float

Could you give me some tips to solve this? Thanks!

« Last Edit: September 28, 2012, 11:46:58 pm by madepabloh »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Plot datetime data from a file
« Reply #1 on: September 23, 2012, 04:29:14 pm »
Quote
Could you give me some tips to solve this? Thanks!
Yes, give us some code. The error message is quite clear:
Quote
It compiles correctly, but it returns an error what say: "06/12/2011 12:05" is not a valid float
So you must have used the datetime string incorrectly, e.g. passing it to something expecting a float, which we wouldn't ever know without seeing what you're doing.

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: Plot datetime data from a file
« Reply #2 on: September 23, 2012, 05:41:33 pm »
You are right @leledumbo,

Here i show an example of what i try to do...
****

I included also a txt file to text the application.
« Last Edit: September 23, 2012, 06:25:26 pm by madepabloh »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Plot datetime data from a file
« Reply #3 on: September 23, 2012, 07:27:28 pm »
It has a problem displaying your datetime as marks, because the dataset uses StrToDateTime which takes DefaultFormatSettings.ShortDateFormat. I haven't found any solution (changing DefaultFormatSettings.ShortDateFormat doesn't help), I'll need to build my rtl/fcl with debugging symbols to get the precise error location... or someone else does it faster than me (it's 00:26 here, I should get some sleep to get to work tomor... I mean, this morning).

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: Plot datetime data from a file
« Reply #4 on: September 23, 2012, 08:10:14 pm »
Thanks @Leledumbo.

Okay, go to sleep. I appreciate any help, but i am not in a hurry, so thanks for any help you could provide.
Cheers,
madepabloh

Ask

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 687
Re: Plot datetime data from a file
« Reply #5 on: September 23, 2012, 08:26:26 pm »
This error comes not from TAChart, but from the dataset.
Your default format settings probably do not correspond to the data you provided.

Depending on your locale, you might need something like this:
Code: [Select]
  DefaultFormatSettings.DateSeparator := '/';
  DefaultFormatSettings.DecimalSeparator := ',';
Also, set TDbChartSource1.Options=[dcsoDateTimeX].

Note, however, that there are further bugs in all three of TSdfDataset, TAChart, and your code. I'll investigate later.
For now, I recommend to use a simple list source and fill data by hand.

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: Plot datetime data from a file
« Reply #6 on: September 24, 2012, 08:37:40 am »
Thanks Ask!

I have my laptop in english configuration, so the decimal separator should not be a problem, but i will check it.

Thanks you for take a look to the code. Any comment will be wellcome.
Cheers,

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: Plot datetime data from a file
« Reply #7 on: September 28, 2012, 01:14:29 pm »
I still dind´t solve the problem, but may be the question now is: how should the data be in the txt file to be readed by my application and plotted in the chart? Because i don´t know if the problme is the datafile or the configuration of the components in lazarus...

Any of you plotted datetime data from a file? How is your file?

Thanks!

wp

  • Hero Member
  • *****
  • Posts: 13401
Re: Plot datetime data from a file
« Reply #8 on: September 28, 2012, 02:42:53 pm »
Code: [Select]
procedure TForm1.LoadFile(const AFileName:String);
var
  i: Integer;
  L: TStringList;
  List: TStringList;
  fmt : TFormatSettings;
  x: TDateTime;
  y: double;

begin
  Chart1LineSeries1.Clear;

  fmt := DefaultFormatSettings;
  fmt.DateSeparator := '/';
  fmt.TimeSeparator := ':';
  fmt.ShortDateFormat := 'm/d/y';
  fmt.LongDateFormat := 'mm/dd/yyyy hh:nn:ss';
  fmt.DecimalSeparator := '.';
  List := TStringList.Create;
  L := TStringList.Create;
  try
    L.Delimiter := ';';
    L.StrictDelimiter := true;
    List.LoadFromFile(AFileName);
    for i:=0 to List.Count-1 do begin
      L.DelimitedText := List[i];
      x := StrToDateTime(L[0], fmt);
      y := StrToFloat(L[1], fmt);
      Chart1LineSeries1.AddXY(x, y);
    end;
  finally
    L.Free;
    List.Free;
  end;
end;

... Runs for me with a data file constructed from the sample you give in the first posting. I am assuming that the first number in the date refers to the month. If not then exchange "d" and "m" (and "dd" and "mm") in above statements for the short and long date format.

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: Plot datetime data from a file
« Reply #9 on: September 28, 2012, 06:53:24 pm »
Thanks WP!

Nice code...

However, i tried to modify the example i uploaded here, and it can´t read the file.

Do you mind to check what i have to see where i have the problem? Thanks!

Ask

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 687
Re: Plot datetime data from a file
« Reply #10 on: September 28, 2012, 07:12:53 pm »
I did modify you code, and with the changes I posted, dataset successfully loads it
(there are problems with displaying in on a chart after that, but the data does get loaded).

So if you do not want to load the data manually, as wp suggested, please try my suggestion.

(Side note: I think TSdfDataset should have its own FormatSettings property, is seems hacky to modify global format settings for it to be able to parse an input file).

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: Plot datetime data from a file
« Reply #11 on: September 28, 2012, 07:25:59 pm »
Thanks Ask!

My problem was where to paste your code in my code... May be OnForm show? I use lazarus very few and i forget from one time to the next one what i know... For that reason, i was trying to read on internet and in the forum before to ask and disturb you.

This is way i still didn´t used you code...

MODIFIED:

@ASK

Now i see what i should do. I will made a misture between your recomendation and the code from wp to apply in my project. Thanks so much!!
« Last Edit: September 28, 2012, 08:32:15 pm by madepabloh »

wp

  • Hero Member
  • *****
  • Posts: 13401
Re: Plot datetime data from a file
« Reply #12 on: September 28, 2012, 07:33:19 pm »
@madepabloh:
  • Skip the header line in you data file by beginning with i=1 instead of 0.
  • Your second data column contains decimal commas instead of points. Set the decimal separator to ',' instead of '.'.
  • Your date contains two-digit years, i.e. you should set the year mask in the LongDateFormat to "yy" instead of "yyyy". However, the yyyy seems to work as well (I don't know why).

This is the modified LoadFile that works here:
Code: [Select]
procedure TForm1.LoadFile(const AFileName:String);
var
  i: Integer;
  L: TStringList;
  List: TStringList;
  fmt : TFormatSettings;
  x: TDateTime;
  y: double;
begin
  Chart1LineSeries1.Clear;
  fmt := DefaultFormatSettings;
  fmt.DateSeparator := '/';
  fmt.TimeSeparator := ':';
  fmt.ShortDateFormat := 'd/m/y';
  fmt.LongDateFormat := 'dd/mm/yy hh:nn:ss';
  fmt.DecimalSeparator := ',';
  List := TStringList.Create;
  L := TStringList.Create;
  try
    L.Delimiter := ';';
    L.StrictDelimiter := true;
    List.LoadFromFile(AFileName);
    for i:=1 to List.Count-1 do begin
      L.DelimitedText := List[i];
      x := StrToDateTime(L[0], fmt);
      y := StrToFloat(L[1], fmt);
      Chart1LineSeries1.AddXY(x, y);
    end;
  finally
    L.Free;
    List.Free;
  end;
end;

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: Plot datetime data from a file
« Reply #13 on: September 28, 2012, 08:30:59 pm »
Okay... i am really a dummy,  the problems were really easy and i didn´t see. Thanks for to solve them for me.

However, it does work. However, i discovered the problem, it was not so easy to detect, but here is the original code:
Code: [Select]
LoadFile(Edit1.Text);and here the solution:
Code: [Select]
LoadFile(UTF8ToAnsi(Edit1.Text));
Now, it works perfect!!
Thanks WP!
« Last Edit: September 28, 2012, 11:42:28 pm by madepabloh »

 

TinyPortal © 2005-2018