2023-10-23 00:03:45 +00:00
|
|
|
unit fpsChartStyles;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, fpsTypes, fpsChart;
|
|
|
|
|
|
|
|
type
|
2023-10-23 10:58:15 +00:00
|
|
|
TsChartStyleType = (cstBackground, cstWall, cstFloor);
|
|
|
|
|
2023-10-23 00:03:45 +00:00
|
|
|
TsChartStyle = class
|
2023-10-23 10:58:15 +00:00
|
|
|
private
|
|
|
|
FStyleType: TsChartStyleType;
|
2023-10-23 00:03:45 +00:00
|
|
|
public
|
2023-10-23 10:58:15 +00:00
|
|
|
constructor Create(AStyleType: TsChartStyleType); virtual;
|
2023-10-23 00:03:45 +00:00
|
|
|
procedure ApplyToChart(AChart: TsChart); virtual; abstract;
|
|
|
|
procedure ExtractFromChart(AChart: TsChart); virtual; abstract;
|
2023-10-23 10:58:15 +00:00
|
|
|
property StyleType: TsChartStyleType read FStyleType;
|
2023-10-23 00:03:45 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
TsChartBackgroundStyle = class(TsChartStyle)
|
|
|
|
private
|
2023-10-23 09:28:05 +00:00
|
|
|
FBackground: TsChartFillRec;
|
|
|
|
FBorder: TsChartLineRec;
|
2023-10-23 00:03:45 +00:00
|
|
|
public
|
|
|
|
procedure ApplyToChart(AChart: TsChart); override;
|
|
|
|
procedure ExtractFromChart(AChart: TsChart); override;
|
2023-10-23 09:28:05 +00:00
|
|
|
property Background: TsChartFillRec read FBackground;
|
|
|
|
property Border: TsChartLineRec read FBorder;
|
2023-10-23 00:03:45 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
TsChartStyleList = class(TFPList)
|
2023-10-23 10:58:15 +00:00
|
|
|
protected
|
|
|
|
|
2023-10-23 00:03:45 +00:00
|
|
|
public
|
|
|
|
destructor Destroy; override;
|
2023-10-23 10:58:15 +00:00
|
|
|
procedure AddChartBackgroundStyle(AChart: TsChart; AStyleType: TsChartStyleType);
|
2023-10-23 00:03:45 +00:00
|
|
|
procedure Clear;
|
2023-10-23 10:58:15 +00:00
|
|
|
function FindChartBackgroundStyle(AChart: TsChart; AStyleType: TsChartStyleType): Integer;
|
2023-10-23 00:03:45 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2023-10-23 10:58:15 +00:00
|
|
|
{ TsChartStyle }
|
|
|
|
|
|
|
|
constructor TsChartStyle.Create(AStyleType: TsChartStyleType);
|
|
|
|
begin
|
|
|
|
FStyleType := AStyleType;
|
|
|
|
end;
|
|
|
|
|
2023-10-23 00:03:45 +00:00
|
|
|
{ TsChartBackgroundstyle }
|
|
|
|
|
|
|
|
procedure TsChartBackgroundStyle.ApplyToChart(AChart: TsChart);
|
|
|
|
begin
|
2023-10-23 10:58:15 +00:00
|
|
|
case FStyleType of
|
|
|
|
cstBackground:
|
|
|
|
begin
|
|
|
|
AChart.Background.FromRecord(FBackground);
|
|
|
|
AChart.Border.FromRecord(FBorder);
|
|
|
|
end;
|
|
|
|
cstWall:
|
|
|
|
begin
|
|
|
|
AChart.PlotArea.Background.FromRecord(FBackground);
|
|
|
|
AChart.PlotArea.Border.FromRecord(FBorder);
|
|
|
|
end;
|
|
|
|
cstFloor:
|
|
|
|
begin
|
|
|
|
AChart.Floor.Background.FromRecord(FBackGround);
|
|
|
|
AChart.Floor.Border.FromRecord(FBorder);
|
|
|
|
end;
|
|
|
|
end;
|
2023-10-23 00:03:45 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TsChartBackgroundStyle.ExtractFromChart(AChart: TsChart);
|
|
|
|
begin
|
2023-10-23 10:58:15 +00:00
|
|
|
case FStyleType of
|
|
|
|
cstBackground:
|
|
|
|
begin
|
|
|
|
FBackground := AChart.Background.ToRecord;
|
|
|
|
FBorder := AChart.Border.ToRecord;
|
|
|
|
end;
|
|
|
|
cstWall:
|
|
|
|
begin
|
|
|
|
FBackground := AChart.PlotArea.Background.ToRecord;
|
|
|
|
FBorder := AChart.PlotArea.Border.ToRecord;
|
|
|
|
end;
|
|
|
|
cstFloor:
|
|
|
|
begin
|
|
|
|
FBackground := AChart.Floor.Background.ToRecord;
|
|
|
|
FBorder := AChart.Floor.Border.ToRecord;
|
|
|
|
end;
|
|
|
|
end;
|
2023-10-23 00:03:45 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ TsChartStyleList }
|
|
|
|
|
|
|
|
destructor TsChartStyleList.Destroy;
|
|
|
|
begin
|
|
|
|
Clear;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2023-10-23 10:58:15 +00:00
|
|
|
{ Adds the style of the specified type in the given chart as new style to the
|
|
|
|
style list. But only if the same style does not yet exist. }
|
|
|
|
procedure TsChartStyleList.AddChartBackgroundStyle(AChart: TsChart;
|
|
|
|
AStyleType: TsChartStyleType);
|
|
|
|
begin
|
|
|
|
FindChartBackgroundStyle(AChart, AStyleType);
|
|
|
|
end;
|
|
|
|
|
2023-10-23 00:03:45 +00:00
|
|
|
procedure TsChartStyleList.Clear;
|
|
|
|
var
|
|
|
|
j: Integer;
|
|
|
|
begin
|
|
|
|
for j := 0 to Count-1 do
|
|
|
|
TsChartStyle(Items[j]).Free;
|
|
|
|
inherited Clear;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ Searches whether the background style of the specified chart is already in the
|
|
|
|
list. If not, a new style is created and added.
|
2023-10-23 10:58:15 +00:00
|
|
|
The type of the requested background must be provided as parameter.
|
2023-10-23 00:03:45 +00:00
|
|
|
Returns the index of the style. }
|
2023-10-23 10:58:15 +00:00
|
|
|
function TsChartStyleList.FindChartBackgroundStyle(AChart: TsChart;
|
|
|
|
AStyleType: TsChartStyleType): Integer;
|
2023-10-23 00:03:45 +00:00
|
|
|
var
|
|
|
|
newStyle, style: TsChartBackgroundStyle;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
Result := -1;
|
2023-10-23 10:58:15 +00:00
|
|
|
newStyle := TsChartBackgroundStyle.Create(AStyleType);
|
2023-10-23 00:03:45 +00:00
|
|
|
newStyle.ExtractFromChart(AChart);
|
|
|
|
for i := 0 to Count-1 do
|
|
|
|
begin
|
2023-10-23 10:58:15 +00:00
|
|
|
if (TsChartStyle(Items[i]) is TsChartBackgroundStyle) and
|
|
|
|
(TsChartStyle(Items[i]).StyleType = AStyleType) then
|
2023-10-23 00:03:45 +00:00
|
|
|
begin
|
2023-10-23 10:58:15 +00:00
|
|
|
style := TsChartBackgroundStyle(Items[i]);
|
|
|
|
if (style.Background = newStyle.Background) then
|
|
|
|
begin
|
|
|
|
Result := i;
|
|
|
|
break;
|
|
|
|
end;
|
2023-10-23 00:03:45 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
if Result = -1 then
|
|
|
|
Result := Add(newStyle)
|
|
|
|
else
|
|
|
|
newStyle.Free;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|