forked from academiadocodigo/TBGWebCharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Charts.DataSet.pas
380 lines (365 loc) · 11 KB
/
Charts.DataSet.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
unit Charts.DataSet;
interface
uses
Interfaces, DB, SysUtils;
Type
TModelHTMLChartsDataSet = class(TInterfacedObject, iModelHTMLDataSet)
private
FParent : iModelHTMLChartsConfig;
FDataSet : TDataSet;
FLabelName : String;
FValueName : String;
FRGBName : String;
FtextLabel : String;
FBackgroundColor : String;
FBackgroundOpacity : String;
FBorderColor : String;
FBorderOpacity : String;
FBorderWidth : Integer;
FBorderDash : String;
FData : String;
FFill : Boolean;
FHidden: Boolean;
FHideZeroValues : Boolean;
FLineTension : String;
FScript : String;
FLabels : String;
FTypes : String;
FRealTime : Boolean;
procedure generatedLabel;
procedure generatedBackgroundColor;
procedure generatedBorderColor;
procedure generatedBorderWidth;
procedure generatedData;
procedure generatedFill;
function replaceValue(Value : String) : String;
function RandomColor : string;
function ConvertInt(aValue : Integer) : string;
public
constructor Create(Parent : iModelHTMLChartsConfig);
destructor Destroy; override;
class function New(Parent : iModelHTMLChartsConfig) : iModelHTMLDataSet;
function DataSet (Value : TDataSet) : iModelHTMLDataSet;
function RealTimeDataSet (Value : TDataSet) : iModelHTMLDataSet;
function LabelName(Value : String) : iModelHTMLDataSet;
function ValueName(Value : String) : iModelHTMLDataSet;
function RGBName(Value : String) : iModelHTMLDataSet;
function textLabel(Value : String) : iModelHTMLDataSet;
function BackgroundColor (Value : String) : iModelHTMLDataSet;
function BackgroundOpacity(Value : Integer) : iModelHTMLDataSet;
function BorderColor (Value : String) : iModelHTMLDataSet;
function BorderOpacity(Value : Integer) : iModelHTMLDataSet;
function BorderWidth (Value : Integer) : iModelHTMLDataSet;
function BorderDash (Lenght : Integer; Space : Integer) : iModelHTMLDataSet;
function Data (Value : String) : iModelHTMLDataSet;
function Fill (Value : Boolean) : iModelHTMLDataSet;
function Hidden(Value : Boolean) : iModelHTMLDataSet;
function HideZeroValuesControl(Value : Boolean) : iModelHTMLDataSet;
function LineTension (Value : Integer) : iModelHTMLDataSet;
function ResultScript : String;
function ResultLabels : String;
function RealTimeInitialValue : String;
function Types (Value : String) : iModelHTMLDataSet;
function &End : iModelHTMLChartsConfig;
end;
implementation
uses
Injection;
{ TModelHTMLChartsDataSet<T> }
function TModelHTMLChartsDataSet.BackgroundColor(
Value: String): iModelHTMLDataSet;
begin
Result := Self;
if Value <> '' then
// FBackgroundColor := '"rgba(' + Value + ', ' + FBackgroundOpacity + ')"';
FBackgroundColor := Value;
end;
function TModelHTMLChartsDataSet.BackgroundOpacity(
Value: Integer): iModelHTMLDataSet;
begin
Result := Self;
FBackgroundOpacity := ConvertInt(Value);
end;
function TModelHTMLChartsDataSet.BorderColor(
Value: String): iModelHTMLDataSet;
begin
Result := Self;
FBorderColor := Value;
end;
function TModelHTMLChartsDataSet.BorderDash(Lenght,
Space: Integer): iModelHTMLDataSet;
begin
Result := Self;
FBorderDash := '[' + IntToStr(Lenght) + ',' + IntToStr(Space) + ']';
end;
function TModelHTMLChartsDataSet.BorderOpacity(
Value: Integer): iModelHTMLDataSet;
begin
Result := Self;
FBorderOpacity := ConvertInt(Value);
end;
function TModelHTMLChartsDataSet.BorderWidth(
Value: Integer): iModelHTMLDataSet;
begin
Result := Self;
FBorderWidth := Value;
end;
function TModelHTMLChartsDataSet.&End: iModelHTMLChartsConfig;
begin
Result := FParent;
end;
function TModelHTMLChartsDataSet.ConvertInt(aValue: Integer): string;
begin
Result := FormatFloat('0.00', aValue / 10.0);
Result := StringReplace(Result, ',', '.',[rfReplaceAll]);
end;
constructor TModelHTMLChartsDataSet.Create(Parent : iModelHTMLChartsConfig);
begin
{$IF RTLVERSION > 27 }
TInjection.Weak(@FParent, Parent);
{$ELSE}
FParent := Parent;
{$IFEND}
FLabelName := 'Label';
FValueName := 'Value';
FRGBName := 'RGB';
FBorderWidth := 1;
FBackgroundOpacity := '1';
FBorderOpacity := '1';
FHidden := false;
FHideZeroValues := false;
end;
function TModelHTMLChartsDataSet.Data(Value: String): iModelHTMLDataSet;
begin
Result := Self;
FData := Value;
end;
function TModelHTMLChartsDataSet.DataSet(
Value: TDataSet): iModelHTMLDataSet;
begin
Result := Self;
FDataSet := Value;
end;
destructor TModelHTMLChartsDataSet.Destroy;
begin
inherited;
end;
function TModelHTMLChartsDataSet.Fill(Value: Boolean): iModelHTMLDataSet;
begin
Result := Self;
FFill := Value;
end;
procedure TModelHTMLChartsDataSet.generatedBackgroundColor;
var
Local_I: Integer;
Aux : String;
HasRGBField : Boolean;
begin
if FBackgroundColor = '' then
begin
HasRGBField := FDataSet.Fields.FindField(FRGBName) <> nil;
FBackgroundColor := '["';
Aux := ', ' + FBackgroundOpacity + ')", "';
FDataSet.First;
for Local_I := 0 to Pred(FDataSet.RecordCount) do
begin
if Local_I = Pred(FDataSet.RecordCount) then Aux := ', ' + FBackgroundOpacity + ')';
if HasRGBField then
FBackgroundColor := FBackgroundColor + 'rgba(' + FDataSet.FieldByName(FRGBName).AsString + Aux
else
FBackgroundColor := FBackgroundColor + 'rgba(' + RandomColor + Aux;
FDataSet.Next;
end;
FBackgroundColor := FBackgroundColor + '"]';
end
else
FBackgroundColor := '"rgba(' + FBackgroundColor + ', ' + FBackgroundOpacity + ')"';
end;
procedure TModelHTMLChartsDataSet.generatedBorderColor;
begin
end;
procedure TModelHTMLChartsDataSet.generatedBorderWidth;
begin
end;
procedure TModelHTMLChartsDataSet.generatedData;
var
Aux: string;
I : Integer;
begin
FData := '[';
Aux := ', ';
if Not FRealTime then
begin
FDataSet.First;
for I := 0 to Pred(FDataSet.RecordCount) do
begin
if I = Pred(FDataSet.RecordCount) then
Aux := '';
FData := FData + replaceValue(FDataSet.FieldByName(FValueName).AsString) + Aux;
FDataSet.Next;
end;
end;
FData := FData + ']';
end;
procedure TModelHTMLChartsDataSet.generatedFill;
begin
end;
procedure TModelHTMLChartsDataSet.generatedLabel;
begin
end;
function TModelHTMLChartsDataSet.Hidden(Value: Boolean): iModelHTMLDataSet;
begin
Result := Self;
FHidden := Value;
end;
function TModelHTMLChartsDataSet.HideZeroValuesControl(
Value: Boolean): iModelHTMLDataSet;
begin
Result := Self;
FHideZeroValues := Value;
end;
function TModelHTMLChartsDataSet.LabelName(Value: String): iModelHTMLDataSet;
begin
Result := Self;
FLabelName := Value;
end;
function TModelHTMLChartsDataSet.LineTension(Value: Integer): iModelHTMLDataSet;
begin
Result := Self;
FLineTension := IntToStr(Value);
end;
class function TModelHTMLChartsDataSet.New(Parent : iModelHTMLChartsConfig): iModelHTMLDataSet;
begin
Result := Self.Create(Parent);
end;
function TModelHTMLChartsDataSet.RandomColor: string;
var
_r,_g,_b, cont: Integer;
rgb : string;
begin
cont := 0;
while cont < 3 do
begin
cont := cont + 1;
_r := Random(255);
_g := Random(255);
_b := Random(255);
rgb := format('%d,%d,%d', [_r,_g,_b]);
if Pos( rgb, FBackgroundColor) = 0 then
cont := 3;
end;
Result := rgb;
end;
function TModelHTMLChartsDataSet.RealTimeDataSet(
Value: TDataSet): iModelHTMLDataSet;
begin
Result := Self;
FDataSet := Value;
FRealTime := True;
end;
function TModelHTMLChartsDataSet.replaceValue(Value: String): String;
var
I,cont : Integer;
caracter : string;
begin
caracter := '';
cont := 0;
{$IF Defined(ANDROID) or Defined(IOS)}
for I := Length(Value) downto 0 do
{$ELSE}
for I := Length(Value) downto 1 do
{$ENDIF}
begin
if (CharInSet(value[i], ['0'..'9']) or (value[i]='-')) then
caracter := value[i] + caracter
else
begin
if cont=0 then
begin
if ((value[i]='.') or (value[i]=',')) then
begin
value[i]:='.';
caracter := value[i] + caracter;
inc(cont);
end;
end;
end;
end;
result:=caracter;
end;
function TModelHTMLChartsDataSet.ResultLabels: String;
var
Local_I: Integer;
Aux : String;
begin
FLabels := '[';
Aux := '", "';
if Not FRealTime then
begin
FLabels := FLabels + '"';
FDataSet.First;
for Local_I := 0 to Pred(FDataSet.RecordCount) do
begin
if Local_I = Pred(FDataSet.RecordCount) then
Aux := '"';
FLabels := FLabels + FDataSet.FieldByName(FLabelName).AsString + Aux;
FDataSet.Next;
end;
end;
FLabels := FLabels + ']';
Result := FLabels;
end;
function TModelHTMLChartsDataSet.RealTimeInitialValue: String;
begin
FDataSet.Last;
Result := '{label: ' + QuotedStr(FtextLabel);
Result := Result + ', value: ' + replaceValue(FDataSet.FieldByName(FValueName).AsString);
Result := Result + '}';
end;
function TModelHTMLChartsDataSet.ResultScript: String;
begin
generatedLabel;
generatedBackgroundColor;
generatedBorderColor;
generatedBorderWidth;
generatedData;
generatedFill;
FScript := FScript + '{' + #13;
FScript := Format('%s hidden:%s,', [FScript, LowerCase(BoolToStr(FHidden, True))]);
FScript := Format('%s hideZeroValues:%s,', [FScript, LowerCase(BoolToStr(FHideZeroValues, True))]);
if FTypes <> '' then FScript := FScript + 'type : '''+FTypes+''', ' + #13;
FScript := FScript + 'label: '''+FtextLabel+''', ' + #13;
if FBackgroundColor <> '' then
FScript := FScript + 'backgroundColor: '+FBackgroundColor+', ' + #13;
if FBorderColor <> '' then
FScript := FScript + 'borderColor: "rgba('+FBorderColor+',' + FBorderOpacity + ')", ' + #13;
FScript := FScript + 'borderWidth: '+ IntToStr(FBorderWidth) + ', ' + #13;
if FFill then FScript := FScript + 'fill: true,' else FScript := FScript + 'fill: false,';
if FLineTension <> '' then FScript := FScript + 'lineTension: ' + FLineTension + ',';
if FBorderDash <> '' then FScript := FScript + 'borderDash: ' + FBorderDash + ',';
FScript := FScript + 'data: ' + FData + #13;
FScript := FScript + '} ' + #13;
Result := FScript;
end;
function TModelHTMLChartsDataSet.RGBName(Value: String): iModelHTMLDataSet;
begin
Result := Self;
FRGBName := Value;
end;
function TModelHTMLChartsDataSet.textLabel(
Value: String): iModelHTMLDataSet;
begin
Result := Self;
FtextLabel := Value;
end;
function TModelHTMLChartsDataSet.Types(Value: String): iModelHTMLDataSet;
begin
Result := Self;
FTypes := Value;
end;
function TModelHTMLChartsDataSet.ValueName(Value: String): iModelHTMLDataSet;
begin
Result := Self;
FValueName := Value;
end;
end.