fpspreadsheet: More user-friendly chart background style.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8972 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2023-10-23 09:28:05 +00:00
parent 623d0bd4c5
commit ce4e36ca38
3 changed files with 122 additions and 36 deletions

View File

@ -1,5 +1,7 @@
program write_chart_demo; program write_chart_demo;
{.$DEFINE DARK_MODE}
uses uses
SysUtils, fpspreadsheet, fpstypes, fpschart, xlsxooxml, fpsopendocument; SysUtils, fpspreadsheet, fpstypes, fpschart, xlsxooxml, fpsopendocument;
var var
@ -8,8 +10,6 @@ var
ch: TsChart; ch: TsChart;
ser: TsChartSeries; ser: TsChartSeries;
i: Integer; i: Integer;
bg: TsChartFill;
frm: TsChartLine;
begin begin
b := TsWorkbook.Create; b := TsWorkbook.Create;
try try
@ -28,18 +28,20 @@ begin
ser.SetLabelRange(1, 0, 7, 0); ser.SetLabelRange(1, 0, 7, 0);
ser.SetYRange(1, 1, 7, 1); ser.SetYRange(1, 1, 7, 1);
bg.FgColor := scYellow; {$IFDEF DARK_MODE}
bg.Style := fsSolidFill; ch.Background.FgColor := scBlack;
ch.Background := bg; ch.Background.Style := fsSolidFill;
frm.color := scRed; ch.Border.Color := scWhite;
frm.Style := clsSolid; ch.Border.Style := clsSolid;
ch.Border := frm; {$ENDIF}
ch.Title.Caption := 'HALLO'; ch.Title.Caption := 'HALLO';
ch.Title.Visible := true; ch.Title.Visible := true;
ch.SubTitle.Caption := 'hallo'; ch.SubTitle.Caption := 'hallo';
ch.SubTitle.Visible := true; ch.SubTitle.Visible := true;
ch.YAxis.ShowMajorGridLines := true; ch.YAxis.ShowMajorGridLines := true;
ch.YAxis.ShowMinorGridLines := true; ch.YAxis.ShowMinorGridLines := true;

View File

