-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuModel.Charts.Radar.pas
162 lines (139 loc) · 4.03 KB
/
uModel.Charts.Radar.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
unit uModel.Charts.Radar;
interface
uses
System.Generics.Collections,
System.Variants,
uModel.Charts.Interfaces,
System.Classes;
type
TModelChartRadar = class(TInterfacedObject, iModelChart)
private
FChartID: string;
FChartDataSets: TInterfaceList;
FHeight: string;
FWidth: string;
FLabel: string;
FOnItemClick: string;
public
constructor Create;
destructor Destroy; override;
class function New: iModelChart;
function AddChartDataSet(ALabel: string): iModelChartDataSet;
function LabelName: string; overload;
function LabelName(AValue: string): iModelChart; overload;
function ClearDataSets: iModelChart;
function DataSets(Index: Integer): iModelChartDataSet;
function Height(AValue: string): iModelChart;
function Width(AValue: string): iModelChart;
function OnItemClick(ACallbackJS: string): iModelChart;
function Generate: string;
function Update: string;
end;
implementation
uses
System.SysUtils,
uModel.Charts.Data,
uModel.Charts.Utils,
uModel.Charts.DataSet;
{ TModelChartRadar }
function TModelChartRadar.ClearDataSets: iModelChart;
begin
FChartDataSets.Clear;
Result := Self;
end;
constructor TModelChartRadar.Create;
begin
inherited Create;
FChartDataSets := TInterfaceList.Create;
FChartID := 'chartjs-radar' + IntToStr(Random(MaxInt));
FHeight := '150px';
FWidth := '400px';
end;
function TModelChartRadar.DataSets(Index: Integer): iModelChartDataSet;
begin
result := FChartDataSets.Items[Index] as iModelChartDataSet;
end;
destructor TModelChartRadar.Destroy;
begin
FChartDataSets.Free;
inherited Destroy;
end;
function TModelChartRadar.AddChartDataSet(ALabel: string): iModelChartDataSet;
begin
Result := TModelChartDataSet.New(Self, ALabel);
FChartDataSets.Add(Result);
end;
function TModelChartRadar.Height(AValue: string): iModelChart;
begin
FHeight := AValue;
Result := Self;
end;
function TModelChartRadar.LabelName(AValue: string): iModelChart;
begin
result := self;
FLabel := AValue;
end;
function TModelChartRadar.LabelName: string;
begin
Result := FLabel;
end;
function TModelChartRadar.Width(AValue: string): iModelChart;
begin
FWidth := AValue;
Result := Self;
end;
class function TModelChartRadar.New: iModelChart;
begin
Result := Self.Create;
end;
function TModelChartRadar.OnItemClick(ACallbackJS: string): iModelChart;
begin
Result := Self;
FOnItemClick := ACallbackJS;
end;
function TModelChartRadar.Update: string;
begin
var LDataSetUpdateStr := '';
for var i := 0 to FChartDataSets.Count - 1 do
begin
var LDatasetsStr := (FChartDataSets[i] as iModelChartDataSet).ArrayValues;
LDataSetUpdateStr := LDataSetUpdateStr + Format('chart.data.datasets[%d].data = %s;', [i, LDatasetsStr]);
end;
Result :=
'var chart = Chart.getChart("'+ FChartID +'");' +
'if (chart) {' +
' ' + LDataSetUpdateStr + ' ' +
' chart.update();' +
'}';
end;
function TModelChartRadar.Generate: string;
var
LLabelsStr, LDatasetsStr: string;
LChartDataSet: iModelChartDataSet;
begin
LLabelsStr := EmptyStr;
LDatasetsStr := EmptyStr;
LLabelsStr := (FChartDataSets[0] as iModelChartDataSet).GenerateLabels;
for var i := 0 to Pred(FChartDataSets.Count) do
begin
LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
if i > 0 then
LDatasetsStr := LDatasetsStr + ', ';
LDatasetsStr := LDatasetsStr + LChartDataSet.Generate;
end;
Result := Format(
'<canvas id="chartjs-radar'+ FChartID +'" width="%s" height="%s"></canvas>' +
'<script>' +
'document.addEventListener("DOMContentLoaded", () => {' +
' new Chart(document.getElementById("chartjs-radar' + FChartID + '"), {' +
' type: "radar",' +
' data: {' +
' labels: [%s],' +
' datasets: [%s]' +
' }' +
' });' +
'});' +
'</script>',
[FWidth, FHeight, LLabelsStr, LDatasetsStr]);
end;
end.