-
Notifications
You must be signed in to change notification settings - Fork 5
/
statistik_u.pas
358 lines (316 loc) · 12.1 KB
/
statistik_u.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
unit statistik_u;
{$I videlibrilanguageconfig.inc}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,diagram,applicationconfig,
StdCtrls, Buttons,applicationformconfig;
type
{ TstatistikForm }
TstatistikForm = class(TVideLibriForm)
Button1: TButton;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
mausInfo: TLabel;
PaintBox1: TPaintBox;
Panel1: TPanel;
Panel2: TPanel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure CheckBox2Change(Sender: TObject);
procedure ComboBox1Select(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure PaintBox1MouseMove(Sender: TObject; {%H-}Shift: TShiftState; X,
Y: Integer);
procedure PaintBox1Paint(Sender: TObject);
procedure PaintBox1Resize(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ private declarations }
procedure translateDate({%H-}sender: TAxis; value: extended; var translated: string);
public
{ public declarations }
diagramDrawer: TDiagramDrawer;
diagramModel: TDiagramDataListModel;
firstYear: longint;
procedure updateStatistic;
end;
const
DIAGRAM_DAYS=0;
DIAGRAM_WEEKS=1;
DIAGRAM_MONTHS=2;
DIAGRAM_YEARS=3;
var
statistikForm: TstatistikForm;
implementation
uses bookwatchmain,bbutils,math,booklistreader,libraryParser,applicationdesktopconfig;
{ TstatistikForm }
procedure TstatistikForm.translateDate(sender: TAxis; value: extended; var translated: string);
begin
case combobox1.itemindex of
DIAGRAM_WEEKS: translated:= inttostr(dateWeekOfYear(round(value)));
//2: translated:= dateTimeFormat('m',value);
DIAGRAM_MONTHS: //translated:= format('%.2d-%.2d',[firstYear+(firstMonth+value) div 12, 1+(firstMonth+value-1) mod 12]);
translated:= format('%.2d',[1+round(value) mod 12]);
DIAGRAM_YEARS:
translated:= IntToStr(round(value));
else translated:= dateTimeFormat('d.m',round(value));
end;
end;
procedure TstatistikForm.updateStatistic;
var i,j,k:integer;
checkDate: longint;
y,m,d,y2,m2,d2,y3,m3,d3: word; //year,..
books: TBookLists;
book:TBook;
earliestDay,lastDay: integer;
showSum: boolean;
accountValues,totalValues: array of longint;
onlyFirstDay: boolean;
begin
if diagramDrawer=nil then exit;
showSum:=CheckBox1.Checked;
diagramModel.deleteLists;
diagramModel.Flags:=[mfEditable];
//Get limits (first known day) and add datalists
earliestDay:=lastCheck;
lastDay:=lastCheck;
for i:=0 to accounts.Count-1 do begin
with (accounts[i]) do begin
for j:=0 to books.current.Count-1 do begin
if (books.current[j].issueDate>0) and (books.current[j].issueDate<earliestDay) then
earliestDay:=books.current[j].issueDate;
if (books.current[j].firstExistsDate>0) and (books.current[j].firstExistsDate<earliestDay) then
earliestDay:=books.current[j].firstExistsDate;
end;
for j:=0 to books.old.Count-1 do begin
if (books.old[j].issueDate>0) and (books.old[j].issueDate<earliestDay) then
earliestDay:=books.old[j].issueDate;
if (books.old[j].firstExistsDate>0) and (books.old[j].firstExistsDate<earliestDay) then
earliestDay:=books.old[j].firstExistsDate;
end;
diagramModel.addDataList.Title:=prettyName;
end;
end;
if showSum then diagramModel.addDataList.Title:='Summe';
// exit;
onlyFirstDay := ComboBox2.ItemIndex = 1;
case ComboBox1.itemIndex of
DIAGRAM_WEEKS: begin
earliestDay:=earliestDay-earliestDay mod 7;
lastDay:=lastDay+7-lastDay mod 7;
SetLength(accountValues,(max(lastDay,currentDate)-earliestDay) div 7+1);
SetLength(totalValues,length(accountValues));
FillChar(totalValues[0],length(totalValues)*sizeof(totalValues[0]),0);
for j:=0 to accounts.Count-1 do begin
FillChar(accountValues[0],length(accountValues)*sizeof(accountValues[0]),0);
books:=TCustomAccountAccess(accounts[j]).books;
for k:=-books.old.count to books.current.count-1 do begin //loop about union
if k<0 then book:=books.old[-k-1]
else book:=books.current[k];
checkdate:=book.issueDate div 7;
if checkDate = 0 then checkDate:=book.firstExistsDate div 7;
if checkDate <= 0 then continue;
while (checkdate<=book.lastExistsDate div 7) do begin
accountValues[checkdate-earliestDay div 7]+=1;
totalValues[checkdate-earliestDay div 7]+=1;
checkdate+=1;
if onlyFirstDay then break;
end;
end;
for i:=0 to high(accountValues) do
diagramModel.addData(j,i*7+earliestDay,accountValues[i]);
end;
if showSum then
for i:=0 to high(totalValues) do
diagramModel.addData(diagramModel.dataRows-1,i*7+earliestDay,totalValues[i]);
end;
DIAGRAM_MONTHS: begin
DecodeDate(earliestDay,y,m,d);
DecodeDate(max(lastDay,currentDate),y2,m2,d2);
SetLength(accountValues,y2*12+m2-1 - (y*12+m-1) + 1);
SetLength(totalValues,length(accountValues));
FillChar(totalValues[0],length(totalValues)*sizeof(totalValues[0]),0);
for j:=0 to accounts.Count-1 do begin
FillChar(accountValues[0],length(accountValues)*sizeof(accountValues[0]),0);
books:=(accounts[j]).books;
for k:=-books.old.count to books.current.count-1 do begin //loop about union
if k<0 then book:=books.old[-k-1]
else book:=books.current[k];
checkdate:=book.issueDate;
if checkDate = 0 then checkDate:=book.firstExistsDate;
if checkDate <= 0 then continue;
DecodeDate(checkDate,y2,m2,d2);
DecodeDate(book.lastExistsDate,y3,m3,d3);
for i:=y2*12+m2-1 - (y*12+m-1) to y3*12+m3-1 - (y*12+m-1) do begin
accountValues[i]+=1;
totalValues[i]+=1;
checkdate+=1;
if onlyFirstDay then break;
end;
end;
for i:=0 to high(accountValues) do
diagramModel.addData(j,i+y*12+m-1,accountValues[i]);
end;
if showSum then
for i:=0 to high(totalValues) do
diagramModel.addData(diagramModel.dataRows-1,i+y*12+m-1,totalValues[i]);
end;
DIAGRAM_YEARS: begin
DecodeDate(earliestDay,y,m,d);
DecodeDate(max(lastDay,currentDate),y2,m2,d2);
SetLength(accountValues,y2 - y + 1);
SetLength(totalValues,length(accountValues));
FillChar(totalValues[0],length(totalValues)*sizeof(totalValues[0]),0);
for j:=0 to accounts.Count-1 do begin
FillChar(accountValues[0],length(accountValues)*sizeof(accountValues[0]),0);
books:=(accounts[j]).books;
for k:=-books.old.count to books.current.count-1 do begin //loop about union
if k<0 then book:=books.old[-k-1]
else book:=books.current[k];
checkdate:=book.issueDate;
if checkDate = 0 then checkDate:=book.firstExistsDate;
if checkDate <= 0 then continue;
DecodeDate(checkDate,y2,m2,d2);
DecodeDate(book.lastExistsDate,y3,m3,d3);
for i:=y2 to y3 do begin
accountValues[i-y]+=1;
totalValues[i-y]+=1;
if onlyFirstDay then break;
end;
end;
for i:=0 to high(accountValues) do
diagramModel.addData(j,i+y,accountValues[i]);
end;
if showSum then
for i:=0 to high(totalValues) do
diagramModel.addData(diagramModel.dataRows-1,i+y,totalValues[i]);
end;
else begin//iterate about days
SetLength(accountValues,max(lastDay,currentDate)-earliestDay+1);
SetLength(totalValues,length(accountValues));
FillChar(totalValues[0],length(totalValues)*sizeof(totalValues[0]),0);
for j:=0 to accounts.Count-1 do begin
FillChar(accountValues[0],length(accountValues)*sizeof(accountValues[0]),0);
books:=(accounts.Objects[j] as TCustomAccountAccess).books;
for k:=-books.old.count to books.current.count-1 do begin //loop about union
if k<0 then book:=books.old[-k-1]
else book:=books.current[k];
checkdate:=book.issueDate;
if checkDate = 0 then checkDate:=book.firstExistsDate;
if checkDate <= 0 then continue;
while checkdate<=book.lastExistsDate do begin
accountValues[checkdate-earliestDay]+=1;
totalValues[checkdate-earliestDay]+=1;
checkdate+=1;
end;
end;
for i:=0 to high(accountValues) do
diagramModel.addData(j,i+earliestDay,accountValues[i]);
end;
if showSum then
for i:=0 to high(totalValues) do
diagramModel.addData(diagramModel.dataRows-1,i+earliestDay,totalValues[i]);
end;
end;
diagramDrawer.AutoSetRangeY:=false;
diagramDrawer.RangeMinY:=0;
diagramDrawer.RangeMaxY:=diagramModel.maxY;
diagramDrawer.update();
PaintBox1.Refresh;
end;
procedure TstatistikForm.FormCreate(Sender: TObject);
begin
diagramDrawer:=nil;
ComboBox1.ItemIndex:=0;
end;
procedure TstatistikForm.Button1Click(Sender: TObject);
begin
close;
end;
procedure TstatistikForm.CheckBox1Click(Sender: TObject);
begin
updateStatistic;
end;
procedure TstatistikForm.CheckBox2Change(Sender: TObject);
begin
if diagramDrawer=nil then exit;
if CheckBox2.Checked then diagramDrawer.FillStyle:=fsMinOverMax
else diagramDrawer.FillStyle:=fsNone;
diagramDrawer.update;
PaintBox1.refresh;
end;
procedure TstatistikForm.ComboBox1Select(Sender: TObject);
begin
updateStatistic;
end;
procedure TstatistikForm.FormDestroy(Sender: TObject);
begin
if diagramDrawer=nil then exit;
diagramDrawer.Free;
end;
procedure TstatistikForm.FormResize(Sender: TObject);
begin
end;
procedure TstatistikForm.FormShow(Sender: TObject);
begin
//updateStatistic;
end;
procedure TstatistikForm.PaintBox1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var day,year,month:longint;
begin
if diagramDrawer=nil then exit;
case ComboBox1.ItemIndex of
DIAGRAM_DAYS: begin
day:=round(diagramDrawer.posToDataX(x));
mausInfo.Caption:=DateToSimpleStr(day);
end;
DIAGRAM_WEEKS: begin
day:=round(diagramDrawer.posToDataX(x));
mausInfo.Caption:=DateToSimpleStr(day-DayOfWeek(day))+' - '+DateToSimpleStr(day+7-DayOfWeek(day));
end;
DIAGRAM_MONTHS: begin
month:=round(diagramDrawer.posToDataX(x));
year:=month div 12;
month:=month mod 12+1;
mausInfo.Caption:=DateToSimpleStr(EncodeDate(word(year),word(month),1))+' - '+DateToSimpleStr(IncMonth(EncodeDate(word(year),word(month),1))-1);;
end;
DIAGRAM_YEARS: begin
year:=round(diagramDrawer.posToDataX(x));
mausInfo.Caption:=IntToStr(year);
end;
end;
mausInfo.Caption := mausInfo.Caption + ': '+inttostr(round(diagramDrawer.posToDataY(y)));
end;
procedure TstatistikForm.PaintBox1Paint(Sender: TObject);
begin
if diagramDrawer=nil then exit;
PaintBox1.Canvas.Draw(0,0,diagramDrawer.Diagram);
end;
procedure TstatistikForm.PaintBox1Resize(Sender: TObject);
begin
if diagramDrawer=nil then exit;
diagramDrawer.Diagram.SetSize(PaintBox1.Width,PaintBox1.Height);
updateStatistic;
end;
procedure TstatistikForm.Timer1Timer(Sender: TObject);
begin
//workaround for LCL bug 18524
timer1.Enabled:=false;
if diagramDrawer<>nil then exit;
diagramDrawer:=TDiagramDrawer.create;
diagramModel:=TDiagramDataListModel.create;
diagramDrawer.SetModel(diagramModel,true);
diagramDrawer.BottomAxis.valueTranslate:=@translateDate;
diagramDrawer.Diagram.SetSize(PaintBox1.Width,PaintBox1.Height);
updateStatistic;
end;
initialization
{$I statistik_u.lrs}
end.