├── README.md
├── uModel.Charts.Bar.pas
├── uModel.Charts.Data.pas
├── uModel.Charts.DataSet.pas
├── uModel.Charts.Doughnut.pas
├── uModel.Charts.Factory.pas
├── uModel.Charts.Interfaces.pas
├── uModel.Charts.Line.pas
├── uModel.Charts.Pie.pas
├── uModel.Charts.PolarArea.pas
├── uModel.Charts.Radar.pas
└── uModel.Charts.Utils.pas
/README.md:
--------------------------------------------------------------------------------
1 | # d2webgraph
2 | Este repositório contém um conjunto de classes Delphi desenvolvido para gerar gráficos no formato HTML. Inicialmente criado para um projeto em [D2Bridge](https://www.d2bridge.com.br/), um framework que permite compilar projetos VCL ou FireMonkey para a Web com o mesmo código.
3 |
4 |
5 |
6 |
7 |
8 |
9 | ## Instalação
10 | Instalação usando o boss
11 | ```
12 | boss install https://github.com/JoRodriguesDev/d2webgraph
13 | ```
14 |
15 | ## Declaração
16 | Para utilizar o d2webgraph você deve adicionar as uses:
17 | ```pascal
18 | uModel.Charts.Interfaces,
19 | uModel.Charts.Factory;
20 | ```
21 |
22 | Declarar no head do html
23 | ```html
24 |
25 | ```
26 |
27 | ## Tipos
28 | ```
29 | Bootstrap 5 Doughnut Chart
30 | Bootstrap 5 Bar Chart
31 | Bootstrap 5 Line Chart
32 | Bootstrap 5 Pie Chart
33 | Bootstrap 5 Polar Area Chart
34 | Bootstrap 5 Radar Chart
35 | ```
36 |
37 | ## Como usar
38 | ```pascal
39 | var HTML := TModelChartFactory.New
40 | .Line
41 | .Height('150px')
42 | .Width('300px')
43 | .AddChartDataSet('2023')
44 | .Opacity(0.8)
45 | .AddChartData('Janeiro', 50, TChartColor.primary, TChartColor.primary)
46 | .AddChartData('Fevereiro', 45, TChartColor.primary, TChartColor.primary)
47 | .AddChartData('Março', 70, TChartColor.primary, TChartColor.primary)
48 | .AddChartData('Abril', 75, TChartColor.primary, TChartColor.primary)
49 | .AddChartData('Maio', 90, TChartColor.primary, TChartColor.primary)
50 | .AddChartData('Junho', 30, TChartColor.primary, TChartColor.primary)
51 | .&end
52 | .AddChartDataSet('2024')
53 | .Opacity(0.5)
54 | .AddChartData('Janeiro', 55, TChartColor.success, TChartColor.success)
55 | .AddChartData('Fevereiro', 50, TChartColor.success, TChartColor.success)
56 | .AddChartData('Março', 82, TChartColor.success, TChartColor.success)
57 | .AddChartData('Abril', 79, TChartColor.success, TChartColor.success)
58 | .AddChartData('Maio', 80, TChartColor.success, TChartColor.success)
59 | .AddChartData('Junho', 42, TChartColor.success, TChartColor.success)
60 | .&end
61 | .Generate;
62 | ```
63 |
--------------------------------------------------------------------------------
/uModel.Charts.Bar.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.Bar;
2 |
3 | interface
4 |
5 | uses
6 | System.Generics.Collections,
7 | System.Variants,
8 | uModel.Charts.Interfaces,
9 | System.Classes;
10 |
11 | type
12 | TModelChartBar = class(TInterfacedObject, iModelChart)
13 | private
14 | FChartID: string;
15 | FChartDataSets: TInterfaceList;
16 | FHeight: string;
17 | FWidth: string;
18 | FLabel: string;
19 | FOnItemClick: string;
20 | public
21 | constructor Create;
22 | destructor Destroy; override;
23 | class function New: iModelChart;
24 | function AddChartDataSet(ALabel: string): iModelChartDataSet;
25 | function DataSets(Index: Integer): iModelChartDataSet;
26 | function LabelName: string; overload;
27 | function LabelName(AValue: string): iModelChart; overload;
28 | function ClearDataSets: iModelChart;
29 | function OnItemClick(ACallbackJS: string): iModelChart;
30 | function Height(AValue: string): iModelChart;
31 | function Width(AValue: string): iModelChart;
32 | function Generate: string;
33 | function Update: string;
34 | end;
35 |
36 | implementation
37 |
38 | uses
39 | System.SysUtils,
40 | uModel.Charts.Data,
41 | uModel.Charts.Utils,
42 | uModel.Charts.DataSet;
43 |
44 | { TModelChartBar }
45 |
46 | function TModelChartBar.ClearDataSets: iModelChart;
47 | begin
48 | FChartDataSets.Clear;
49 | Result := Self;
50 | end;
51 |
52 | constructor TModelChartBar.Create;
53 | begin
54 | inherited Create;
55 | FChartDataSets := TInterfaceList.Create;
56 | FChartID := 'chartjs-bar' + IntToStr(Random(MaxInt));
57 | FHeight := '150px';
58 | FWidth := '400px';
59 | end;
60 |
61 | function TModelChartBar.DataSets(Index: Integer): iModelChartDataSet;
62 | begin
63 | result := FChartDataSets.Items[Index] as iModelChartDataSet;
64 | end;
65 |
66 | destructor TModelChartBar.Destroy;
67 | begin
68 | FChartDataSets.Free;
69 | inherited Destroy;
70 | end;
71 |
72 | function TModelChartBar.AddChartDataSet(ALabel: string): iModelChartDataSet;
73 | begin
74 | Result := TModelChartDataSet.New(Self, ALabel);
75 | FChartDataSets.Add(Result);
76 | end;
77 |
78 | function TModelChartBar.Height(AValue: string): iModelChart;
79 | begin
80 | FHeight := AValue;
81 | Result := Self;
82 | end;
83 |
84 | function TModelChartBar.LabelName(AValue: string): iModelChart;
85 | begin
86 | result := self;
87 | FLabel := AValue;
88 | end;
89 |
90 | function TModelChartBar.LabelName: string;
91 | begin
92 | Result := FLabel;
93 | end;
94 |
95 | function TModelChartBar.Width(AValue: string): iModelChart;
96 | begin
97 | FWidth := AValue;
98 | Result := Self;
99 | end;
100 |
101 | class function TModelChartBar.New: iModelChart;
102 | begin
103 | Result := Self.Create;
104 | end;
105 |
106 | function TModelChartBar.OnItemClick(ACallbackJS: string): iModelChart;
107 | begin
108 | Result := Self;
109 | FOnItemClick := ACallbackJS;
110 | end;
111 |
112 | function TModelChartBar.Update: string;
113 | begin
114 | var LDataSetUpdateStr := '';
115 | var LLabelsUpdateStr := '';
116 |
117 | for var i := 0 to FChartDataSets.Count - 1 do
118 | begin
119 | var LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
120 | var LDatasetsStr := LChartDataSet.ArrayValues;
121 | var LDatasetLabels := LChartDataSet.GenerateLabels;
122 |
123 | LDataSetUpdateStr := LDataSetUpdateStr + Format('chart.data.datasets[%d].data = %s;', [i, LDatasetsStr]);
124 | LLabelsUpdateStr := LLabelsUpdateStr + Format('chart.data.datasets[%d].labels = [%s];', [i, LDatasetLabels]);
125 | end;
126 |
127 | Result :=
128 | 'var chart = Chart.getChart("'+ FChartID +'");' +
129 | 'if (chart) {' +
130 | ' ' + LLabelsUpdateStr +
131 | ' ' + LDataSetUpdateStr +
132 | ' chart.update();' +
133 | '}';
134 | end;
135 |
136 | function TModelChartBar.Generate: string;
137 | var
138 | LLabelsStr, LDatasetsStr: string;
139 | LChartDataSet: iModelChartDataSet;
140 | begin
141 | LLabelsStr := EmptyStr;
142 | LDatasetsStr := EmptyStr;
143 | LLabelsStr := (FChartDataSets[0] as iModelChartDataSet).GenerateLabels;
144 |
145 | for var i := 0 to Pred(FChartDataSets.Count) do
146 | begin
147 | LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
148 | if i > 0 then
149 | LDatasetsStr := LDatasetsStr + ', ';
150 | LDatasetsStr := LDatasetsStr + LChartDataSet.Generate;
151 | end;
152 |
153 | Result := Format(
154 | '' +
155 | '', [FWidth, FHeight, LLabelsStr, LDatasetsStr]);
191 | end;
192 |
193 | end.
194 |
--------------------------------------------------------------------------------
/uModel.Charts.Data.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.Data;
2 |
3 | interface
4 |
5 | uses
6 | uModel.Charts.Interfaces;
7 |
8 | type
9 | TModelChartData = class(TInterfacedObject, iModelChartData)
10 | private
11 | [weak]
12 | FParent: iModelChartDataSet;
13 | FLabelName: string;
14 | FValue: Variant;
15 | FBackgroundColor: TChartColor;
16 | FBorderColor: TChartColor;
17 | FPointBackgroundColor: TChartColor;
18 | FPointBorderColor: TChartColor;
19 | FPointHoverBackgroundColor: TChartColor;
20 | FPointHoverBorderColor: TChartColor;
21 | public
22 | constructor Create(AParent: iModelChartDataSet; const ALabel: string; AValue: Variant; ABackgroundColor, ABorderColor, APointBackgroundColor, APointBorderColor, APointHoverBackgroundColor, APointHoverBorderColor: TChartColor);
23 | class function New(AParent: iModelChartDataSet; const ALabel: string; AValue: Variant; ABackgroundColor, ABorderColor, APointBackgroundColor, APointBorderColor, APointHoverBackgroundColor, APointHoverBorderColor: TChartColor): iModelChartData;
24 | destructor Destroy; override;
25 | function LabelName(AValue: string): iModelChartData; overload;
26 | function Value(AValue: Variant): iModelChartData; overload;
27 | function BackgroundColor(AValue: TChartColor): iModelChartData; overload;
28 | function BorderColor(AValue: TChartColor): iModelChartData; overload;
29 | function PointBackgroundColor(AValue: TChartColor): iModelChartData; overload;
30 | function PointBorderColor(AValue: TChartColor): iModelChartData; overload;
31 | function PointHoverBackgroundColor(AValue: TChartColor): iModelChartData; overload;
32 | function PointHoverBorderColor(AValue: TChartColor): iModelChartData; overload;
33 | function LabelName: string; overload;
34 | function Value: Variant; overload;
35 | function BackgroundColor: TChartColor; overload;
36 | function BorderColor: TChartColor; overload;
37 | function PointBackgroundColor: TChartColor; overload;
38 | function PointBorderColor: TChartColor; overload;
39 | function PointHoverBackgroundColor: TChartColor; overload;
40 | function PointHoverBorderColor: TChartColor; overload;
41 | function &End: iModelChartDataSet;
42 | end;
43 |
44 | implementation
45 |
46 | { TChartData }
47 |
48 | function TModelChartData.BorderColor: TChartColor;
49 | begin
50 | result := FBorderColor;
51 | end;
52 |
53 | function TModelChartData.BorderColor(AValue: TChartColor): iModelChartData;
54 | begin
55 | result := Self;
56 | FBorderColor := AValue;
57 | end;
58 |
59 | function TModelChartData.BackgroundColor(AValue: TChartColor): iModelChartData;
60 | begin
61 | result := Self;
62 | FBackgroundColor := AValue;
63 | end;
64 |
65 | function TModelChartData.&End: iModelChartDataSet;
66 | begin
67 | Result := FParent;
68 | end;
69 |
70 | function TModelChartData.BackgroundColor: TChartColor;
71 | begin
72 | Result := FBackgroundColor;
73 | end;
74 |
75 | constructor TModelChartData.Create(AParent: iModelChartDataSet; const ALabel: string; AValue: Variant; ABackgroundColor, ABorderColor, APointBackgroundColor, APointBorderColor, APointHoverBackgroundColor, APointHoverBorderColor: TChartColor);
76 | begin
77 | FParent := AParent;
78 | FLabelName := ALabel;
79 | FValue := AValue;
80 | FBackgroundColor := ABackgroundColor;
81 | FBorderColor := ABorderColor;
82 | FPointBackgroundColor := APointBackgroundColor;
83 | FPointBorderColor := APointBorderColor;
84 | FPointHoverBackgroundColor := APointHoverBackgroundColor;
85 | FPointHoverBorderColor := APointHoverBorderColor;
86 | end;
87 |
88 | destructor TModelChartData.Destroy;
89 | begin
90 |
91 | inherited;
92 | end;
93 |
94 | function TModelChartData.LabelName: string;
95 | begin
96 | Result := FLabelName;
97 | end;
98 |
99 | function TModelChartData.LabelName(AValue: string): iModelChartData;
100 | begin
101 | result := Self;
102 | FLabelName := AValue;
103 | end;
104 |
105 | class function TModelChartData.New(AParent: iModelChartDataSet; const ALabel: string; AValue: Variant; ABackgroundColor, ABorderColor, APointBackgroundColor, APointBorderColor, APointHoverBackgroundColor, APointHoverBorderColor: TChartColor): iModelChartData;
106 | begin
107 | result := self.Create(AParent, ALabel, AValue, ABackgroundColor, ABorderColor, APointBackgroundColor, APointBorderColor, APointHoverBackgroundColor, APointHoverBorderColor);
108 | end;
109 |
110 | function TModelChartData.PointBackgroundColor(AValue: TChartColor): iModelChartData;
111 | begin
112 | result := Self;
113 | FPointBackgroundColor := AValue;
114 | end;
115 |
116 | function TModelChartData.PointBackgroundColor: TChartColor;
117 | begin
118 | Result := FPointBackgroundColor;
119 | end;
120 |
121 | function TModelChartData.PointBorderColor: TChartColor;
122 | begin
123 | Result := FPointBorderColor;
124 | end;
125 |
126 | function TModelChartData.PointBorderColor(AValue: TChartColor): iModelChartData;
127 | begin
128 | result := Self;
129 | FPointBorderColor := AValue;
130 | end;
131 |
132 | function TModelChartData.PointHoverBackgroundColor(AValue: TChartColor): iModelChartData;
133 | begin
134 | result := Self;
135 | FPointHoverBackgroundColor := AValue;
136 | end;
137 |
138 | function TModelChartData.PointHoverBackgroundColor: TChartColor;
139 | begin
140 | Result := FPointHoverBackgroundColor;
141 | end;
142 |
143 | function TModelChartData.PointHoverBorderColor(AValue: TChartColor): iModelChartData;
144 | begin
145 | result := Self;
146 | FPointHoverBorderColor := AValue;
147 | end;
148 |
149 | function TModelChartData.PointHoverBorderColor: TChartColor;
150 | begin
151 | Result := FPointHoverBorderColor;
152 | end;
153 |
154 | function TModelChartData.Value(AValue: Variant): iModelChartData;
155 | begin
156 | result := Self;
157 | FValue := AVAlue;
158 | end;
159 |
160 | function TModelChartData.Value: Variant;
161 | begin
162 | result := FValue;
163 | end;
164 |
165 | end.
166 |
--------------------------------------------------------------------------------
/uModel.Charts.DataSet.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.DataSet;
2 |
3 | interface
4 |
5 | uses
6 | System.Generics.Collections,
7 | System.Variants,
8 | uModel.Charts.Interfaces,
9 | System.Classes;
10 |
11 | type
12 | TModelChartDataSet = class(TInterfacedObject, iModelChartDataSet)
13 | private
14 | [weak]
15 | FParent: iModelChart;
16 | FChartDataList: TInterfaceList;
17 | FOpacity: Double;
18 | FLabel: string;
19 | function GenerateBackgroundColors: string;
20 | function GenerateBorderColors: string;
21 | function GenerateValue: string;
22 | function GeneratePointBackgroundColor: string;
23 | public
24 | constructor Create(AParent: iModelChart; ALabel: string);
25 | class function New(AParent: iModelChart; ALabel: string): iModelChartDataSet;
26 | destructor Destroy; override;
27 | function GenerateLabels: string;
28 | function AddChartData(ALabel: string; AValue: Variant; ABackgroundColor, ABorderColor: TChartColor; APointBackgroundColor: TChartColor = None; APointBorderColor: TChartColor = None; APointHoverBackgroundColor: TChartColor = None; APointHoverBorderColor: TChartColor = None): iModelChartDataSet;
29 | function Data(Index: Integer): iModelChartData;
30 | function LabelName: string; overload;
31 | function LabelName(AValue: string): iModelChartDataSet; overload;
32 | function Opacity(AValue: Double): iModelChartDataSet; overload;
33 | function Generate: string;
34 | function ArrayValues: string;
35 | function Opacity: Double; overload;
36 | function RecordCount: integer;
37 | function ClearData: iModelChartDataSet;
38 | function &End: iModelChart;
39 | end;
40 |
41 | implementation
42 |
43 | uses
44 | System.SysUtils,
45 | uModel.Charts.Data,
46 | uModel.Charts.Utils;
47 |
48 | { TModelChartDataSet }
49 |
50 | function TModelChartDataSet.ArrayValues: string;
51 | begin
52 | Result := '[' + Self.GenerateValue + ']';
53 | end;
54 |
55 | function TModelChartDataSet.ClearData: iModelChartDataSet;
56 | begin
57 | result := self;
58 | FChartDataList.Clear;
59 | end;
60 |
61 | constructor TModelChartDataSet.Create(AParent: iModelChart; ALabel: string);
62 | begin
63 | FParent := AParent;
64 | FChartDataList := TInterfaceList.Create;
65 | FLabel := ALabel;
66 | FOpacity := 1;
67 | end;
68 |
69 | function TModelChartDataSet.Data(Index: Integer): iModelChartData;
70 | begin
71 | result := FChartDataList.Items[Index] as iModelChartData;
72 | end;
73 |
74 | destructor TModelChartDataSet.Destroy;
75 | begin
76 | FChartDataList.Free;
77 | inherited Destroy;
78 | end;
79 |
80 | function TModelChartDataSet.&End: iModelChart;
81 | begin
82 | result := FParent;
83 | end;
84 |
85 | function TModelChartDataSet.AddChartData(ALabel: string; AValue: Variant; ABackgroundColor, ABorderColor: TChartColor; APointBackgroundColor: TChartColor = None; APointBorderColor: TChartColor = None; APointHoverBackgroundColor: TChartColor = None; APointHoverBorderColor: TChartColor = None): iModelChartDataSet;
86 | begin
87 | Result := Self;
88 | var ChartData := TModelChartData.New(Self, ALabel, AValue, ABackgroundColor, ABorderColor, APointBackgroundColor, APointBorderColor, APointHoverBackgroundColor, APointHoverBorderColor);
89 | FChartDataList.Add(ChartData);
90 | end;
91 |
92 | function TModelChartDataSet.GenerateValue: string;
93 | begin
94 | var DataStr := EmptyStr;
95 | for var I := 0 to FChartDataList.Count - 1 do
96 | begin
97 | var ChartData := FChartDataList[I] as iModelChartData;
98 | DataStr := DataStr + VarToStr(ChartData.Value);
99 | if I < FChartDataList.Count - 1 then
100 | DataStr := DataStr + ', ';
101 | end;
102 | Result := DataStr;
103 | end;
104 |
105 | function TModelChartDataSet.GenerateLabels: string;
106 | begin
107 | var DataStr := EmptyStr;
108 | for var I := 0 to FChartDataList.Count - 1 do
109 | begin
110 | var ChartData := FChartDataList[I] as iModelChartData;
111 | DataStr := DataStr + '"' + VarToStr(ChartData.LabelName) + '"';
112 | if I < FChartDataList.Count - 1 then
113 | DataStr := DataStr + ', ';
114 | end;
115 | Result := DataStr;
116 | end;
117 |
118 | function TModelChartDataSet.LabelName(AValue: string): iModelChartDataSet;
119 | begin
120 | result := Self;
121 | FLabel := AValue;
122 | end;
123 |
124 | function TModelChartDataSet.LabelName: string;
125 | begin
126 | Result := FLabel;
127 | end;
128 |
129 | class function TModelChartDataSet.New(AParent: iModelChart; ALabel: string): iModelChartDataSet;
130 | begin
131 | Result := self.Create(AParent, Alabel);
132 | end;
133 |
134 | function TModelChartDataSet.Opacity: Double;
135 | begin
136 | result := FOpacity;
137 | end;
138 |
139 | function TModelChartDataSet.Opacity(AValue: Double): iModelChartDataSet;
140 | begin
141 | result := self;
142 | FOpacity := AValue;
143 | end;
144 |
145 | function TModelChartDataSet.RecordCount: integer;
146 | begin
147 | Result := FChartDataList.Count;
148 | end;
149 |
150 | function TModelChartDataSet.GenerateBackgroundColors: string;
151 | begin
152 | var ColorsStr := EmptyStr;
153 | for var I := 0 to FChartDataList.Count - 1 do
154 | begin
155 | var ChartData := FChartDataList[I] as iModelChartData;
156 | ColorsStr := ColorsStr + '"' + ColorEnumToString(ChartData.BackgroundColor, FOpacity) + '"';
157 | if I < FChartDataList.Count - 1 then
158 | ColorsStr := ColorsStr + ', ';
159 | end;
160 | Result := ColorsStr;
161 | end;
162 |
163 | function TModelChartDataSet.GeneratePointBackgroundColor: string;
164 | begin
165 | var BackgroundColorStr := EmptyStr;
166 | if FChartDataList.Count > 0 then
167 | begin
168 | var ChartData := FChartDataList[0] as iModelChartData;
169 | BackgroundColorStr := '"' + ColorEnumToString(ChartData.PointBackgroundColor, FOpacity) + '"';
170 | end;
171 | Result := BackgroundColorStr;
172 | end;
173 |
174 | function TModelChartDataSet.GenerateBorderColors: string;
175 | begin
176 | var ColorsStr := EmptyStr;
177 | for var I := 0 to FChartDataList.Count - 1 do
178 | begin
179 | var ChartData := FChartDataList[I] as iModelChartData;
180 | ColorsStr := ColorsStr + '"' + ColorEnumToString(ChartData.BorderColor, FOpacity) + '"';
181 | if I < FChartDataList.Count - 1 then
182 | ColorsStr := ColorsStr + ', ';
183 | end;
184 | Result := ColorsStr;
185 | end;
186 |
187 | function TModelChartDataSet.Generate: string;
188 | var
189 | LPointBackgroundColorStr, LPointBorderColorStr, LPointHoverBackgroundColorStr, LPointHoverBorderColorStr: string;
190 | begin
191 | var LPointBackgroundColor := (FChartDataList[0] as iModelChartData).PointBackgroundColor;
192 | if LPointBackgroundColor <> Default(TChartColor) then
193 | LPointBackgroundColorStr := Format('pointBackgroundColor: "%s",', [ColorEnumToString(LPointBackgroundColor, FOpacity)])
194 | else LPointBackgroundColorStr := '';
195 |
196 | var LPointBorderColor := (FChartDataList[0] as iModelChartData).PointBackgroundColor;
197 | if LPointBorderColor <> Default(TChartColor) then
198 | LPointBorderColorStr := Format('pointBorderColor: "%s",', [ColorEnumToString(LPointBorderColor, FOpacity)])
199 | else LPointBorderColorStr := '';
200 |
201 | var LPointHoverBackgroundColor := (FChartDataList[0] as iModelChartData).PointBackgroundColor;
202 | if LPointHoverBackgroundColor <> Default(TChartColor) then
203 | LPointHoverBackgroundColorStr := Format('pointHoverBackgroundColor: "%s",', [ColorEnumToString(LPointHoverBackgroundColor, FOpacity)])
204 | else LPointHoverBackgroundColorStr := '';
205 |
206 | var LPointHoverBorderColor := (FChartDataList[0] as iModelChartData).PointBackgroundColor;
207 | if LPointHoverBorderColor <> Default(TChartColor) then
208 | LPointHoverBorderColorStr := Format('pointHoverBorderColor: "%s",', [ColorEnumToString(LPointHoverBorderColor, FOpacity)])
209 | else LPointHoverBorderColorStr := '';
210 |
211 | Result := Format(
212 | '{' +
213 | ' label: "%s",' +
214 | ' fill: true,' +
215 | ' backgroundColor: [%s],' +
216 | ' borderColor: [%s],' +
217 | ' %s' +
218 | ' %s' +
219 | ' %s' +
220 | ' %s' +
221 | ' data: [%s]' +
222 | '}', [self.LabelName, self.GenerateBackgroundColors, self.GenerateBorderColors,
223 | LPointBackgroundColorStr, LPointBorderColorStr, LPointHoverBackgroundColorStr,
224 | LPointHoverBorderColorStr, Self.GenerateValue]);
225 | end;
226 |
227 | end.
228 |
229 |
--------------------------------------------------------------------------------
/uModel.Charts.Doughnut.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.Doughnut;
2 |
3 | interface
4 |
5 | uses
6 | System.Generics.Collections,
7 | System.Variants,
8 | uModel.Charts.Interfaces,
9 | System.Classes;
10 |
11 | type
12 | TModelChartDoughnut = class(TInterfacedObject, iModelChart)
13 | private
14 | FChartID: string;
15 | FChartDataSets: TInterfaceList;
16 | FHeight: string;
17 | FWidth: string;
18 | FLabel: string;
19 | FOnItemClick: string;
20 | public
21 | constructor Create;
22 | destructor Destroy; override;
23 | class function New: iModelChart;
24 | function AddChartDataSet(ALabel: string): iModelChartDataSet;
25 | function LabelName: string; overload;
26 | function LabelName(AValue: string): iModelChart; overload;
27 | function ClearDataSets: iModelChart;
28 | function Height(AValue: string): iModelChart;
29 | function Width(AValue: string): iModelChart;
30 | function DataSets(Index: Integer): iModelChartDataSet;
31 | function OnItemClick(ACallbackJS: string): iModelChart;
32 | function Generate: string;
33 | function Update: string;
34 | end;
35 |
36 | implementation
37 |
38 | uses
39 | System.SysUtils,
40 | uModel.Charts.Data,
41 | uModel.Charts.Utils,
42 | uModel.Charts.DataSet;
43 |
44 | { TModelChartDoughnut }
45 |
46 | function TModelChartDoughnut.ClearDataSets: iModelChart;
47 | begin
48 | FChartDataSets.Clear;
49 | Result := Self;
50 | end;
51 |
52 | constructor TModelChartDoughnut.Create;
53 | begin
54 | inherited Create;
55 | FChartDataSets := TInterfaceList.Create;
56 | FChartID := 'chartjs-doughnut' + IntToStr(Random(MaxInt));
57 | FHeight := '150px';
58 | FWidth := '400px';
59 | end;
60 |
61 | function TModelChartDoughnut.DataSets(Index: Integer): iModelChartDataSet;
62 | begin
63 | result := FChartDataSets.Items[Index] as iModelChartDataSet;
64 | end;
65 |
66 | destructor TModelChartDoughnut.Destroy;
67 | begin
68 | FChartDataSets.Free;
69 | inherited Destroy;
70 | end;
71 |
72 | function TModelChartDoughnut.AddChartDataSet(ALabel: string): iModelChartDataSet;
73 | begin
74 | Result := TModelChartDataSet.New(Self, ALabel);
75 | FChartDataSets.Add(Result);
76 | end;
77 |
78 | function TModelChartDoughnut.Height(AValue: string): iModelChart;
79 | begin
80 | FHeight := AValue;
81 | Result := Self;
82 | end;
83 |
84 | function TModelChartDoughnut.LabelName(AValue: string): iModelChart;
85 | begin
86 | result := self;
87 | FLabel := AValue;
88 | end;
89 |
90 | function TModelChartDoughnut.LabelName: string;
91 | begin
92 | Result := FLabel;
93 | end;
94 |
95 | function TModelChartDoughnut.Width(AValue: string): iModelChart;
96 | begin
97 | FWidth := AValue;
98 | Result := Self;
99 | end;
100 |
101 | class function TModelChartDoughnut.New: iModelChart;
102 | begin
103 | Result := Self.Create;
104 | end;
105 |
106 | function TModelChartDoughnut.OnItemClick(ACallbackJS: string): iModelChart;
107 | begin
108 | Result := Self;
109 | FOnItemClick := ACallbackJS;
110 | end;
111 |
112 | function TModelChartDoughnut.Update: string;
113 | begin
114 | var LDataSetUpdateStr := '';
115 | var LLabelsUpdateStr := '';
116 |
117 | for var i := 0 to FChartDataSets.Count - 1 do
118 | begin
119 | var LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
120 | var LDatasetsStr := LChartDataSet.ArrayValues;
121 | var LDatasetLabels := LChartDataSet.GenerateLabels;
122 |
123 | LDataSetUpdateStr := LDataSetUpdateStr + Format('chart.data.datasets[%d].data = %s;', [i, LDatasetsStr]);
124 | LLabelsUpdateStr := LLabelsUpdateStr + Format('chart.data.datasets[%d].labels = [%s];', [i, LDatasetLabels]);
125 | end;
126 |
127 | Result :=
128 | 'var chart = Chart.getChart("'+ FChartID +'");' +
129 | 'if (chart) {' +
130 | ' ' + LLabelsUpdateStr +
131 | ' ' + LDataSetUpdateStr +
132 | ' chart.update();' +
133 | '}';
134 | end;
135 |
136 | function TModelChartDoughnut.Generate: string;
137 | var
138 | LLabelsStr, LDatasetsStr: string;
139 | LChartDataSet: iModelChartDataSet;
140 | begin
141 | LLabelsStr := EmptyStr;
142 | LDatasetsStr := EmptyStr;
143 | LLabelsStr := (FChartDataSets[0] as iModelChartDataSet).GenerateLabels;
144 |
145 | for var i := 0 to Pred(FChartDataSets.Count) do
146 | begin
147 | LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
148 | if i > 0 then
149 | LDatasetsStr := LDatasetsStr + ', ';
150 | LDatasetsStr := LDatasetsStr + LChartDataSet.Generate;
151 | end;
152 |
153 | Result := Format(
154 | '' +
155 | '', [FWidth, FHeight, LLabelsStr, LDatasetsStr]);
169 | end;
170 |
171 | end.
172 |
--------------------------------------------------------------------------------
/uModel.Charts.Factory.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.Factory;
2 |
3 | interface
4 |
5 | uses
6 | System.Generics.Collections,
7 | uModel.Charts.Interfaces;
8 |
9 | type
10 | TModelChartFactory = class(TInterfacedObject, iModelChartFactory)
11 | private
12 | public
13 | constructor Create;
14 | destructor Destroy; override;
15 | class function New: iModelChartFactory ;
16 | function Bar: iModelChart;
17 | function Line: iModelChart;
18 | function Pie: iModelChart;
19 | function Doughnut: iModelChart;
20 | function PolarArea: iModelChart;
21 | function Radar: iModelChart;
22 | end;
23 |
24 | implementation
25 |
26 | uses
27 | uModel.Charts.Bar,
28 | uModel.Charts.Line,
29 | uModel.Charts.Pie,
30 | uModel.Charts.Doughnut,
31 | uModel.Charts.PolarArea,
32 | uModel.Charts.Radar;
33 |
34 | { TModelChartFactory }
35 |
36 | const
37 | cHeight = '160px';
38 | cWidth = '500px';
39 |
40 | function TModelChartFactory.Bar: iModelChart;
41 | begin
42 | result := TModelChartBar.New.Height(cHeight).Width(cWidth);
43 | end;
44 |
45 | constructor TModelChartFactory.Create;
46 | begin
47 |
48 | end;
49 |
50 | destructor TModelChartFactory.Destroy;
51 | begin
52 | inherited;
53 | end;
54 |
55 | function TModelChartFactory.Doughnut: iModelChart;
56 | begin
57 | result := TModelChartDoughnut.New.Height(cHeight).Width(cWidth);
58 | end;
59 |
60 | function TModelChartFactory.Line: iModelChart;
61 | begin
62 | result := TModelChartLine.New.Height(cHeight).Width(cWidth);
63 | end;
64 |
65 | class function TModelChartFactory.New: iModelChartFactory ;
66 | begin
67 | Result := Self.Create;
68 | end;
69 |
70 | function TModelChartFactory.Pie: iModelChart;
71 | begin
72 | result := TModelChartPie.New.Height(cHeight).Width(cWidth);
73 | end;
74 |
75 | function TModelChartFactory.PolarArea: iModelChart;
76 | begin
77 | result := TModelChartPolarArea.New.Height(cHeight).Width(cWidth);
78 | end;
79 |
80 | function TModelChartFactory.Radar: iModelChart;
81 | begin
82 | result := TModelChartRadar.New.Height(cHeight).Width(cWidth);
83 | end;
84 |
85 | end.
86 |
87 |
--------------------------------------------------------------------------------
/uModel.Charts.Interfaces.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.Interfaces;
2 |
3 | interface
4 |
5 | uses
6 | System.Classes;
7 |
8 | type
9 | EnumColor = (
10 | none, blue, indigo,
11 | purple, pink ,red,
12 | orange, yellow, green,
13 | teal, cyan, black,
14 | white, gray, graydark,
15 | gray100, gray200, gray300,
16 | gray400, gray500, gray600,
17 | gray700, gray800, gray900,
18 | primary, secondary, success,
19 | info, warning, danger,
20 | light, dark, transparent);
21 |
22 | TChartColor = EnumColor;
23 |
24 | type
25 | iModelChartData = interface;
26 | iModelChartDataSet = interface;
27 | iModelChart = interface;
28 |
29 | iModelChartData = interface
30 | ['{8E128234-A31F-422D-9001-00D12883C4E5}']
31 | function LabelName(AValue: string): iModelChartData; overload;
32 | function Value(AValue: Variant): iModelChartData; overload;
33 | function BackgroundColor(AValue: TChartColor): iModelChartData; overload;
34 | function BorderColor(AValue: TChartColor): iModelChartData; overload;
35 | function PointBackgroundColor(AValue: TChartColor): iModelChartData; overload;
36 | function PointBorderColor(AValue: TChartColor): iModelChartData; overload;
37 | function PointHoverBackgroundColor(AValue: TChartColor): iModelChartData; overload;
38 | function PointHoverBorderColor(AValue: TChartColor): iModelChartData; overload;
39 |
40 | function LabelName: string; overload;
41 | function Value: Variant; overload;
42 | function BackgroundColor: TChartColor; overload;
43 | function BorderColor: TChartColor; overload;
44 | function PointBackgroundColor: TChartColor; overload;
45 | function PointBorderColor: TChartColor; overload;
46 | function PointHoverBackgroundColor: TChartColor; overload;
47 | function PointHoverBorderColor: TChartColor; overload;
48 | function &End: iModelChartDataSet;
49 | end;
50 |
51 | iModelChartDataSet = interface
52 | ['{A1234567-89AB-CDEF-0123-456789ABCDEF}']
53 | function GenerateLabels: string;
54 | function AddChartData(ALabel: string; AValue: Variant; ABackgroundColor, ABorderColor: TChartColor; APointBackgroundColor: TChartColor = None; APointBorderColor: TChartColor = None; APointHoverBackgroundColor: TChartColor = None; APointHoverBorderColor: TChartColor = None): iModelChartDataSet;
55 | function LabelName: string; overload;
56 | function Data(Index: Integer): iModelChartData;
57 | function LabelName(AValue: string): iModelChartDataSet; overload;
58 | function Opacity(AValue: Double): iModelChartDataSet; overload;
59 | function Generate: string;
60 | function ArrayValues: string;
61 | function ClearData: iModelChartDataSet;
62 | function Opacity: Double; overload;
63 | function RecordCount: integer;
64 | function &End: iModelChart;
65 | end;
66 |
67 | iModelChart = interface
68 | ['{C9BD1133-7A8F-42A9-A2C9-950251F8177A}']
69 | function AddChartDataSet(ALabel: string): iModelChartDataSet;
70 | function DataSets(Index: Integer): iModelChartDataSet;
71 | function LabelName: string; overload;
72 | function LabelName(AValue: string): iModelChart; overload;
73 | function ClearDataSets: iModelChart;
74 | function Height(AValue: string): iModelChart;
75 | function Width(AValue: string): iModelChart;
76 | function OnItemClick(ACallbackJS: string): iModelChart;
77 | function Generate: string;
78 | function Update: string;
79 | end;
80 |
81 | iModelChartFactory = interface
82 | ['{AABB99D5-B5A5-4F99-B2AF-332E27B66172}']
83 | function Bar: iModelChart;
84 | function Line: iModelChart;
85 | function Pie: iModelChart;
86 | function Doughnut: iModelChart;
87 | function PolarArea: iModelChart;
88 | function Radar: iModelChart;
89 | end;
90 |
91 | implementation
92 |
93 | end.
94 |
95 |
--------------------------------------------------------------------------------
/uModel.Charts.Line.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.Line;
2 |
3 | interface
4 |
5 | uses
6 | System.Generics.Collections,
7 | System.Variants,
8 | uModel.Charts.Interfaces,
9 | System.Classes;
10 |
11 | type
12 | TModelChartLine = class(TInterfacedObject, iModelChart)
13 | private
14 | FChartID: string;
15 | FChartDataSets: TInterfaceList;
16 | FHeight: string;
17 | FWidth: string;
18 | FLabel: string;
19 | FOnItemClick: string;
20 | public
21 | constructor Create;
22 | destructor Destroy; override;
23 | class function New: iModelChart;
24 | function AddChartDataSet(ALabel: string): iModelChartDataSet;
25 | function LabelName: string; overload;
26 | function LabelName(AValue: string): iModelChart; overload;
27 | function ClearDataSets: iModelChart;
28 | function Height(AValue: string): iModelChart;
29 | function DataSets(Index: Integer): iModelChartDataSet;
30 | function Width(AValue: string): iModelChart;
31 | function OnItemClick(ACallbackJS: string): iModelChart;
32 | function Generate: string;
33 | function Update: string;
34 | end;
35 |
36 | implementation
37 |
38 | uses
39 | System.SysUtils,
40 | uModel.Charts.Data,
41 | uModel.Charts.Utils,
42 | uModel.Charts.DataSet;
43 |
44 | { TModelChartLine }
45 |
46 | function TModelChartLine.ClearDataSets: iModelChart;
47 | begin
48 | FChartDataSets.Clear;
49 | Result := Self;
50 | end;
51 |
52 | constructor TModelChartLine.Create;
53 | begin
54 | inherited Create;
55 | FChartDataSets := TInterfaceList.Create;
56 | FChartID := 'chartjs-line' + IntToStr(Random(MaxInt));
57 | FHeight := '150px';
58 | FWidth := '400px';
59 | end;
60 |
61 | function TModelChartLine.DataSets(Index: Integer): iModelChartDataSet;
62 | begin
63 | result := FChartDataSets.Items[Index] as iModelChartDataSet;
64 | end;
65 |
66 | destructor TModelChartLine.Destroy;
67 | begin
68 | FChartDataSets.Free;
69 | inherited Destroy;
70 | end;
71 |
72 | function TModelChartLine.AddChartDataSet(ALabel: string): iModelChartDataSet;
73 | begin
74 | Result := TModelChartDataSet.New(Self, ALabel);
75 | FChartDataSets.Add(Result);
76 | end;
77 |
78 | function TModelChartLine.Height(AValue: string): iModelChart;
79 | begin
80 | FHeight := AValue;
81 | Result := Self;
82 | end;
83 |
84 | function TModelChartLine.LabelName(AValue: string): iModelChart;
85 | begin
86 | result := self;
87 | FLabel := AValue;
88 | end;
89 |
90 | function TModelChartLine.LabelName: string;
91 | begin
92 | Result := FLabel;
93 | end;
94 |
95 | function TModelChartLine.Width(AValue: string): iModelChart;
96 | begin
97 | FWidth := AValue;
98 | Result := Self;
99 | end;
100 |
101 | class function TModelChartLine.New: iModelChart;
102 | begin
103 | Result := Self.Create;
104 | end;
105 |
106 | function TModelChartLine.OnItemClick(ACallbackJS: string): iModelChart;
107 | begin
108 | Result := Self;
109 | FOnItemClick := ACallbackJS;
110 | end;
111 |
112 | function TModelChartLine.Update: string;
113 | begin
114 | var LDataSetUpdateStr := '';
115 | var LLabelsUpdateStr := '';
116 |
117 | for var i := 0 to FChartDataSets.Count - 1 do
118 | begin
119 | var LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
120 | var LDatasetsStr := LChartDataSet.ArrayValues;
121 | var LDatasetLabels := LChartDataSet.GenerateLabels;
122 |
123 | LDataSetUpdateStr := LDataSetUpdateStr + Format('chart.data.datasets[%d].data = %s;', [i, LDatasetsStr]);
124 | LLabelsUpdateStr := LLabelsUpdateStr + Format('chart.data.datasets[%d].labels = [%s];', [i, LDatasetLabels]);
125 | end;
126 |
127 | Result :=
128 | 'var chart = Chart.getChart("'+ FChartID +'");' +
129 | 'if (chart) {' +
130 | ' ' + LLabelsUpdateStr +
131 | ' ' + LDataSetUpdateStr +
132 | ' chart.update();' +
133 | '}';
134 | end;
135 |
136 | function TModelChartLine.Generate: string;
137 | var
138 | LLabelsStr, LDatasetsStr: string;
139 | LChartDataSet: iModelChartDataSet;
140 | begin
141 | LLabelsStr := EmptyStr;
142 | LDatasetsStr := EmptyStr;
143 |
144 | LLabelsStr := (FChartDataSets[0] as iModelChartDataSet).GenerateLabels;
145 |
146 | for var i := 0 to Pred(FChartDataSets.Count) do
147 | begin
148 | LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
149 | if i > 0 then
150 | LDatasetsStr := LDatasetsStr + ', ';
151 | LDatasetsStr := LDatasetsStr + LChartDataSet.Generate;
152 | end;
153 |
154 | Result := Format(
155 | '' +
156 | '', [FWidth, FHeight, LLabelsStr, LDatasetsStr]);
192 | end;
193 |
194 | end.
195 |
--------------------------------------------------------------------------------
/uModel.Charts.Pie.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.Pie;
2 |
3 | interface
4 |
5 | uses
6 | System.Generics.Collections,
7 | System.Variants,
8 | uModel.Charts.Interfaces,
9 | System.Classes;
10 |
11 | type
12 | TModelChartPie = class(TInterfacedObject, iModelChart)
13 | private
14 | FChartID: string;
15 | FChartDataSets: TInterfaceList;
16 | FHeight: string;
17 | FWidth: string;
18 | FLabel: string;
19 | FOnItemClick: string;
20 | public
21 | constructor Create;
22 | destructor Destroy; override;
23 | class function New: iModelChart;
24 | function AddChartDataSet(ALabel: string): iModelChartDataSet;
25 | function LabelName: string; overload;
26 | function LabelName(AValue: string): iModelChart; overload;
27 | function ClearDataSets: iModelChart;
28 | function Height(AValue: string): iModelChart;
29 | function Width(AValue: string): iModelChart;
30 | function DataSets(Index: Integer): iModelChartDataSet;
31 | function OnItemClick(ACallbackJS: string): iModelChart;
32 | function Generate: string;
33 | function Update: string;
34 | end;
35 |
36 | implementation
37 |
38 | uses
39 | System.SysUtils,
40 | uModel.Charts.Data,
41 | uModel.Charts.Utils,
42 | uModel.Charts.DataSet;
43 |
44 | { TModelChartPie }
45 |
46 | function TModelChartPie.ClearDataSets: iModelChart;
47 | begin
48 | Result := Self;
49 | FChartDataSets.Clear;
50 |
51 | end;
52 |
53 | constructor TModelChartPie.Create;
54 | begin
55 | inherited Create;
56 | FChartDataSets := TInterfaceList.Create;
57 | FChartID := 'chartjs-pie' + IntToStr(Random(MaxInt));
58 | FHeight := '150px';
59 | FWidth := '400px';
60 | end;
61 |
62 | function TModelChartPie.DataSets(Index: Integer): iModelChartDataSet;
63 | begin
64 | result := FChartDataSets.Items[Index] as iModelChartDataSet;
65 | end;
66 |
67 | destructor TModelChartPie.Destroy;
68 | begin
69 | FChartDataSets.Free;
70 | inherited Destroy;
71 | end;
72 |
73 | function TModelChartPie.AddChartDataSet(ALabel: string): iModelChartDataSet;
74 | begin
75 | Result := TModelChartDataSet.New(Self, ALabel);
76 | FChartDataSets.Add(Result);
77 | end;
78 |
79 | function TModelChartPie.Height(AValue: string): iModelChart;
80 | begin
81 | FHeight := AValue;
82 | Result := Self;
83 | end;
84 |
85 | function TModelChartPie.LabelName(AValue: string): iModelChart;
86 | begin
87 | result := self;
88 | FLabel := AValue;
89 | end;
90 |
91 | function TModelChartPie.LabelName: string;
92 | begin
93 | Result := FLabel;
94 | end;
95 |
96 | function TModelChartPie.Width(AValue: string): iModelChart;
97 | begin
98 | FWidth := AValue;
99 | Result := Self;
100 | end;
101 |
102 | class function TModelChartPie.New: iModelChart;
103 | begin
104 | Result := Self.Create;
105 | end;
106 |
107 | function TModelChartPie.OnItemClick(ACallbackJS: string): iModelChart;
108 | begin
109 | Result := Self;
110 | FOnItemClick := ACallbackJS;
111 | end;
112 |
113 | function TModelChartPie.Update: string;
114 | begin
115 | var LDataSetUpdateStr := '';
116 | var LLabelsUpdateStr := '';
117 |
118 | for var i := 0 to FChartDataSets.Count - 1 do
119 | begin
120 | var LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
121 | var LDatasetsStr := LChartDataSet.ArrayValues;
122 | var LDatasetLabels := LChartDataSet.GenerateLabels;
123 |
124 | LDataSetUpdateStr := LDataSetUpdateStr + Format('chart.data.datasets[%d].data = %s;', [i, LDatasetsStr]);
125 | LLabelsUpdateStr := LLabelsUpdateStr + Format('chart.data.datasets[%d].labels = [%s];', [i, LDatasetLabels]);
126 | end;
127 |
128 | Result :=
129 | 'var chart = Chart.getChart("'+ FChartID +'");' +
130 | 'if (chart) {' +
131 | ' ' + LLabelsUpdateStr +
132 | ' ' + LDataSetUpdateStr +
133 | ' chart.update();' +
134 | '}';
135 | end;
136 |
137 | function TModelChartPie.Generate: string;
138 | var
139 | LLabelsStr, LDatasetsStr: string;
140 | LChartDataSet: iModelChartDataSet;
141 | begin
142 | LLabelsStr := EmptyStr;
143 | LDatasetsStr := EmptyStr;
144 | LLabelsStr := (FChartDataSets[0] as iModelChartDataSet).GenerateLabels;
145 |
146 | for var i := 0 to Pred(FChartDataSets.Count) do
147 | begin
148 | LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
149 | if i > 0 then
150 | LDatasetsStr := LDatasetsStr + ', ';
151 | LDatasetsStr := LDatasetsStr + LChartDataSet.Generate;
152 | end;
153 |
154 | Result := Format(
155 | '' +
156 | '', [FWidth, FHeight, LLabelsStr, LDatasetsStr]);
170 | end;
171 |
172 | end.
173 |
--------------------------------------------------------------------------------
/uModel.Charts.PolarArea.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.PolarArea;
2 |
3 | interface
4 |
5 | uses
6 | System.Generics.Collections,
7 | System.Variants,
8 | uModel.Charts.Interfaces,
9 | System.Classes;
10 |
11 | type
12 | TModelChartPolarArea = class(TInterfacedObject, iModelChart)
13 | private
14 | FChartID: string;
15 | FChartDataSets: TInterfaceList;
16 | FHeight: string;
17 | FWidth: string;
18 | FLabel: string;
19 | FOnItemClick: string;
20 | public
21 | constructor Create;
22 | destructor Destroy; override;
23 | class function New: iModelChart;
24 | function AddChartDataSet(ALabel: string): iModelChartDataSet;
25 | function LabelName: string; overload;
26 | function LabelName(AValue: string): iModelChart; overload;
27 | function ClearDataSets: iModelChart;
28 | function DataSets(Index: Integer): iModelChartDataSet;
29 | function Height(AValue: string): iModelChart;
30 | function Width(AValue: string): iModelChart;
31 | function OnItemClick(ACallbackJS: string): iModelChart;
32 | function Generate: string;
33 | function Update: string;
34 | end;
35 |
36 | implementation
37 |
38 | uses
39 | System.SysUtils,
40 | uModel.Charts.Data,
41 | uModel.Charts.Utils,
42 | uModel.Charts.DataSet;
43 |
44 | { TModelChartPolarArea }
45 |
46 | function TModelChartPolarArea.ClearDataSets: iModelChart;
47 | begin
48 | FChartDataSets.Clear;
49 | Result := Self;
50 | end;
51 |
52 | constructor TModelChartPolarArea.Create;
53 | begin
54 | inherited Create;
55 | FChartDataSets := TInterfaceList.Create;
56 | FChartID := 'chartjs-polararea' + IntToStr(Random(MaxInt));
57 | FHeight := '150px';
58 | FWidth := '400px';
59 | end;
60 |
61 | function TModelChartPolarArea.DataSets(Index: Integer): iModelChartDataSet;
62 | begin
63 | result := FChartDataSets.Items[Index] as iModelChartDataSet;
64 | end;
65 |
66 | destructor TModelChartPolarArea.Destroy;
67 | begin
68 | FChartDataSets.Free;
69 | inherited Destroy;
70 | end;
71 |
72 | function TModelChartPolarArea.AddChartDataSet(ALabel: string): iModelChartDataSet;
73 | begin
74 | Result := TModelChartDataSet.New(Self, ALabel);
75 | FChartDataSets.Add(Result);
76 | end;
77 |
78 | function TModelChartPolarArea.Height(AValue: string): iModelChart;
79 | begin
80 | FHeight := AValue;
81 | Result := Self;
82 | end;
83 |
84 | function TModelChartPolarArea.LabelName(AValue: string): iModelChart;
85 | begin
86 | result := self;
87 | FLabel := AValue;
88 | end;
89 |
90 | function TModelChartPolarArea.LabelName: string;
91 | begin
92 | Result := FLabel;
93 | end;
94 |
95 | function TModelChartPolarArea.Width(AValue: string): iModelChart;
96 | begin
97 | FWidth := AValue;
98 | Result := Self;
99 | end;
100 |
101 | class function TModelChartPolarArea.New: iModelChart;
102 | begin
103 | Result := Self.Create;
104 | end;
105 |
106 | function TModelChartPolarArea.OnItemClick(ACallbackJS: string): iModelChart;
107 | begin
108 | Result := Self;
109 | FOnItemClick := ACallbackJS;
110 | end;
111 |
112 | function TModelChartPolarArea.Update: string;
113 | begin
114 | var LDataSetUpdateStr := '';
115 | var LLabelsUpdateStr := '';
116 |
117 | for var i := 0 to FChartDataSets.Count - 1 do
118 | begin
119 | var LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
120 | var LDatasetsStr := LChartDataSet.ArrayValues;
121 | var LDatasetLabels := LChartDataSet.GenerateLabels;
122 |
123 | LDataSetUpdateStr := LDataSetUpdateStr + Format('chart.data.datasets[%d].data = %s;', [i, LDatasetsStr]);
124 | LLabelsUpdateStr := LLabelsUpdateStr + Format('chart.data.datasets[%d].labels = [%s];', [i, LDatasetLabels]);
125 | end;
126 |
127 | Result :=
128 | 'var chart = Chart.getChart("'+ FChartID +'");' +
129 | 'if (chart) {' +
130 | ' ' + LLabelsUpdateStr +
131 | ' ' + LDataSetUpdateStr +
132 | ' chart.update();' +
133 | '}';
134 | end;
135 |
136 | function TModelChartPolarArea.Generate: string;
137 | var
138 | LLabelsStr, LDatasetsStr: string;
139 | LChartDataSet: iModelChartDataSet;
140 | begin
141 | LLabelsStr := EmptyStr;
142 | LDatasetsStr := EmptyStr;
143 | LLabelsStr := (FChartDataSets[0] as iModelChartDataSet).GenerateLabels;
144 |
145 | for var i := 0 to Pred(FChartDataSets.Count) do
146 | begin
147 | LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
148 | if i > 0 then
149 | LDatasetsStr := LDatasetsStr + ', ';
150 | LDatasetsStr := LDatasetsStr + LChartDataSet.Generate;
151 | end;
152 |
153 | Result := Format(
154 | '' +
155 | '',
166 | [FWidth, FHeight, LLabelsStr, LDatasetsStr]);
167 | end;
168 |
169 | end.
170 |
--------------------------------------------------------------------------------
/uModel.Charts.Radar.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.Radar;
2 |
3 | interface
4 |
5 | uses
6 | System.Generics.Collections,
7 | System.Variants,
8 | uModel.Charts.Interfaces,
9 | System.Classes;
10 |
11 | type
12 | TModelChartRadar = class(TInterfacedObject, iModelChart)
13 | private
14 | FChartID: string;
15 | FChartDataSets: TInterfaceList;
16 | FHeight: string;
17 | FWidth: string;
18 | FLabel: string;
19 | FOnItemClick: string;
20 | public
21 | constructor Create;
22 | destructor Destroy; override;
23 | class function New: iModelChart;
24 | function AddChartDataSet(ALabel: string): iModelChartDataSet;
25 | function LabelName: string; overload;
26 | function LabelName(AValue: string): iModelChart; overload;
27 | function ClearDataSets: iModelChart;
28 | function DataSets(Index: Integer): iModelChartDataSet;
29 | function Height(AValue: string): iModelChart;
30 | function Width(AValue: string): iModelChart;
31 | function OnItemClick(ACallbackJS: string): iModelChart;
32 | function Generate: string;
33 | function Update: string;
34 | end;
35 |
36 | implementation
37 |
38 | uses
39 | System.SysUtils,
40 | uModel.Charts.Data,
41 | uModel.Charts.Utils,
42 | uModel.Charts.DataSet;
43 |
44 | { TModelChartRadar }
45 |
46 | function TModelChartRadar.ClearDataSets: iModelChart;
47 | begin
48 | FChartDataSets.Clear;
49 | Result := Self;
50 | end;
51 |
52 | constructor TModelChartRadar.Create;
53 | begin
54 | inherited Create;
55 | FChartDataSets := TInterfaceList.Create;
56 | FChartID := 'chartjs-radar' + IntToStr(Random(MaxInt));
57 | FHeight := '150px';
58 | FWidth := '400px';
59 | end;
60 |
61 | function TModelChartRadar.DataSets(Index: Integer): iModelChartDataSet;
62 | begin
63 | result := FChartDataSets.Items[Index] as iModelChartDataSet;
64 | end;
65 |
66 | destructor TModelChartRadar.Destroy;
67 | begin
68 | FChartDataSets.Free;
69 | inherited Destroy;
70 | end;
71 |
72 | function TModelChartRadar.AddChartDataSet(ALabel: string): iModelChartDataSet;
73 | begin
74 | Result := TModelChartDataSet.New(Self, ALabel);
75 | FChartDataSets.Add(Result);
76 | end;
77 |
78 | function TModelChartRadar.Height(AValue: string): iModelChart;
79 | begin
80 | FHeight := AValue;
81 | Result := Self;
82 | end;
83 |
84 | function TModelChartRadar.LabelName(AValue: string): iModelChart;
85 | begin
86 | result := self;
87 | FLabel := AValue;
88 | end;
89 |
90 | function TModelChartRadar.LabelName: string;
91 | begin
92 | Result := FLabel;
93 | end;
94 |
95 | function TModelChartRadar.Width(AValue: string): iModelChart;
96 | begin
97 | FWidth := AValue;
98 | Result := Self;
99 | end;
100 |
101 | class function TModelChartRadar.New: iModelChart;
102 | begin
103 | Result := Self.Create;
104 | end;
105 |
106 | function TModelChartRadar.OnItemClick(ACallbackJS: string): iModelChart;
107 | begin
108 | Result := Self;
109 | FOnItemClick := ACallbackJS;
110 | end;
111 |
112 | function TModelChartRadar.Update: string;
113 | begin
114 | var LDataSetUpdateStr := '';
115 | var LLabelsUpdateStr := '';
116 |
117 | for var i := 0 to FChartDataSets.Count - 1 do
118 | begin
119 | var LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
120 | var LDatasetsStr := LChartDataSet.ArrayValues;
121 | var LDatasetLabels := LChartDataSet.GenerateLabels;
122 |
123 | LDataSetUpdateStr := LDataSetUpdateStr + Format('chart.data.datasets[%d].data = %s;', [i, LDatasetsStr]);
124 | LLabelsUpdateStr := LLabelsUpdateStr + Format('chart.data.datasets[%d].labels = [%s];', [i, LDatasetLabels]);
125 | end;
126 |
127 | Result :=
128 | 'var chart = Chart.getChart("'+ FChartID +'");' +
129 | 'if (chart) {' +
130 | ' ' + LLabelsUpdateStr +
131 | ' ' + LDataSetUpdateStr +
132 | ' chart.update();' +
133 | '}';
134 | end;
135 |
136 | function TModelChartRadar.Generate: string;
137 | var
138 | LLabelsStr, LDatasetsStr: string;
139 | LChartDataSet: iModelChartDataSet;
140 | begin
141 | LLabelsStr := EmptyStr;
142 | LDatasetsStr := EmptyStr;
143 | LLabelsStr := (FChartDataSets[0] as iModelChartDataSet).GenerateLabels;
144 |
145 | for var i := 0 to Pred(FChartDataSets.Count) do
146 | begin
147 | LChartDataSet := (FChartDataSets[i] as iModelChartDataSet);
148 | if i > 0 then
149 | LDatasetsStr := LDatasetsStr + ', ';
150 | LDatasetsStr := LDatasetsStr + LChartDataSet.Generate;
151 | end;
152 |
153 | Result := Format(
154 | '' +
155 | '',
166 | [FWidth, FHeight, LLabelsStr, LDatasetsStr]);
167 | end;
168 |
169 | end.
170 |
--------------------------------------------------------------------------------
/uModel.Charts.Utils.pas:
--------------------------------------------------------------------------------
1 | unit uModel.Charts.Utils;
2 |
3 | interface
4 |
5 | uses
6 | uModel.Charts.Interfaces;
7 |
8 | function ColorEnumToString(AColor: TChartColor; AOpacity: Double = 1.0): string;
9 |
10 | implementation
11 |
12 | uses
13 | System.SysUtils;
14 |
15 | function ColorEnumToString(AColor: TChartColor; AOpacity: Double = 1.0): string;
16 | begin
17 | case AColor of
18 | none: Result := 'rgba(222, 226, 230, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
19 | blue: Result := 'rgba(13, 110, 253, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
20 | indigo: Result := 'rgba(102, 16, 242, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
21 | purple: Result := 'rgba(111, 66, 193, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
22 | pink: Result := 'rgba(214, 51, 132, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
23 | red: Result := 'rgba(220, 53, 69, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
24 | orange: Result := 'rgba(253, 126, 20, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
25 | yellow: Result := 'rgba(255, 193, 7, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
26 | green: Result := 'rgba(25, 135, 84, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
27 | teal: Result := 'rgba(32, 201, 151, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
28 | cyan: Result := 'rgba(13, 202, 240, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
29 | black: Result := 'rgba(0, 0, 0, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
30 | white: Result := 'rgba(255, 255, 255, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
31 | gray: Result := 'rgba(108, 117, 125, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
32 | graydark: Result := 'rgba(52, 58, 64, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
33 | gray100: Result := 'rgba(248, 249, 250, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
34 | gray200: Result := 'rgba(233, 236, 239, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
35 | gray300: Result := 'rgba(222, 226, 230, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
36 | gray400: Result := 'rgba(206, 212, 218, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
37 | gray500: Result := 'rgba(173, 181, 189, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
38 | gray600: Result := 'rgba(108, 117, 125, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
39 | gray700: Result := 'rgba(73, 80, 87, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
40 | gray800: Result := 'rgba(52, 58, 64, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
41 | gray900: Result := 'rgba(33, 37, 41, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
42 | primary: Result := 'rgba(13, 110, 253, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
43 | secondary: Result := 'rgba(108, 117, 125, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
44 | success: Result := 'rgba(25, 135, 84, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
45 | info: Result := 'rgba(13, 202, 240, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
46 | warning: Result := 'rgba(255, 193, 7, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
47 | danger: Result := 'rgba(220, 53, 69, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
48 | light: Result := 'rgba(248, 249, 250, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
49 | dark: Result := 'rgba(33, 37, 41, ' + StringReplace(FloatToStr(AOpacity), ',', '.', [rfReplaceAll]) + ')';
50 | transparent: Result := 'transparent';
51 | end;
52 | end;
53 |
54 | end.
55 |
--------------------------------------------------------------------------------