[SOLVED] Plot datetime data from a file lazarus / 55-TAChart ---------------------------------------------------------------------- madepabloh · 2012-09-23 14:03 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! ---------------------------------------------------------------------- Leledumbo · 2012-09-23 14:29 QuoteCould you give me some tips to solve this? Thanks!Yes, give us some code. The error message is quite clear: QuoteIt compiles correctly, but it returns an error what say: "06/12/2011 12:05" is not a valid floatSo 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 · 2012-09-23 15:41 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. ---------------------------------------------------------------------- Leledumbo · 2012-09-23 17:27 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 · 2012-09-23 18:10 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 · 2012-09-23 18:26 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 · 2012-09-24 06:37 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 · 2012-09-28 11:14 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 · 2012-09-28 12:42 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 · 2012-09-28 16:53 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 · 2012-09-28 17:12 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 · 2012-09-28 17:25 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!! ---------------------------------------------------------------------- wp · 2012-09-28 17:33 @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 · 2012-09-28 18:30 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!