@ -27,11 +27,36 @@ var
type type
TsChart = class; TsChart = class;
TsChartFill = record TsChartLineRec = record
Style: Integer; // index into chart's LineStyle list or predefined clsSolid/clsNoLine
Width: Double; // mm
Color: TsColor; // in hex: $00bbggrr, r=red, g=green, b=blue
Transparency: Double; // in percent
class operator = (A, B: TsChartLineRec): Boolean;
end;
TsChartLine = class
Style: Integer; // index into chart's LineStyle list or predefined clsSolid/clsNoLine
Width: Double; // mm
Color: TsColor; // in hex: $00bbggrr, r=red, g=green, b=blue
Transparency: Double; // in percent
procedure FromRecord(const ALine: TsChartLineRec);
function ToRecord: TsChartLineRec;
end;
TsChartFillRec = record
Style: TsFillStyle; Style: TsFillStyle;
FgColor: TsColor; FgColor: TsColor;
BgColor: TsColor; BgColor: TsColor;
class operator = (A, B: TsChartFill): Boolean; class operator = (A, B: TsChartFillRec): Boolean;
end;
TsChartFill = class
Style: TsFillStyle;
FgColor: TsColor;
BgColor: TsColor;
procedure FromRecord(const AFill: TsChartFillRec);
function ToRecord: TsChartFillRec;
end; end;
TsChartLineSegment = record TsChartLineSegment = record
@ -60,13 +85,6 @@ type
property Items[AIndex: Integer]: TsChartLineStyle read GetItem write SetItem; default; property Items[AIndex: Integer]: TsChartLineStyle read GetItem write SetItem; default;
end; end;
TsChartLine = record
Style: Integer; // index into chart's LineStyle list or predefined clsSolid/clsNoLine
Width: Double; // mm
Color: TsColor; // in hex: $00bbggrr, r=red, g=green, b=blue
Transparency: Double; // in percent
end;
TsChartElement = class TsChartElement = class
private private
FChart: TsChart; FChart: TsChart;
@ -83,6 +101,7 @@ type
FBorder: TsChartLine; FBorder: TsChartLine;
public public
constructor Create(AChart: TsChart); constructor Create(AChart: TsChart);
destructor Destroy; override;
property Background: TsChartFill read FBackground write FBackground; property Background: TsChartFill read FBackground write FBackground;
property Border: TsChartLine read FBorder write FBorder; property Border: TsChartLine read FBorder write FBorder;
end; end;
@ -201,9 +220,7 @@ type
TsLineSeries = class(TsChartSeries) TsLineSeries = class(TsChartSeries)
private private
FLineStyle: TsChartLine; FLine: TsChartLine;
FShowLines: Boolean;
FShowSymbols: Boolean;
FSymbol: TsChartSeriesSymbol; FSymbol: TsChartSeriesSymbol;
FSymbolFill: TsChartFill; FSymbolFill: TsChartFill;
FSymbolBorder: TsChartLine; FSymbolBorder: TsChartLine;
@ -211,9 +228,8 @@ type
FSymbolWidth: Double; // in mm FSymbolWidth: Double; // in mm
public public
constructor Create(AChart: TsChart); constructor Create(AChart: TsChart);
property LineStyle: TsChartLine read FLineStyle write FLineStyle; destructor Destroy; override;
property ShowLines: Boolean read FShowLines write FShowLines; property Line: TsChartLine read FLine write FLine;
property ShowSymbols: Boolean read FShowSymbols write FShowSymbols;
property Symbol: TsChartSeriesSymbol read FSymbol write FSymbol; property Symbol: TsChartSeriesSymbol read FSymbol write FSymbol;
property SymbolBorder: TsChartLine read FSymbolBorder write FSymbolBorder; property SymbolBorder: TsChartLine read FSymbolBorder write FSymbolBorder;
property SymbolFill: TsChartFill read FSymbolFill write FSymbolFill; property SymbolFill: TsChartFill read FSymbolFill write FSymbolFill;
@ -330,13 +346,51 @@ implementation
const const
DEFAULT_LINE_WIDTH = 0.75; // pts DEFAULT_LINE_WIDTH = 0.75; // pts
{ TsChartFill } { TsChartLineRec }
class operator TsChartLineRec.= (A, B: TsChartLineRec): Boolean;
begin
Result := (A.Style = B.Style) and (A.Width = B.Width) and
(A.Color = B.Color) and (A.Transparency = B.Transparency);
end;
class operator TsChartFill.= (A, B: TsChartFill): Boolean; { TsChartLine }
procedure TsChartLine.FromRecord(const ALine: TsChartLineRec);
begin
Style := ALine.Style;
Width := ALine.Width;
Color := ALine.Color;
Transparency := ALine.Transparency;
end;
function TsChartLine.ToRecord: TsChartLineRec;
begin
Result.Style := Style;
Result.Width := Width;
Result.Color := Color;
Result.Transparency := Transparency;
end;
{ TsChartFillRec }
class operator TsChartFillRec.= (A, B: TsChartFillRec): Boolean;
begin begin
Result := (A.Style = B.Style) and (A.FgColor = B.FgColor) and (A.BgColor = B.BgColor); Result := (A.Style = B.Style) and (A.FgColor = B.FgColor) and (A.BgColor = B.BgColor);
end; end;
{ TsChartFill }
procedure TsChartFill.FromRecord(const AFill: TsChartFillRec);
begin
Style := AFill.Style;
FgColor := AFill.FgColor;
BgColor := AFill.BgColor;
end;
function TsChartFill.ToRecord: TsChartFillRec;
begin
Result.Style := Style;
Result.BgColor := BgColor;
Result.FgColor := FgColor;
end;
{ TsChartLineStyle } { TsChartLineStyle }
@ -397,14 +451,23 @@ end;
constructor TsChartFillElement.Create(AChart: TsChart); constructor TsChartFillElement.Create(AChart: TsChart);
begin begin
inherited Create(AChart); inherited Create(AChart);
FBackground := TsChartFill.Create;
FBackground.Style := fsSolidFill; FBackground.Style := fsSolidFill;
FBackground.BgColor := scWhite; FBackground.BgColor := scWhite;
FBackground.FgColor := scWhite; FBackground.FgColor := scWhite;
FBorder := TsChartLine.Create;
FBorder.Style := clsSolid; FBorder.Style := clsSolid;
FBorder.Width := PtsToMM(DEFAULT_LINE_WIDTH); FBorder.Width := PtsToMM(DEFAULT_LINE_WIDTH);
FBorder.Color := scBlack; FBorder.Color := scBlack;
end; end;
destructor TsChartFillElement.Destroy;
begin
FBorder.Free;
FBackground.Free;
inherited;
end;
{ TsChartText } { TsChartText }
@ -443,22 +506,27 @@ begin
FShowLabels := true; FShowLabels := true;
FAxisLine := TsChartLine.Create;
FAxisLine.Color := scBlack; FAxisLine.Color := scBlack;
FAxisLine.Style := clsSolid; FAxisLine.Style := clsSolid;
FAxisLine.Width := PtsToMM(DEFAULT_LINE_WIDTH); FAxisLine.Width := PtsToMM(DEFAULT_LINE_WIDTH);
FMajorTickLines := TsChartLine.Create;
FMajorTickLines.Color := scBlack; FMajorTickLines.Color := scBlack;
FMajorTickLines.Style := clsSolid; FMajorTickLines.Style := clsSolid;
FMajorTickLines.Width := PtsToMM(DEFAULT_LINE_WIDTH); FMajorTickLines.Width := PtsToMM(DEFAULT_LINE_WIDTH);
FMinorTickLines := TsChartLine.Create;
FMinorTickLines.Color := scBlack; FMinorTickLines.Color := scBlack;
FMinorTickLines.Style := clsSolid; FMinorTickLines.Style := clsSolid;
FMinorTickLines.Width := PtsToMM(DEFAULT_LINE_WIDTH); FMinorTickLines.Width := PtsToMM(DEFAULT_LINE_WIDTH);
FMajorGridLines := TsChartLine.Create;
FMajorGridLines.Color := scSilver; FMajorGridLines.Color := scSilver;
FMajorGridLines.Style := clsSolid; FMajorGridLines.Style := clsSolid;
FMajorGridLines.Width := PtsToMM(DEFAULT_LINE_WIDTH); FMajorGridLines.Width := PtsToMM(DEFAULT_LINE_WIDTH);
FMinorGridLines := TsChartLine.Create;
FMinorGridLines.Color := scSilver; FMinorGridLines.Color := scSilver;
FMinorGridLines.Style := clsDot; FMinorGridLines.Style := clsDot;
FMinorGridLines.Width := PtsToMM(DEFAULT_LINE_WIDTH); FMinorGridLines.Width := PtsToMM(DEFAULT_LINE_WIDTH);
@ -466,6 +534,11 @@ end;
destructor TsChartAxis.Destroy; destructor TsChartAxis.Destroy;
begin begin
FMinorGridLines.Free;
FMajorGridLines.Free;
FMinorTickLines.Free;
FMajorTickLines.Free;
FAxisLine.Free;
FLabelFont.Free; FLabelFont.Free;
inherited; inherited;
end; end;
@ -623,14 +696,17 @@ begin
FChartType := ctLine; FChartType := ctLine;
FLineStyle.Color := scBlack; FLine := TsChartLine.Create;
FLineStyle.Style := clsSolid; FLine.Color := scBlack;
FLineStyle.Width := PtsToMM(DEFAULT_LINE_WIDTH); FLine.Style := clsSolid;
FLine.Width := PtsToMM(DEFAULT_LINE_WIDTH);
FSymbolBorder := TsChartline.Create;
FSymbolBorder.Color := scBlack; FSymbolBorder.Color := scBlack;
FSymbolBorder.Style := clsSolid; FSymbolBorder.Style := clsSolid;
FSymbolBorder.Width := PtsToMM(DEFAULT_LINE_WIDTH); FSymbolBorder.Width := PtsToMM(DEFAULT_LINE_WIDTH);
FSymbolFill := TsChartFill.Create;
FSymbolFill.FgColor := scWhite; FSymbolFill.FgColor := scWhite;
FSymbolFill.BgColor := scWhite; FSymbolFill.BgColor := scWhite;
FSymbolFill.Style := fsSolidFill; FSymbolFill.Style := fsSolidFill;
@ -639,6 +715,14 @@ begin
FSymbolHeight := 2.5; FSymbolHeight := 2.5;
end; end;
destructor TsLineSeries.Destroy;
begin
FSymbolFill.Free;
FSymbolBorder.Free;
FLine.Free;
inherited;
end;
{ TsChart } { TsChart }

