-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataAnalysis.pas
594 lines (550 loc) · 14.6 KB
/
DataAnalysis.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
(*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Initial Developer of this code is John Hansen.
* Portions created by John Hansen are Copyright (C) 2009-2013 John Hansen.
* All Rights Reserved.
*
*)
unit DataAnalysis;
interface
uses
Classes, Controls, Forms, Dialogs,
ExtCtrls, TeeProcs, TeEngine, Chart, Series, StdCtrls, Menus, ActnList,
ImgList, ToolWin, ComCtrls, Buttons;
type
TfrmDataAnalysis = class(TForm)
chtData: TChart;
aclMain: TActionList;
imlButtons: TImageList;
actPrint: TAction;
actConfig: TAction;
actExport: TAction;
actCopy: TAction;
actClose: TAction;
dlgPrint: TPrintDialog;
pnlToolbar: TPanel;
pnlLeft: TPanel;
pnlVCR: TPanel;
tbrMain: TToolBar;
btnPrint: TToolButton;
btnConfig: TToolButton;
btnCopy: TToolButton;
btnExport: TToolButton;
btnClose: TToolButton;
bvlVCR: TBevel;
btnFirst: TSpeedButton;
btnPrev: TSpeedButton;
btnNext: TSpeedButton;
btnLast: TSpeedButton;
btnSelSeries: TToolButton;
actSeries: TAction;
actAddCalcSeries: TAction;
btnSep2: TToolButton;
btnAddCalcSeries: TToolButton;
btnSep1: TToolButton;
btnSep4: TToolButton;
btnHelp: TToolButton;
actHelp: TAction;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure actPrintExecute(Sender: TObject);
procedure actConfigExecute(Sender: TObject);
procedure actCopyExecute(Sender: TObject);
procedure actExportExecute(Sender: TObject);
procedure actCloseExecute(Sender: TObject);
procedure chtDataMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure btnFirstClick(Sender: TObject);
procedure btnPrevClick(Sender: TObject);
procedure btnNextClick(Sender: TObject);
procedure btnLastClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure actSeriesExecute(Sender: TObject);
procedure actAddCalcSeriesExecute(Sender: TObject);
procedure actHelpExecute(Sender: TObject);
private
{ Private declarations }
fData: TStrings;
fDataIsXY: Boolean;
FChartType: Integer;
fFromWatch: Boolean;
fWatchPoints: Byte;
fSyncSeries: Boolean;
fNextX : Integer;
fRelTime: Boolean;
fFromNewWatch: Boolean;
procedure ConfigureSeries(S : TChartSeries);
procedure SetData(const Value: TStrings);
procedure PrepareChartXY;
procedure PrepareChartNormal;
procedure SetChartType(const Value: integer);
procedure UpdateVCRButtons;
function PromptForSeries : TChartSeries;
procedure ProcessValue(valStr: string; bTrimSeries : Boolean = false);
function CreateSeries: TChartSeries;
function FindValue(valStr: string): Single;
function FindType(valStr: string): string;
function GetXValue(s : TChartSeries): integer;
function MaxXValue: double;
procedure TrimSeries;
function GetSeriesIndexByName(name: string): integer;
public
{ Public declarations }
procedure AddNewData(aValues : TStrings);
procedure PrepareChart;
property Data : TStrings read fData write SetData;
property DataIsXY : Boolean read fDataIsXY write fDataIsXY;
property UseChartType : Integer read FChartType write SetChartType;
property FromWatch : Boolean read fFromWatch write fFromWatch;
property FromNewWatch : Boolean read fFromNewWatch write fFromNewWatch;
property WatchPoints : Byte read fWatchPoints write fWatchPoints;
property SyncSeries : Boolean read fSyncSeries write fSyncSeries;
property RelativeTime : Boolean read fRelTime write fRelTime;
end;
implementation
{$R *.DFM}
uses
SysUtils, Printers, Clipbrd, uChartExport, uChartConfig, uSeriesConfig,
uPromptSeries, Watch, uNewWatch;
{ TfrmDataAnalysis }
procedure TfrmDataAnalysis.SetData(const Value: TStrings);
begin
fData.Clear;
fData.Assign(Value);
PrepareChart;
end;
procedure TfrmDataAnalysis.FormCreate(Sender: TObject);
begin
fWatchPoints := 10;
fFromWatch := False;
fFromNewWatch := False;
fChartType := 0;
fDataIsXY := False;
fData := TStringList.Create;
fNextX := 0;
fSyncSeries := False;
fRelTime := False;
end;
procedure TfrmDataAnalysis.FormDestroy(Sender: TObject);
begin
if FromWatch and Assigned(WatchForm) then
WatchForm.GraphDestroyed;
if FromNewWatch and Assigned(frmNewWatch) then
frmNewWatch.GraphDestroyed;
fData.Free;
end;
procedure TfrmDataAnalysis.PrepareChart;
begin
// delete any existing series first
while chtData.SeriesCount > 0 do
chtData.Series[0].Free;
// now load new series
if DataIsXY then
PrepareChartXY
else
PrepareChartNormal;
end;
procedure TfrmDataAnalysis.actPrintExecute(Sender: TObject);
begin
if dlgPrint.Execute then
begin
if Printer.Orientation = poLandscape then
chtData.PrintLandscape
else
chtData.Print;
end;
end;
procedure TfrmDataAnalysis.actConfigExecute(Sender: TObject);
begin
with TfrmChartConfig.Create(nil) do
try
ChartType := UseChartType;
TheChart := chtData;
if ShowModal = mrOK then
begin
CopySettings;
UseChartType := ChartType;
chtData.Refresh;
UpdateVCRButtons;
pnlVCR.Visible := chtData.MaxPointsPerPage > 0;
end;
finally
Free;
end;
end;
function TfrmDataAnalysis.GetSeriesIndexByName(name : string) : integer;
var
i : Integer;
begin
Result := -1;
for i := 0 to chtData.SeriesCount - 1 do
begin
if chtData.Series[i].Title = name then
begin
Result := i;
Exit;
end;
end;
end;
procedure TfrmDataAnalysis.actCopyExecute(Sender: TObject);
var
i, j, n : integer;
tmpStr, valStr : string;
dataArray : array of array of string;
begin
if chtData.SeriesCount = 0 then Exit;
// copy to clipboard
tmpStr := '';
for j := 0 to chtData.SeriesCount - 1 do
begin
tmpStr := tmpStr + chtData.Series[j].Title;
if j < chtData.SeriesCount - 1 then
tmpStr := tmpStr + #9;
end;
tmpStr := tmpStr + #13#10;
if DataIsXY then
begin
j := 0;
while j < (fData.Count - 1) do
begin
valStr := fData[j];
// should be only one series and X, Y pairs of data
// so output (x, y)
tmpStr := tmpStr + '(' +
FloatToStr(FindValue(valStr)) + ', ' +
FloatToStr(FindValue(fData[j+1])) + ')'#13#10;
Inc(j, 2);
end;
end
else
begin
// initialize array dimensions
SetLength(dataArray, chtData.SeriesCount);
for i := 0 to chtData.SeriesCount - 1 do
SetLength(dataArray[i], 0);
for j := 0 to fData.Count - 1 do
begin
valStr := fData[j];
i := GetSeriesIndexByName(FindType(valStr));
if i = -1 then Continue;
n := Length(dataArray[i]);
SetLength(dataArray[i], n + 1);
dataArray[i][n] := FloatToStr(FindValue(valStr));
end;
for j := 0 to Length(dataArray[0]) - 1 do
begin
for i := 0 to chtData.SeriesCount - 1 do
begin
if j < Length(dataArray[i]) then
tmpStr := tmpStr + dataArray[i][j];
if i < chtData.SeriesCount - 1 then
tmpStr := tmpStr + #9;
end;
tmpStr := tmpStr + #13#10;
end;
end;
ClipBoard.AsText := tmpStr;
end;
procedure TfrmDataAnalysis.actExportExecute(Sender: TObject);
begin
TfrmChartExport.DoExport(chtData);
end;
procedure TfrmDataAnalysis.actCloseExecute(Sender: TObject);
begin
Close;
end;
function TfrmDataAnalysis.CreateSeries : TChartSeries;
begin
case UseChartType of
0 : Result := TLineSeries.Create( Self );
1 : Result := TBarSeries.Create( Self );
2 : Result := THorizBarSeries.Create( Self );
3 : Result := TAreaSeries.Create( Self );
else
Result := TPointSeries.Create( Self );
end;
Result.ParentChart := chtData;
Result.Marks.Visible := False;
end;
function TfrmDataAnalysis.FindType(valStr : string) : string;
begin
Result := Copy(valStr, 1, Pos(':', valStr)-1);
end;
function TfrmDataAnalysis.FindValue(valStr : string) : Single;
begin
Delete(valStr, 1, Pos(':', valStr));
Result := StrToFloatDef(Trim(valStr), 0.0);
end;
function TfrmDataAnalysis.GetXValue(s : TChartSeries) : integer;
var
x1, x2 : Double;
begin
if RelativeTime then
begin
// The desired X value should be either
// 1) one greater than the max X value in this series
// OR
// 2) the maximum X value of any series other than this series
// (whichever is greater)
x1 := MaxXValue;
x2 := s.Count;
if x2 > 0 then
x2 := s.MaxXValue + 1;
if x2 > x1 then
Result := Trunc(x2)
else
Result := Trunc(x1);
end
else if SyncSeries then
begin
Result := fNextX;
end
else
begin
Result := s.Count;
if Result > 0 then
Result := Trunc(s.MaxXValue) + 1;
end;
end;
function TfrmDataAnalysis.MaxXValue : double;
var
j : Integer;
s : TChartSeries;
tmp : Double;
begin
Result := 0;
for j := 0 to chtData.SeriesCount - 1 do
begin
s := chtData.Series[j];
if s.Count > 0 then
begin
tmp := s.MaxXValue;
if tmp > Result then
Result := tmp;
end;
end;
end;
procedure TfrmDataAnalysis.ProcessValue(valStr : string; bTrimSeries : boolean);
var
j, xVal : integer;
val : Single;
sType : string;
s, sTmp : TChartSeries;
begin
sType := FindType(valStr);
val := FindValue(valStr);
s := nil;
for j := 0 to chtData.SeriesCount - 1 do
begin
sTmp := chtData.Series[j];
if sTmp.Title = sType then
begin
s := sTmp;
Break;
end;
end;
if not Assigned(s) then
begin
// create a new series
s := CreateSeries;
s.Title := sType;
s.XValues.Order := loNone;
end;
if bTrimSeries then TrimSeries;
// now add a data point
xVal := GetXValue(s);
s.AddXY(xVal, val);
end;
procedure TfrmDataAnalysis.TrimSeries;
var
x, maxX : Double;
i : Integer;
cs : TChartSeries;
begin
// delete all data points where X is less than MaxXValue - WatchPoints
for i := 0 to chtData.SeriesCount - 1 do
begin
cs := chtData.Series[i];
if cs.Count > 0 then
begin
maxX := MaxXValue;
x := cs.XValue[0];
while x < (Trunc(maxX) - WatchPoints) do
begin
cs.Delete(0);
if cs.Count = 0 then Break;
x := cs.XValue[0];
end;
end;
end;
end;
procedure TfrmDataAnalysis.PrepareChartNormal;
var
i : integer;
begin
for i := 0 to fData.Count - 1 do
begin
ProcessValue(fData[i]);
end;
end;
procedure TfrmDataAnalysis.PrepareChartXY;
var
i : integer;
s : TChartSeries;
begin
// only one series
s := CreateSeries;
s.Title := 'Datalog';
i := 0;
while i < fData.Count - 1 do
begin
s.AddXY(FindValue(fData[i]), FindValue(fData[i+1]));
inc(i, 2);
end;
end;
procedure TfrmDataAnalysis.SetChartType(const Value: integer);
begin
if FChartType <> Value then
begin
FChartType := Value;
PrepareChart;
end;
end;
procedure TfrmDataAnalysis.chtDataMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
p : TChartClickedPart;
S : TChartSeries;
begin
if (ssCtrl in Shift) and (Button = mbLeft) then
begin
S := nil;
chtData.CalcClickedPart(Point(X, Y), p);
if p.Part = cpSeries then
begin
S := p.ASeries;
end
else if p.Part = cpChartRect then
begin
// prompt for series
S := PromptForSeries;
end;
ConfigureSeries(S);
end;
end;
procedure TfrmDataAnalysis.btnFirstClick(Sender: TObject);
begin
chtData.UndoZoom;
chtData.Page := 1;
UpdateVCRButtons;
end;
procedure TfrmDataAnalysis.btnPrevClick(Sender: TObject);
begin
chtData.UndoZoom;
chtData.PreviousPage;
UpdateVCRButtons;
end;
procedure TfrmDataAnalysis.btnNextClick(Sender: TObject);
begin
chtData.UndoZoom;
chtData.NextPage;
UpdateVCRButtons;
end;
procedure TfrmDataAnalysis.btnLastClick(Sender: TObject);
begin
chtData.UndoZoom;
chtData.Page := chtData.NumPages;
UpdateVCRButtons;
end;
procedure TfrmDataAnalysis.UpdateVCRButtons;
var
p, n : integer;
begin
p := chtData.Page;
n := chtData.NumPages;
btnFirst.Enabled := p > 1;
btnPrev.Enabled := p > 1;
btnNext.Enabled := p < n;
btnLast.Enabled := p < n;
end;
procedure TfrmDataAnalysis.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TfrmDataAnalysis.AddNewData(aValues: TStrings);
var
i : integer;
begin
// create series if needed and add data points
for i := 0 to aValues.Count - 1 do
begin
ProcessValue(aValues[i], True);
end;
fNextX := Trunc(MaxXValue) + 1;
// add data in aValues to fData
fData.AddStrings(aValues);
end;
function TfrmDataAnalysis.PromptForSeries: TChartSeries;
var
i : integer;
begin
result := nil;
i := TfrmSeriesPrompt.PromptForSeries(chtData);
if i <> -1 then
result := chtData.Series[i];
end;
procedure TfrmDataAnalysis.actSeriesExecute(Sender: TObject);
begin
ConfigureSeries(PromptForSeries);
end;
procedure TfrmDataAnalysis.ConfigureSeries(S: TChartSeries);
begin
if not Assigned(S) then Exit;
with TfrmSeriesConfig.Create(nil) do
try
Chart := chtData;
ChartType := UseChartType;
Mode := scmEdit;
TheSeries := S;
if ShowModal = mrOK then
begin
CopySeriesSettings;
chtData.Refresh;
end;
finally
Free;
end;
end;
procedure TfrmDataAnalysis.actAddCalcSeriesExecute(Sender: TObject);
begin
with TfrmSeriesConfig.Create(nil) do
try
Chart := chtData;
ChartType := UseChartType;
Mode := scmAdd;
if ShowModal = mrOK then
begin
// do something here
CopySeriesSettings;
chtData.AddSeries(TheSeries);
chtData.Refresh;
end;
finally
Free;
end;
end;
procedure TfrmDataAnalysis.actHelpExecute(Sender: TObject);
begin
Application.HelpContext(HelpContext);
end;
end.