View File

@ -16,13 +16,13 @@ type
TsChartBackgroundStyle = class(TsChartStyle) TsChartBackgroundStyle = class(TsChartStyle)
private private
FBackground: TsChartFill; FBackground: TsChartFillRec;
FBorder: TsChartLine; FBorder: TsChartLineRec;
public public
procedure ApplyToChart(AChart: TsChart); override; procedure ApplyToChart(AChart: TsChart); override;
procedure ExtractFromChart(AChart: TsChart); override; procedure ExtractFromChart(AChart: TsChart); override;
property Background: TsChartFill read FBackground; property Background: TsChartFillRec read FBackground;
property Border: TsChartLine read FBorder; property Border: TsChartLineRec read FBorder;
end; end;
TsChartStyleList = class(TFPList) TsChartStyleList = class(TFPList)
@ -38,14 +38,14 @@ implementation
procedure TsChartBackgroundStyle.ApplyToChart(AChart: TsChart); procedure TsChartBackgroundStyle.ApplyToChart(AChart: TsChart);
begin begin
AChart.Background := FBackground; AChart.Background.FromRecord(FBackground);
AChart.Border := FBorder; AChart.Border.FromRecord(FBorder);
end; end;
procedure TsChartBackgroundStyle.ExtractFromChart(AChart: TsChart); procedure TsChartBackgroundStyle.ExtractFromChart(AChart: TsChart);
begin begin
FBackground := AChart.Background; FBackground := AChart.Background.ToRecord;
FBorder := AChart.Border; FBorder := AChart.Border.ToRecord;
end; end;
{ TsChartStyleList } { TsChartStyleList }